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

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.

Leave a Comment