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

标签 c#

例子:

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

}

visual studio 声称新的是带有错误的红色下划线。 我需要全部小写以安排清洁

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

最佳答案

不,你不能,因为 new 是一个关键字。但是,是的,如果被接受,您可以通过 @ 转义关键字。

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

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

C#建议在类名和方法名中使用大驼峰式。

由于关键字是小写字母,因此大写驼峰名称(如 void New())不受影响。

关于C# 是否可以在名为 new 的类中命名一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38044982/

相关文章:

c# - Visual Studio 2008 无法调试

C# 和 .NET : How to serialize a structure into a byte[] array, 使用 BinaryWriter?

c# - 在 .NET 中乘以 TimeSpan

具有接口(interface)混淆的 C# 通用 ClientBase

c# - 以 mdi 形式将 .exe 作为窗口打开?

c# - 为什么我在 C# 中调试时得到一个空白表单?

c# - 空工具提示问题

c# - 更新消息后弹出收据已更改

javascript - AntiForgeryToken 无法跨同一解决方案的子项目进行验证

c# - 使用正则表达式获取标签内的文本