Spock
Spock

Reputation: 6992

What is the correct usage of C# var when using the interface members?

Below is the interface and its implementation.

interface ICompanyService
{
    Company GetCompany();
}

public class CompanyService : ICompanyService
{
    public Company GetCompany()
    {
        //Do something
        return new Company();

    }
}

Now if below "companyService" is the implementation of above ICompanyService, which one is better (A or B) and why?

  var company = companyService.GetCompany(); //.....A

  Company company = companyService.GetCompany(); //.....B

Upvotes: 0

Views: 141

Answers (2)

Nils Magne Lunde
Nils Magne Lunde

Reputation: 1824

Like Arnis says, they are identical underneath. Personally I'd prefer B in this case since you can't see the return type just by looking at the statement.

Upvotes: 0

Arnis Lapsa
Arnis Lapsa

Reputation: 47647

They are identical underneath.

A is better because of readability. But that's subjective. Some people like lengthy lines.

Upvotes: 1

Related Questions