What does "a field initializer cannot reference non static fields" mean in C#?

The links below may shed some light on why doing what you are trying to do may not be such a good thing, in particular the second link:

Why Do Initializers Run In The Opposite Order As Constructors? Part One

Why Do Initializers Run In The Opposite Order As Constructors? Part Two


Any object initializer used outside a constructor has to refer to static members, as the instance hasn't been constructed until the constructor is run, and direct variable initialization conceptually happens before any constructor is run. getUserName is an instance method, but the containing instance isn't available.

To fix it, try putting the usernameDict initializer inside a constructor.

Tags:

C#

Delegates