C# 将静态成员分配给常量

C# 将静态成员分配给常量,c#,C#,假设您有一个具有共享成员的类来引用这样的已知值 public class Semaphore() { public static int Red = 0; public static int Yellow = 1; public static int Green = 2; } 在VB.Net中,您可以将这些静态(共享)值分配给常量,如下所示: Public Class Transit Private Const Red as Integer = Semaphor

假设您有一个具有共享成员的类来引用这样的已知值

public class Semaphore()
{
    public static int Red = 0;
    public static int Yellow = 1;
    public static int Green = 2;
}
在VB.Net中,您可以将这些静态(共享)值分配给常量,如下所示:

Public Class Transit
    Private Const Red as Integer = Semaphore.Red
End Class
但在C#中,这是不可能的。这是有原因的吗?一些变通办法

更新


事实上,VB.NET的行为方式与C#相同。至少在VS 2012年。唯一的区别是,在C#中,共享成员甚至在intellisense中都不可用。

可以使用
只读
(),而不是使用
常量

readonly关键字与const关键字不同。常量字段可以 只能在声明字段时初始化。只读字段 可以在声明或构造函数中初始化。 因此,只读字段可以具有不同的值,具体取决于 使用的构造函数。另外,虽然const字段是编译时 常量,只读字段可用于运行时常量,如中所示 此行:public static readonly uint l1=(uint)DateTime.Now.Ticks

C#解决方案示例:

public class Semaphore
{
    public static int Red = 0;
    public static int Yellow = 1;
    public static int Green = 2;
}

class Transit
{
    readonly int Red = Semaphore.Red;
}

您可以使用
readonly
(),而不是使用
const

readonly关键字与const关键字不同。常量字段可以 只能在声明字段时初始化。只读字段 可以在声明或构造函数中初始化。 因此,只读字段可以具有不同的值,具体取决于 使用的构造函数。另外,虽然const字段是编译时 常量,只读字段可用于运行时常量,如中所示 此行:public static readonly uint l1=(uint)DateTime.Now.Ticks

C#解决方案示例:

public class Semaphore
{
    public static int Red = 0;
    public static int Yellow = 1;
    public static int Green = 2;
}

class Transit
{
    readonly int Red = Semaphore.Red;
}

您的意图是可以更改信号灯.Red

Semaphore.Red = 5;
如果不是,请在此处使用常量:

public class Semaphore
{
    public const int Red = 0;
    public const int Yellow = 1;
    public const int Green = 2;
}

您的意图是可以更改信号灯.Red

Semaphore.Red = 5;
如果不是,请在此处使用常量:

public class Semaphore
{
    public const int Red = 0;
    public const int Yellow = 1;
    public const int Green = 2;
}
事实上,VB.NET的行为方式与C#相同。至少在VS 2012年。唯一的区别是,在C#中,共享成员在intellisense中甚至不可用

这是因为在VB中,可以调用所有
Public
Friend
模块
成员,而无需指定
模块
名称:

Imports TestNamespace

Public Class TestClass

    Public Sub New()
        Dim test = TestPropertyFromModule
    End Sub

End Class
考虑以下
模块

Namespace TestNamespace

    Module TestModule

        Public ReadOnly Property TestPropertyFromModule As String
            Get
                Return "My Test Property Value"
            End Get
        End Property

    End Module

End Namespace
只要文件顶部有
Imports TestNamespace
,就可以调用
TestPropertyFromModule
,而无需指定
TestModule
名称:

Imports TestNamespace

Public Class TestClass

    Public Sub New()
        Dim test = TestPropertyFromModule
    End Sub

End Class
在C#中,这是不可能的。您必须使用
命名并编写
TestModule.TestPropertyFromModule
。这就是为什么VB.NET上的intellisense显示了与C#不同的可用方法/属性/etc集

事实上,VB.NET的行为方式与C#相同。至少在VS 2012年。唯一的区别是,在C#中,共享成员在intellisense中甚至不可用

这是因为在VB中,可以调用所有
Public
Friend
模块
成员,而无需指定
模块
名称:

Imports TestNamespace

Public Class TestClass

    Public Sub New()
        Dim test = TestPropertyFromModule
    End Sub

End Class
考虑以下
模块

Namespace TestNamespace

    Module TestModule

        Public ReadOnly Property TestPropertyFromModule As String
            Get
                Return "My Test Property Value"
            End Get
        End Property

    End Module

End Namespace
只要文件顶部有
Imports TestNamespace
,就可以调用
TestPropertyFromModule
,而无需指定
TestModule
名称:

Imports TestNamespace

Public Class TestClass

    Public Sub New()
        Dim test = TestPropertyFromModule
    End Sub

End Class

在C#中,这是不可能的。您必须使用
命名并编写
TestModule.TestPropertyFromModule
。这就是为什么VB.NET上的intellisense显示了与C#不同的可用方法/属性集等。

不相信它在VB中是可能的…为什么不使用
枚举
。。。?它将更加类型安全,甚至可以将其用作常量。我不相信它在VB中是可行的…为什么不使用
enum
。。。?它会更安全,你甚至可以把它当作一个常量。