Redundancy in C#?

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

    The compiler improvements for C# 3.0 (which corresponds with .Net 3.5) eliminate some of this sort of thing. So your code can now be written as:

    var distances = new List();
    

    The updated compiler is much better at figuring out types based on additional information in the statement. That means that there are fewer instances where you need to specify a type either for an assignment, or as part of a Generic.

    That being said, there are still some areas which could be improved. Some of that is API and some is simply due to the restrictions of strong typing.

提交回复
热议问题