Reputation: 6463
I just found out that it is possible to use the keyword var
as a class name:
public class var // no problem here
{
}
Now if I overload the implicit cast operator I can use my class in an interesting manner:
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
var x = 1; // var is of type MyApp.var
}
}
public class var
{
public implicit operator var(int i)
{
return new var();
}
}
}
In such scenario, is it still possible to somehow make the compiler infer the types? This blog entry states that it is not possible (go to the paragraph starting with "Or, another example."), but maybe something changed since 2009?
Upvotes: 7
Views: 260
Reputation: 6463
It is not possible to use implicit variable declaration if you name a class var
.
No, because that might break some code written before the new meaning of var
was introduced in the language. All legacy code with var
as class names would not compile anymore. And I read lately that backwards compatibility is very important for the C# designer team.
Upvotes: 2