Skip to main content
 首页 » 编程设计

c#之私有(private)类中的公共(public)字段 : "Field [FieldName] is never assigned to, and will always have its default value null"

2024年02月20日12lidabo

我有一个如下所示的私有(private)类(class):

private class PocoUserWithResourceId 
{ 
    public USER User; 
    public string ResourceId; 
} 

我用这个类做事(在同一个封装公共(public)类中),比如:

var uQuery = from u in db.USERS.All() 
select new PocoUserWithResourceId {User = u, ResourceId = arv.Resource_Id}; 

我收到警告:

Field 'PocoUserWithResourceId.ResourceId' is never assigned to, and will always have its default value null

我错过了什么?我在我的 LINQ to SQL 代码中分配值。

请您参考如下方法:

Is it just because I'm assigning property values during the declaration that the compiler doesn't see the fact that I'm using them?

不,那只是因为您要分配 field值,而不是 property值(value)观……

您可以在 2 种良好做法之间进行选择:
- 使用构造器
- 使用访问器(属性)