开发者

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 Compa开发者_如何学Gony();

    }
}

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


They are identical underneath.

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜