Why can't we declare var a = new List at class level?

前端 未结 5 2053
醉梦人生
醉梦人生 2021-01-18 23:27

I know we cannot do this at class level but at method level we can always do this.

var myList=new List // or something else like this
<         


        
5条回答
  •  再見小時候
    2021-01-18 23:36

    The var keyword was invented specific to support anonymous types. You are generally NOT going to declare anonymous types at the class level, and thus it was not implemented.

    Your example statement

    var myList=new List
    

    is not a very good example of how to use the var keyword since it's not for the intended purpose.

提交回复
热议问题