字段初始值设定项不能引用非静态字段,方法或属性?

时间:2022-11-10 22:48:23

I have a Repository Class and a Services Class as below :

我有一个Repository类和一个Services Class,如下所示:

public class DinnerRepository
{
    DinnerDataContext db = new DinnerDataContext();

    public Dinner GetDinner(int id)
    {
        return db.Dinners.SingleOrDefault(d => d.DinnerID == id);   
    }

// Others Code        
}



public class Service
{
        DinnerRepository repo = new DinnerRepository(); 
        Dinner dinner = repo.GetDinner(5);

// Other Code
}

This throws error:

抛出错误:

A field initializer cannot reference the non-static field, method, or property.

字段初始值设定项无法引用非静态字段,方法或属性。

Even though I have intatiated the DinnerRepository Class to expose its method GetDinner() in the Service Class. This works fine with below code. Is there any alternative to it or is it a standard practice? I cannot use static methods here..

即使我已经使用DinnerRepository类,也要在Service Class中公开它的方法GetDinner()。这适用于下面的代码。有没有其他选择,还是标准做法?我不能在这里使用静态方法..

public class Service
{

    public Service()
    {
        DinnerRepository repo = new DinnerRepository(); 
        Dinner dinner = repo.GetDinner(5);
    }

}

2 个解决方案

#1


18  

Personally I'd just initialize the fields in a constructor:

就个人而言,我只是在构造函数中初始化字段:

public class Service
{
    private readonly DinnerRepository repo;
    private readonly Dinner dinner;

    public Service()
    {
        repo = new DinnerRepository();
        dinner = repo.GetDinner(5);
    }
}

Note that this isn't the same as the code you show at the bottom of the question, as that's only declaring local variables. If you only want local variables, that's fine - but if you need instance variables, then use code as above.

请注意,这与您在问题底部显示的代码不同,因为它只声明局部变量。如果你只想要局部变量,那很好 - 但如果你需要实例变量,那么使用上面的代码。

Basically, field initializers are limited in what they can do. From section 10.5.5.2 of the C# 4 spec:

基本上,现场初始化器的功能有限。从C#4规范的10.5.5.2节:

A variable initializer for an instance field cannot reference the instance being created. Thus it is a compile-time error to reference this in a variable initializer, because it is a compile-time error for a variable initializer to reference any instance member through a simple-name.

实例字段的变量初始值设定项无法引用正在创建的实例。因此,在变量初始化程序中引用它是一个编译时错误,因为变量初始化程序通过简单名称引用任何实例成员是编译时错误。

(That "thus" and "therefore" looks the wrong way round to me - it's illegal to reference a member via a simple-name because it references this - I'll ping Mads about it - but that's basically the relevant section.)

(“因此”和“因此”对我来说是错误的方式 - 通过一个简单的名称引用一个成员是非法的,因为它引用了这个 - 我会ping Mads关于它 - 但这基本上是相关部分。)

#2


2  

Even if the initializaton expressions are guaranteed to be in the "textual order", it is illegal for an instance fields initializers to access the this reference, and you are implicitly using it in

即使初始化表达式保证是“文本顺序”,实例字段初始化程序访问此引用也是非法的,并且您隐式在

Dinner dinner = repo.GetDinner(5); 

which is equivalent to

这相当于

Dinner dinner = this.repo.GetDinner(5);

The best practice IMHO, is to reserve field initializations to constant values or to a simple new statement. Anything hairier than that should go to a constructor.

IMHO的最佳实践是将字段初始化保留为常量值或简单的新语句。任何比这更毛茸茸的东西应该去构造函数。

#1


18  

Personally I'd just initialize the fields in a constructor:

就个人而言,我只是在构造函数中初始化字段:

public class Service
{
    private readonly DinnerRepository repo;
    private readonly Dinner dinner;

    public Service()
    {
        repo = new DinnerRepository();
        dinner = repo.GetDinner(5);
    }
}

Note that this isn't the same as the code you show at the bottom of the question, as that's only declaring local variables. If you only want local variables, that's fine - but if you need instance variables, then use code as above.

请注意,这与您在问题底部显示的代码不同,因为它只声明局部变量。如果你只想要局部变量,那很好 - 但如果你需要实例变量,那么使用上面的代码。

Basically, field initializers are limited in what they can do. From section 10.5.5.2 of the C# 4 spec:

基本上,现场初始化器的功能有限。从C#4规范的10.5.5.2节:

A variable initializer for an instance field cannot reference the instance being created. Thus it is a compile-time error to reference this in a variable initializer, because it is a compile-time error for a variable initializer to reference any instance member through a simple-name.

实例字段的变量初始值设定项无法引用正在创建的实例。因此,在变量初始化程序中引用它是一个编译时错误,因为变量初始化程序通过简单名称引用任何实例成员是编译时错误。

(That "thus" and "therefore" looks the wrong way round to me - it's illegal to reference a member via a simple-name because it references this - I'll ping Mads about it - but that's basically the relevant section.)

(“因此”和“因此”对我来说是错误的方式 - 通过一个简单的名称引用一个成员是非法的,因为它引用了这个 - 我会ping Mads关于它 - 但这基本上是相关部分。)

#2


2  

Even if the initializaton expressions are guaranteed to be in the "textual order", it is illegal for an instance fields initializers to access the this reference, and you are implicitly using it in

即使初始化表达式保证是“文本顺序”,实例字段初始化程序访问此引用也是非法的,并且您隐式在

Dinner dinner = repo.GetDinner(5); 

which is equivalent to

这相当于

Dinner dinner = this.repo.GetDinner(5);

The best practice IMHO, is to reserve field initializations to constant values or to a simple new statement. Anything hairier than that should go to a constructor.

IMHO的最佳实践是将字段初始化保留为常量值或简单的新语句。任何比这更毛茸茸的东西应该去构造函数。