Oliver Apel
Oliver Apel

Reputation: 1878

Initialize list with default values

I want to have a class, that stores all "allowed languages" in a list. Code party should be able to modify the list. But on first usage, the list should be "initialized" with some default values.

I have the following class:

public class ApiLanguages
{
    public static List<string> AllowedLanguages { get; set; }

    public ApiLanguages()
    {
        AllowedLanguages.Add("de");
        //AllowedLanguages.Add("en");
        //AllowedLanguages.Add("es");
        //AllowedLanguages.Add("fr");
        //AllowedLanguages.Add("it");
    }
}

When I access the class now in code with

foreach (var language in ApiLanguages.AllowedLanguages)
{
      // do something here...
}

the ApiLanguages.AllowedLanguages is null. But I expect one entry ("de"). What I am doing wrong here?

Upvotes: 0

Views: 3505

Answers (3)

Vadim Martynov
Vadim Martynov

Reputation: 8902

You should initialize AllowedLanguages with new instance of List<string> first. You can do it with initializers for auto-properties in c# 6.0 or in the static constructor for older versions of c#.

public class ApiLanguages
{
    // c# 6.0 syntax
    public static List<string> AllowedLanguages { get; set; } = new List<string>();

    static ApiLanguages()
    {
        // c# < 6.0 (old style) syntax
        AllowedLanguages = new List<string>();
    }

    public ApiLanguages()
    {
        AllowedLanguages.Add("de");
    }
}

Also I'm sure that you no need to add new values to the list for each instance of ApiLanguages class then you should move AllowedLanguages.Add(...) to the static constructor too. And you can join object creation and initialization to a single line of code:

public static List<string> AllowedLanguages { get; set; } = new List<string>() { "de", "en", "ru" };

Upvotes: 3

fhnaseer
fhnaseer

Reputation: 7277

You can make your constructor static as well, but I prefer lazy loading. By this you will not populate list again and again whenever object is created of ApiLanguages,

public class ApiLanguages
{
    private static IEnumerable<string> _allowedLanguages;
    public static IEnumerable<string> AllowedLanguages
    {
       get
       {
           return _allowedLangues ?? (_allowedLangues = new List<string>{ "EN", "AR"});
       }
    }    
}

Upvotes: 3

Yacoub Massad
Yacoub Massad

Reputation: 27871

public ApiLanguages() is an instance constructor. It runs only (and every time) when you create a new instance of ApiLanguages (via new ApiLanguages()). It's purpose is to initialize instance variables, not static ones. You usually shouldn't initialize static properties or fields in an instance constructor.

You need to use the static constructor to initialize the static list like this:

public class ApiLanguages
{
    public static List<string> AllowedLanguages { get; set; }

    static ApiLanguages()
    {
        AllowedLanguages = new List<string>();
        AllowedLanguages.Add("de");
        //...
    }
}

Upvotes: 5

Related Questions