Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a static readonly variable using an anonymous method?

I'm trying to clean up my code for initializing static readonly variables.

Original:

public static readonly List<int> MyList;

//Initialize MyList in the static constructor
static MyObject() { ... }


I decided to clean it up because CodeAnalysis said I should not use the static constructor (CA1810).

Cleanup:

public static readonly List<int> MyList = GetMyList();

//Returns the list
private static List<int> GetMyList() { ... }


I didn't really like the additional method, so I figured I'd try to get it to be all inline, but it won't work. I'm not sure what I'm doing wrong here...
public static readonly List<int> MyList =
    () =>
       {
           ...

           return list;
       };

I attempted to take the code within the GetMyList() method and place it in an anonymous delegate to return the list to assign, but it says I'm attempting to convert a delegate into a List<int>?

like image 339
michael Avatar asked Nov 26 '12 17:11

michael


People also ask

Can readonly be static in C#?

A readonly field can be initialized either at the time of declaration or within the constructor of the same class. Therefore, readonly fields can be used for run-time constants. Explicitly, you can specify a readonly field as static since like constant by default it is not static.

Are readonly variables static?

The keyword readonly has a value that may be changed or assigned at runtime, but only through the non-static constructor. There isn't even a method or static method.

What is private static readonly in C#?

A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime.

What is difference between static constant and readonly variables?

Difference between const and readonlyconst variables can declared in methods ,while readonly fields cannot be declared in methods. const fields cannot be used with static modifier, while readonly fields can be used with static modifier.


2 Answers

This looks a little weird, but try this:

public static readonly List<int> MyList = new Func<List<int>>(
() =>
{
    // Create your list here
    return new List<int>();
})();

The trick is creating a new Func<List<int>> and invoking it.

like image 189
Jon B Avatar answered Sep 20 '22 04:09

Jon B


The reason is that when you write

() =>
   {
       ...

       return list;
   };

You're actually declaring a delegate - a function that returns List<int>, but you're not actually invoking that function, and therefore this evaluates to Func<List<int>> and not to List<int>.

I'm not quite sure why you're trying to remove your static constructor though. Static constructors have their uses, and in some cases even make the code cleaner and more readable.

like image 25
Adi Lester Avatar answered Sep 23 '22 04:09

Adi Lester