Redundancy in C#?

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

    Because declaring a type doesn't necessarily have anything to do with initializing it.

    I can declare

    List foo; 
    

    and leave it to be initialized later. Where's the redundancy then? Maybe it receives the value from another function like BuildList().

    As others have mentioned the new var keyword lets you get around that, but you have to initialize the variable at declaration so that the compiler can tell what type it is.

提交回复
热议问题