c# - Why cannot an implicitly typed variable be declared at class level? -
this question has answer here:
- why class fields cannot var? [duplicate] 5 answers
so code below valid , outputs 5 excpected , type of foo inferred system.int32.
class program { static void main() { var foo = 5; console.writeline(foo); } } but if write this
class program { static var foo = 5; static void main() { console.writeline(foo); } } you following error:
the contextual keyword 'var' may appear within local variable declaration.
what problem of declaring variable using var-keyword @ class level? don't quite understand this, can make clear?
well 1 reason var cannot accessed outside scope had declared , , static persists , can accessed globally(here can see conflicts of intrests) , there goes answer can not declare var static.
implicitly typed local variables
Comments
Post a Comment