Aleksa Ristic
Aleksa Ristic

Reputation: 2499

Define members of static List outside of method

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.

Upvotes: 0

Views: 48

Answers (1)

Grant Winney
Grant Winney

Reputation: 66439

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")
            };
}

Upvotes: 2

Related Questions