Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force usage of “var” to be parsed as keyword rather than class name

Tags:

c#

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

like image 345
Franta Avatar asked Jun 04 '20 17:06

Franta


1 Answers

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.

like image 125
Fernand Avatar answered Oct 22 '22 17:10

Fernand