Will using 'var' affect performance?

后端 未结 12 1652
南方客
南方客 2020-11-22 14:01

Earlier I asked a question about why I see so many examples use the varkeyword and got the answer that while it is only necessary for anonymous types, that it is used noneth

12条回答
  •  礼貌的吻别
    2020-11-22 14:22

    I always use the word var in web articles or guides writings.

    The width of the text editor of online article is small.

    If I write this:

    SomeCoolNameSpace.SomeCoolClassName.SomeCoolSubClassName coolClass = new SomeCoolNameSpace.SomeCoolClassName.SomeCoolSubClassName();
    

    You will see that above rendered pre code text is too long and flows out of the box, it gets hidden. The reader needs to scroll to the right to see the complete syntax.

    That's why I always use the keyword var in web article writings.

    var coolClass = new SomeCoolNameSpace.SomeCoolClassName.SomeCoolSubClassName();
    

    The whole rendered pre code just fit within the screen.

    In practice, for declaring object, I seldom use var, I rely on intellisense to declare object faster.

    Example:

    SomeCoolNamespace.SomeCoolObject coolObject = new SomeCoolNamespace.SomeCoolObject();
    

    But, for returning object from a method, I use var to write code faster.

    Example:

    var coolObject = GetCoolObject(param1, param2);
    

提交回复
热议问题