如何将类变量声明为常量

时间:2019-10-09 19:37:19

标签: c# unit-testing .net-core

我是第一次在.Net Core中编写单元测试文件。我有一个常量文件,需要在其中创建类的对象列表。

以下代码有效。

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;
}

是否可以将访问类变量声明为常量并在列表中对其进行初始化?

类似这样的东西

public class access
{
  public const Boolean edit;
  public const  Boolean delete;
  public const int siteid;
}

1 个答案:

答案 0 :(得分:1)

好吧,您可以将字段标记为WITH NEIGHBORHOOD(NEIGHBORHOOD_NAME) AS ( VALUES ('Shady Acres') ) , HOUSEHOLD (NEIGHBORHOOD_NAME, HOUSEHOLD_NAME, HOUSEHOLD_INCOME) AS ( VALUES ('Shady Acres', '123 Shady Lane', 25000) , ('Shady Acres', '126 Shady Lane', 15000) ) , HOUSEHOLD_MEMBER (HOUSEHOLD_NAME, PERSON_NAME) AS ( VALUES ('123 Shady Lane', 'Jane' ) ,('123 Shady Lane', 'Mary' ) ,('123 Shady Lane', 'Robert') ,('126 Shady Lane', 'George') ,('126 Shady Lane', 'Tom' ) ,('126 Shady Lane', 'Betsy' ) ,('126 Shady Lane', 'Timmy' ) ) , TMP AS ( SELECT N.NEIGHBORHOOD_NAME , CASE WHEN H.HOUSEHOLD_NAME = LAG(H.HOUSEHOLD_NAME) OVER (PARTITION BY N.NEIGHBORHOOD_NAME ORDER BY H.HOUSEHOLD_NAME) THEN 0 ELSE 1 END AS HOUSEHOLD_NAME_CHANGED , H.HOUSEHOLD_INCOME FROM NEIGHBORHOOD N JOIN HOUSEHOLD H ON H.NEIGHBORHOOD_NAME = N.NEIGHBORHOOD_NAME JOIN HOUSEHOLD_MEMBER M ON M.HOUSEHOLD_NAME = H.HOUSEHOLD_NAME ) SELECT NEIGHBORHOOD_NAME , SUM(HOUSEHOLD_NAME_CHANGED) AS HOUSEHOLD_NAMES , SUM(HOUSEHOLD_NAME_CHANGED * HOUSEHOLD_INCOME) AS HOUSEHOLD_INCOME , COUNT(1) AS MEMBERS FROM TMP GROUP BY NEIGHBORHOOD_NAME; ,并提供一个构造函数:

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; } } 类中:

user