C#:为什么设计团队决定不在标识符中使用允许空白的语法?

C#:为什么设计团队决定不在标识符中使用允许空白的语法?,c#,asp.net,.net,C#,Asp.net,.net,在Visual Basic中,可以使用以下语法: Dim [My variable with spaces] [My variable with spaces] = 9000 在SQL Server中:(我意识到这与C无关,但只是用作语法示例) 我的问题是,为什么这种语法从来没有被纳入C# 我知道人们可能会认为这是愚蠢的,很少使用,在标识符中使用空格是不好的做法,等等。如果它在Visual Basic和SQL Server中有支持,那么它一定不会有那么坏的做法。 在C#中,它将立即解决友好枚举

在Visual Basic中,可以使用以下语法:

Dim [My variable with spaces]
[My variable with spaces] = 9000
在SQL Server中:(我意识到这与C无关,但只是用作语法示例)

我的问题是,为什么这种语法从来没有被纳入C#

我知道人们可能会认为这是愚蠢的,很少使用,在标识符中使用空格是不好的做法,等等。如果它在Visual Basic和SQL Server中有支持,那么它一定不会有那么坏的做法。 在C#中,它将立即解决友好枚举的问题。现在,大多数解决方案都涉及使用DescriptionAttribute:

public enum UnitedStates
{
    [Description("Alabama")]
    Alabama,
    //...
    [Description("New Hampshire")]
    NH,
    //...
    [Description("Wyoming")]
    Wyoming
}
public static class Extensions
{
    public static string GetDescription(this Enum self)
    {
        //tedious, inefficient reflection stuff...
    }
}
这不仅需要更多的代码,而且在数据绑定中使用ToString()的ASP.NET数据绑定方法也会失败。例如:

public class Person
{
    public string Name { get; set; }
    public UnitedStates FavoriteState { get; set; }
}
在ASPX中:

<asp:GridView ID="myGridView" runat="server">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="FavoriteState" HeaderText="Favorite State" /><%--NO!--%>
    </Columns>
</asp:GridView>
据我所见,MSIL中定义的程序集支持字段、属性、变量等,名称中带有空格。例如,使用System.Reflection.Emit创建部件时:

AssemblyName assemblyName = new AssemblyName("TestModule");
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("TestModule", "TestModule.dll");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public | TypeAttributes.BeforeFieldInit);
typeBuilder.DefineField("My field with spaces", typeof(int), FieldAttributes.Public);
EnumBuilder usEnum = moduleBuilder.DefineEnum("UnitedStates", TypeAttributes.Public, typeof(int));
usEnum.DefineLiteral("Alabama", 0);
usEnum.DefineLiteral("New Hampshire", 1);
usEnum.CreateType();
typeBuilder.CreateType();
assemblyBuilder.Save("TestModule.dll");
这样做可以创建工作部件。因此,在我看来,在语法中添加支持将是相对无缝的。我发现的另一个问题是VisualStudioIntelliSense不显示包含空格的公共成员


虽然这看起来更像是一个愿望清单项目而不是一个问题,但我只是想知道为什么设计师决定支持Visual Basic而不是C。

简单回答:因为设计C的人决定了这样做。你应该阅读Eric Lippert的文章。谢谢,这很有帮助。好问题,这里有一些有趣的信息:简短回答:历史原因。这个特性至少从1993年开始在VB中出现,那是我第一次遇到它的时候。由于很少使用,因此没有太多动机将其添加到C#中。我注意到C#确实允许(1)在标识符中使用Unicode;你想说
constdoubleπ=3.14
您可以直接进行,(2)关键字作为标识符,例如
@interface@for=@class+@event
是合法的,但很奇怪。枚举是我能想到的唯一一个标识符有助于实现实际功能的例子,而不仅仅是元数据。能够定义方法,例如在枚举中重写ToString是更好的解决方案,但我意识到这是另一种蠕虫;)
public enum UnitedStates
{
    Alabama,
    //...
    [New Hampshire],
    //...
    Wyoming
}
var myFavoriteState = UnitedStates.[New Hampshire];
string str = myFavoriteState.ToString(); // YES!
AssemblyName assemblyName = new AssemblyName("TestModule");
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("TestModule", "TestModule.dll");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public | TypeAttributes.BeforeFieldInit);
typeBuilder.DefineField("My field with spaces", typeof(int), FieldAttributes.Public);
EnumBuilder usEnum = moduleBuilder.DefineEnum("UnitedStates", TypeAttributes.Public, typeof(int));
usEnum.DefineLiteral("Alabama", 0);
usEnum.DefineLiteral("New Hampshire", 1);
usEnum.CreateType();
typeBuilder.CreateType();
assemblyBuilder.Save("TestModule.dll");