Redundancy in C#?

前端 未结 17 1544
猫巷女王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:43

    I see one other problem with the using of var for laziness like that

    var names = new List();
    

    If you use var, the variable named "names" is typed as List, but you would eventually only use one of the interfaces inherited by List.

    IList = new List();
    ICollection = new List();
    IEnumerable = new List();
    

    You can automatically use everything of that, but can you consider what interface you wanted to use at the time you wrote the code?

    The var keyword does not improve readability in this example.

提交回复
热议问题