Search code examples
c#.netvariablesimplicit-typing

Implicit typing; why just local variables?


Does anyone know or care to speculate why implicit typing is limited to local variables?

var thingy = new Foo();

But why not...

var getFoo() {
    return new Foo(); 
}

Solution

  • Eric Lippert did an entire blog post on the subject.

    In summary, the main problem is that it would have required a major re-architecture of the C# compiler to do so. Declarations are currently processed in a single pass manner. This would require multiple passes because of the ability to form cycles between inferred variables. VB.NET has roughly the same problem.