gpt4 book ai didi

C# 静态字典在抽象类 .NET 2.0 中声明和初始化

转载 作者:行者123 更新时间:2023-11-30 20:07:57 36 4
gpt4 key购买 nike

我有一个抽象类,想为错误代码添加一个静态字典。我尝试了以下方法:

public abstract class Base
{
...
protected static readonly Dictionary<int, string> errorDescriptions = new Dictionary<int, string>()
{
{ 1, "Description1"},
{ 2, "Description2"},
...
};
...
}

但后来发现这是在.NET 3.0中实现的;我正在使用 2.0.我环顾四周,其他一些人建议我在构造函数中添加对,但这是一个抽象类。

我可以/应该如何填充字典?

谢谢。

最佳答案

public abstract class Base
{
...
protected static readonly Dictionary<int, string> errorDescriptions;
// Type constructor called when Type is first accessed.
// This is called before any Static members are called or instances are constructed.
static Base ()
{
errorDescriptions = new Dictionary<int, string>();
errorDescriptions[1] = "Description1";
errorDescriptions[2] = "Description2";
}
}

关于C# 静态字典在抽象类 .NET 2.0 中声明和初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7742459/

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