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

前端 未结 5 2051
醉梦人生
醉梦人生 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:34

    There are technical issues with implementing this feature. The common cases seem simple but the tougher cases (e.g., fields referencing other fields in chains or cycles, expressions which contain anonymous types) are not.

    See Eric Lippert's blog for an in-depth explanation: Why no var on fields?

提交回复
热议问题