Skip to main content
 首页 » 编程设计

c#之最好只对内部的静态字段使用类或结构

2024年02月06日12unruledboy

我有几个静态字段,需要知道什么是最适合它们的容器:class 还是 struct?

public class/struct MySharedFields 
{ 
    public static readonly string Field1 = AppConfig.GetValue("Field1"); 
    public static readonly string Field2 = AppConfig.GetValue("Field2"); 
    public static readonly string Field3 = AppConfig.GetValue("Field3"); 
} 

请您参考如下方法:

通常静态类是要走的路,但它真的没有什么区别。加号是您可以在将来根据需要添加方法。

public static class MySharedFields 
{ 
    public static readonly string Field1 = AppConfig.GetValue("Field1"); 
    public static readonly string Field2 = AppConfig.GetValue("Field2"); 
    public static readonly string Field3 = AppConfig.GetValue("Field3"); 
}