2

How can i define a List as field of struct?

Something like this :

public struct MyStruct
{
    public decimal SomeDecimalValue;
    public int SomeIntValue;
    public List<string> SomeStringList = new List<string> // <<I Mean this one?
}

And then use that string Like this :

Private void UseMyStruct()
{
     MyStruct S= new MyStruct();
     s.Add("first string");
     s.Add("second string");
}

I've tried a few things but they all return errors and don't work.

5
  • 2
    Why dont you use class instead of struct? Commented Oct 3, 2013 at 11:35
  • 8
    You can always use s.SomeStringList.Add(); Commented Oct 3, 2013 at 11:36
  • ok i can,but i cant define List in Struct.. This is my problem..not using s.Add().. Please read the post carefully. And try this code if you are sure Commented Oct 3, 2013 at 11:39
  • 1
    @sine and @SWeko: How exactly will that work when the string list cannot be initialized? It can't.. it will throw a NullRef. The OP would have to initialize the list after instantiating the struct. Commented Oct 3, 2013 at 11:43
  • You´re absolutely right, overlooked that o_O shame on me... Commented Oct 3, 2013 at 11:43

2 Answers 2

13

You cannot have field initializers in a struct.

The reason is that a field initializer is really compiled into the parameterless constructor, but you cannot have a parameterless constructor in a struct.

The reason you cannot have a parameterless constructor, is that the default construction of a struct is to erase its memory with zero bytes.

However, what you can do is this:

public struct MyStruct
{
    private List<string> someStringList;

    public List<string> SomeStringList
    {
         get
         {
             if (this.someStringList == null)
             {
                 this.someStringList = new List<string>();
             }

             return this.someStringList;
         }
    }
}

Note: this is not threadsafe, though it can be modified to be if needed.

Sign up to request clarification or add additional context in comments.

Comments

1

Public fields in structs are evil, and will stab you in the back when you are not looking!

That said, you can initialize it in the (parameterfull) constructor, like this:

public struct MyStruct
{
    public decimal SomeDecimalValue;
    public int SomeIntValue;
    public List<string> SomeStringList;

    public MyStruct(decimal myDecimal, int myInt)
    {
      SomeDecimalValue = myDecimal;
      SomeIntValue = myInt;
      SomeStringList = new List<string>();
    }

    public void Add(string value)
    {
      if (SomeStringList == null)
        SomeStringList = new List<string>();
      SomeStringList.Add(value);
    }
}

Note that the SomeStringList will still be null if someone uses the default constructor:

MyStruct s = new MyStruct(1, 2);
s.SomeStringList.Add("first string");
s.Add("second string");

MyStruct s1 = new MyStruct(); //SomeStringList is null
//s1.SomeStringList.Add("first string"); //blows up
s1.Add("second string");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.