Constant variables in structures.

First time dude here. Something is really confusing me about structures.

Right now, I'm trying to create a library of structures with basically only constant variables and properties. An example of one of my structs:

internal struct Struct1 : IHero { private const string name; private const int baseHealth; private const int baseMana; public string Name { get { return name; } } public int BaseHealth { get { return baseHealth; } } public int BaseMana { get { return baseMana; } } } 

I then put them all within a Dictionary in another class. Something like this:

public static class CharacterBaseLibrary { private static Dictionary<string, IHero> heroList = new Dictionary<string, IHero> (); private static Struct1 st1; private static Struct2 st2; private static Struct3 st3; static CharacterBaseLibrary () { heroList.Add (Struct1.name, st1); heroList.Add (Struct2.name, st2); heroList.Add (Struct3.name, st3); } } //IHero is an interface that all of the Structs implement 

However, when I build the program, I get this error:

Field 'CharacterBaseLibrary.st1' is never assigned to, and will always have its default value. 

This is for all of the structs in the Dictionary.

Now I know that a structure without a constructor will simply be the default value, however, when I read up on how constant variables behave, it looked like it "engraved" the values into the compiler, unlike readonly variables, thus making me believe that the values will actually persist due to having the 'const' keyword.

This may be something really simple/me being dumb. Sorry, I'm just really confused about this.

by TheSnowballofCobalt via /r/csharp

Leave a Reply