开发者

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

开发者 https://www.devze.com 2023-02-16 18:22 出处:网络
Below is the interface and its implementation. interface ICompanyService { Company GetCompany(); } public class CompanyService : ICompanyService

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

精彩评论

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