nyconing
nyconing

Reputation: 1136

C# IS possible to name a method in class named new?

example:

class document
{
    public void new()
    {
    }
    public void save()
    {
    }

}

visual studio claim that new is red-underlined with error. I need that all is lower-cased to arrange cleanliness

For those seen this Questions:* **(Answer)C# Naming rule violation: Words must be begin with Upper Cases!!!

Upvotes: 2

Views: 230

Answers (2)

Tommy
Tommy

Reputation: 3124

NO, you can't since new is a keywoard. But, YES, you can escape keywords by @ if accepted.

class document
{
    public void test()
    {
        this.@new();
    }

    public void @new()
    {
        Console.WriteLine();
    }
}

C# suggests using upper camel case in class name and method name.

Since keywords are in lower cases, upper camel case names, like void New(), are unaffected.

Upvotes: 5

utxin
utxin

Reputation: 64

No you cannot use keywords as identifiers. See article https://msdn.microsoft.com/en-us/library/x53a06bb.aspx.

Upvotes: 1

Related Questions