Redundancy in C#?

前端 未结 17 1601
猫巷女王i
猫巷女王i 2021-02-13 19:28

Take the following snippet:

List distances = new List();

Was the redundancy intended by the language designers? If so, wh

17条回答
  •  佛祖请我去吃肉
    2021-02-13 19:42

    Use var if it is obvious what the type is to the reader.

    //Use var here
    var names = new List();
    
    //but not here
    List names = GetNames();
    

    From microsofts C# programing guide

    The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code

提交回复
热议问题