0

I am having class with public static List which looks like this:

public class MyClass
{
    public static List<Tuple<Ogranicenja, string>> defaultValues = new List<Tuple<Ogranicenja, string>>();
//Here i want to add item
}

I would add new item to list easy if i am creating class with constructor inside it:

public MyClass()
{
    defaultValues.Add(new Tuple<Ogranicenja, string>(Ogranicenja.Dnevno, "TEST");
}

BUT i do not want it like that. I want that list to be just like database but inside code with about 20 rows and so it is accessible to every class so how can i accomplish that.

It is not same as THIS question since i know how to do public static int test = 1 but how to the same thing with list which is typeOf Tuple and need to have multple items.

Aleksa Ristic
  • 2,394
  • 3
  • 23
  • 54
  • Look at edit. Since it it normal to do `public static int test = 1` why isn't something like that possible with list? – Aleksa Ristic Jun 05 '18 at 21:09
  • Yea at the time i create it. – Aleksa Ristic Jun 05 '18 at 21:10
  • It is not normal to have a static mutable state, like `public static int` or `public static List`. Moreover, public fields violate Microsoft design guidelines. In your case it should be `private static readonly IReadOnlyCollection<>` and then a public static property to access it from outside. – Pavel Tupitsyn Jun 05 '18 at 21:19

1 Answers1

2

You can add values to a collection at the time you declare it, like this:

public class MyClass
{
    public static List<Tuple<Ogranicenja, string>> defaultValues
        = new List<Tuple<Ogranicenja, string>>
            {
                Tuple.Create(new Ogranicenja(...), "string_one"),
                Tuple.Create(new Ogranicenja(...), "string_two")
            };
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165