Why would var be a bad thing?

前端 未结 17 1933
误落风尘
误落风尘 2020-12-04 17:21

I\'ve been chatting with my colleagues the other day and heard that their coding standard explicitly forbids them to use the var keyword in C#. They had no idea

17条回答
  •  旧时难觅i
    2020-12-04 18:08

    I wrote a blog article on this topic a few months ago. For me, I use it every where possible and specifically design my APIs around type inference. The basic reasons I use type inference are

    1. It does not reduce type safety
    2. It will actually increase type safety in your code by alerting you to implicit casts. The best example in the foreach statement
    3. Maintains DRY principles in C#. This is specifically for the declaration case, why bother saying the name twice?
    4. In some cases it's flat out required. Example anonymous types
    5. Less typing with no loss of functionality.

    http://blogs.msdn.com/jaredpar/archive/2008/09/09/when-to-use-type-inference.aspx

提交回复
热议问题