gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:42 24 4
gpt4 key购买 nike

我有一个如下所示的私有(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 种良好做法之间进行选择:
- 使用构造器
- 使用访问器(属性)

关于c# - 私有(private)类中的公共(public)字段 : "Field [FieldName] is never assigned to, and will always have its default value null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12397243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com