Is it possible to force C# compiler to treat var
as a keyword and not as a class identifier when a class of name var
is declared?
public class var
{
}
public class A
{
}
public class Program
{
public static void Main()
{
var a=new A{}; // Cannot implicitly convert type 'A' to 'var'
}
}
https://dotnetfiddle.net/O1wVoO
In your code, you can no longer use var
as a keyword within the context.
var
is a contextual keyword (there are two types of keywords: normal and contextual), where it behaves like a keyword within its respective contexts where it is used as a type name, but can still be used as an identifier (not reserved).
You declared it as class name therefore it loses its special meaning within the context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With