1

I have several static fields and need to know what is the best container for them: class or 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");
}
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
st_stefanov
  • 1,147
  • 1
  • 10
  • 28
  • 2
    Doesn't matter. Struct/Class changes behavior of instances only. – Atomosk Aug 01 '14 at 11:35
  • Would I have advantage in this case to use Singleton or when all fields are static is not an advantage? Actually static class or Singleton? – st_stefanov Aug 01 '14 at 11:55
  • 2
    Having static classes can really impede testing. Making this class a singleton and giving it an interface that can be mocked would be an advantage when testing. – Chris Mantle Aug 01 '14 at 12:04
  • 1
    Go for singleton, which is injectable – Liviu Mandras Aug 01 '14 at 12:16
  • And one more thing I think is worth adding here: With static fields the syntax is just MySharedFields.Field1, when with the Singleton this syntax is not applicable and you will have to take the instance every time and make call via it. So it is more overhead for the programmer as well. – st_stefanov Aug 01 '14 at 12:31
  • A few extra key strokes isn't even a consideration compared to the benefits. And anyway, if you inject the instance, it's almost exactly the same: `mySharedFields.Field1` (possibly `this.mySharedFields.Field1`, depending on your code style). If you create a static instance field on the class (make sure it's of the interface type), it's just an extra de-reference: `MySharedFields.Instance.Field1`. – Chris Mantle Aug 01 '14 at 13:28

4 Answers4

2

Usually static class is the way to go, but it really doesn't make a difference. The plus is you can add Methods in the future if required.

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");
}
nsgocev
  • 4,390
  • 28
  • 37
1

You should use a static class, as C# doesn't allow static structs:

public static class MySharedFields
{
    public static readonly Field1 = AppConfig.GetValue("Field1");
    public static readonly Field2 = AppConfig.GetValue("Field2");
    public static readonly Field3 = AppConfig.GetValue("Field3");
}
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
1

When you use a class or struct, first time CLR loads a Type class. All objects of a type have a reference to that type avaliable via GetType method. Objects themself contains only instance fields, all methods and static fields are in Type. Instance methods takes implicit extra argument this. When you call instance method, object passes himself to method, which actualy defined in Type. Difference between struct and classes only in where they keep their fields: heap for classes and stack for struct. So there is no real difference between static method of class and struct. But you can mark class as static to prevent creating instances of that class.

Atomosk
  • 1,871
  • 2
  • 22
  • 26
0

The difference between a class and struct in C# is very small.

I would say, use a class, because then you can easily afterwards add methods to it when needed.

Probably structs might have some performance advantages, but if that is not an issue, a class is probably in most of such cases the best way to go.

You can in your case make the class static, unless you have multiple instances or later you want to have multiple instances.

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • 2
    One more question then. When the code calls value of any field will it execute AppConfig.GetValue every time or only the first time? (They are readonly fields) – st_stefanov Aug 01 '14 at 11:40
  • 2
    @st_stefanov it is going to be executed only the first time – nsgocev Aug 01 '14 at 11:41
  • @nsgovec (previous remark was for the first comment; I removed the abstract class remark. – Michel Keijzers Aug 01 '14 at 11:53
  • @nsgovec - is this because the field is read only, or because it is static? – st_stefanov Aug 01 '14 at 12:00
  • 2
    @st_stefanov because it is static. Difference between readonly and const can be found here: http://stackoverflow.com/questions/755685/c-static-readonly-vs-const – nsgocev Aug 01 '14 at 12:02