“字段初始值设定项不能引用非静态字段”在C#中意味着什么?

时间:2021-03-01 22:49:43

I don't understand this error in C#

我不明白C#中的这个错误

error CS0236: A field initializer cannot reference the non-static field, method, or property 'Prv.DB.getUserName(long)'

错误CS0236:字段初始值设定项无法引用非静态字段,方法或属性'Prv.DB.getUserName(long)'

For the following code

对于以下代码

public class MyDictionary<K, V>
{
    public delegate V NonExistentKey(K k);
    NonExistentKey nonExistentKey;

    public MyDictionary(NonExistentKey nonExistentKey_) { }
}

class DB
{
    SQLiteConnection connection;
    SQLiteCommand command;

    MyDictionary<long, string> usernameDict = new MyDictionary<long, string>(getUserName);

    string getUserName(long userId) { }
}

4 个解决方案

#1


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.

在构造函数外部使用的任何对象初始值设定项都必须引用静态成员,因为在构造函数运行之前尚未构造实例,并且直接变量初始化在概念上发生在任何构造函数运行之前。 getUserName是一个实例方法,但包含的实例不可用。

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

要修复它,请尝试将usernameDict初始化程序放在构造函数中。

#2


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

为什么初始化器作为构造函数以相反的顺序运行?第二部分

#3


getUserName is an instance method.
Change it to static, that might work.

getUserName是一个实例方法。将其更改为静态,这可能有效。

OR

Initialize the dictionary in the constructor.

在构造函数中初始化字典。

#4


You cannot do this because the instance has to be initialized before you can access the properties of its class. The field initializers are called before the class is initialized.

您无法执行此操作,因为必须先初始化实例,然后才能访问其类的属性。在初始化类之前调用​​字段初始值设定项。

If you want to initialize the field usernameDict with the return-value of the GetUserName-Method you have to do it within the constructor or make the Method a static one.

如果要使用GetUserName-Method的返回值初始化字段usernameDict,则必须在构造函数中执行此操作,或使Method成为静态方法。

#1


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.

在构造函数外部使用的任何对象初始值设定项都必须引用静态成员,因为在构造函数运行之前尚未构造实例,并且直接变量初始化在概念上发生在任何构造函数运行之前。 getUserName是一个实例方法,但包含的实例不可用。

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

要修复它,请尝试将usernameDict初始化程序放在构造函数中。

#2


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

为什么初始化器作为构造函数以相反的顺序运行?第二部分

#3


getUserName is an instance method.
Change it to static, that might work.

getUserName是一个实例方法。将其更改为静态,这可能有效。

OR

Initialize the dictionary in the constructor.

在构造函数中初始化字典。

#4


You cannot do this because the instance has to be initialized before you can access the properties of its class. The field initializers are called before the class is initialized.

您无法执行此操作,因为必须先初始化实例,然后才能访问其类的属性。在初始化类之前调用​​字段初始值设定项。

If you want to initialize the field usernameDict with the return-value of the GetUserName-Method you have to do it within the constructor or make the Method a static one.

如果要使用GetUserName-Method的返回值初始化字段usernameDict,则必须在构造函数中执行此操作,或使Method成为静态方法。