-4

I need to initialize this list and it should be static, but I can't. Any help, please :(

private static List<MyObj> teste;

public static List<MyObj> Teste;
{
    get {return Teste;}
    set
    {
        teste.Add(new MyObj{ Value = 1, Quant = 1 });
        teste.Add(new MyObj{ Value = 2, Quant = 1 });
        teste.Add(new MyObj{ Value = 99, Quant = 1 });
        teste.Add(new MyObj{ Value = 33, Quant = 1 });
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

2

Before using teste you need to instantiate a list of myobj:

private static List<MyObj> teste = new List<MyObj>();

And the get of your property can't return itself, it will cause an infinite loop.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • I have already coded it before Bruno, and it didn't work. I use the code from GSerg and it worked. But I didn't understand why. Thank you anyway. – Bonnie Saur Apr 11 '16 at 22:29