[英]How to declare class variables as constants
I am new to writing unit test files in.Net Core.我是在.Net Core 中编写单元测试文件的新手。 I have a constant file where I need to create a list of objects for a class.
我有一个常量文件,我需要在其中为 class 创建对象列表。
Below code is working.下面的代码正在工作。
public class user
{
public const Int ID = "1";
public const String Name = "FakeUserId1";
public readonly List<access> p = new List<access>(){
new access {
edit = true,
edit = true,
siteid = 123
},
new access {
edit = true,
edit = false,
siteid = 234
}
};
}
public class access
{
public Boolean edit;
public Boolean delete;
public int siteid;
}
Is it possible to declare access class variables as constants and initialize them in the list?是否可以将访问 class 变量声明为常量并在列表中初始化它们?
Something like this像这样的东西
public class access
{
public const Boolean edit;
public const Boolean delete;
public const int siteid;
}
Well, you could mark the fields as readonly
, and supply a constructor:好吧,您可以将字段标记为
readonly
,并提供一个构造函数:
public class access
{
public readonly Boolean edit;
public readonly Boolean delete;
public readonly int siteid;
public access(Boolean e, Boolean d, int s)
{
edit = e;
delete = d;
sideid = s;
}
}
And then in your user
class:然后在您的
user
class 中:
public readonly List<access> p = new List<access>(){
new access(true, true, 123),
new access(true, false, 234)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.