简体   繁体   English

C#是否可以在名为new的类中命名方法?

[英]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. Visual Studio声称new带有红色下划线并带有错误。 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!!! 对于那些看到此问题的人:* **(答案)C#违反命名规则:单词必须以大写字母开头!!!

NO, you can't since new is a keywoard. 不,您不能,因为new是关键因素。 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. C#建议在类名和方法名中使用大写字母大写。

Since keywords are in lower cases, upper camel case names, like void New() , are unaffected. 由于关键字是小写字母,因此不影响大驼峰名称,例如void New()

No you cannot use keywords as identifiers. 不,您不能使用关键字作为标识符。 See article https://msdn.microsoft.com/en-us/library/x53a06bb.aspx . 请参阅文章https://msdn.microsoft.com/en-us/library/x53a06bb.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM