实例化:将列表移动到参数中
Instantiation: Moving a List into the Arguments
我是 C# 的新手,一直在努力寻找一种惯用的方法来在构造函数中初始化列表。
没有完全解决问题的相关问题:
- How to initialize a list with constructor?
- Initializing lists in a constructor.
这有效,但有一个缺陷:
class Datapoint
{
public bool Debug { get; set; }
public string Pattern { get; private set; }
// I would prefer to initialize this list in the constructor
public List<Dictionary<string, dynamic>> operations =
new List<Dictionary<string, dynamic>>();
// constructor
public Datapoint(bool debug = false,
string pattern = ""
// I would prefer operations to go here
// but the following doesn't work:
// List<Dictionary<string, dynamic>> operations =
// new List<Dictionary<string, dynamic>>()
)
{
Debug = debug;
Pattern = pattern;
}
}
// Let's define some Datapoints
class Definitions
{
public static Datapoint turtles = new Datapoint
(
pattern: @"turtle pattern",
// I would prefer operations to go here
)
{
operations =
{ new Dictionary<string, dynamic>
{
["func"] = "stitch_lines"
}
}
};
}
缺陷是我不能将操作设置为私有,否则在创建海龟时会出错。
理想情况下,我希望 operations
成为构造函数的参数,但我遗漏了一些东西,因为我尝试的每个组合都会产生此错误:
Default parameter value for operations must be a compile-time
constant.
提前感谢您的任何见解。
如错误所示,默认值必须是编译时常量。我会将其作为两个重载来执行:
// constructor
public Datapoint(bool debug = false,
string pattern = "")
{
Debug = debug;
Pattern = pattern;
operations = new List<Dictionary<string, dynamic>>();
}
public Datapoint(List<Dictionary<string, dynamic>> operations,
bool debug = false,
string pattern = "")
{
Debug = debug;
Pattern = pattern;
this.operations = operations;
}
请注意,您需要在第二次重载中对参数重新排序,因为可选参数必须位于参数列表的末尾。
你可以接受一个 null
然后在你的构造函数中检查它:
public Datapoint(
bool debug = false,
string pattern = "",
List<Dictionary<string, dynamic>> operations = null
)
{
Debug = debug;
Pattern = pattern;
this.operations = operations ?? new List<Dictionary<string, dynamic>>();
}
但是,请参阅 D Stanley 的回答的评论,以讨论一般情况下的缺点。
避免以这种方式使用可选参数
//constructor
public Datapoint(
bool debug = false, //dangerous
string pattern = "",//dangerous
List<Dictionary<string, dynamic>> operations = null
)
改为使用:
//constructor
public Datapoint(
bool? debug = null,
string pattern = null,
List<Dictionary<string, dynamic>> operations = null
)
{
Debug = debug.HasValue && debug.Value;
Pattern = pattern;
this.operations = (operations == null) ?
new List<Dictionary<string, dynamic>>()
: operations ;
}
我是 C# 的新手,一直在努力寻找一种惯用的方法来在构造函数中初始化列表。
没有完全解决问题的相关问题:
- How to initialize a list with constructor?
- Initializing lists in a constructor.
这有效,但有一个缺陷:
class Datapoint
{
public bool Debug { get; set; }
public string Pattern { get; private set; }
// I would prefer to initialize this list in the constructor
public List<Dictionary<string, dynamic>> operations =
new List<Dictionary<string, dynamic>>();
// constructor
public Datapoint(bool debug = false,
string pattern = ""
// I would prefer operations to go here
// but the following doesn't work:
// List<Dictionary<string, dynamic>> operations =
// new List<Dictionary<string, dynamic>>()
)
{
Debug = debug;
Pattern = pattern;
}
}
// Let's define some Datapoints
class Definitions
{
public static Datapoint turtles = new Datapoint
(
pattern: @"turtle pattern",
// I would prefer operations to go here
)
{
operations =
{ new Dictionary<string, dynamic>
{
["func"] = "stitch_lines"
}
}
};
}
缺陷是我不能将操作设置为私有,否则在创建海龟时会出错。
理想情况下,我希望 operations
成为构造函数的参数,但我遗漏了一些东西,因为我尝试的每个组合都会产生此错误:
Default parameter value for operations must be a compile-time constant.
提前感谢您的任何见解。
如错误所示,默认值必须是编译时常量。我会将其作为两个重载来执行:
// constructor
public Datapoint(bool debug = false,
string pattern = "")
{
Debug = debug;
Pattern = pattern;
operations = new List<Dictionary<string, dynamic>>();
}
public Datapoint(List<Dictionary<string, dynamic>> operations,
bool debug = false,
string pattern = "")
{
Debug = debug;
Pattern = pattern;
this.operations = operations;
}
请注意,您需要在第二次重载中对参数重新排序,因为可选参数必须位于参数列表的末尾。
你可以接受一个 null
然后在你的构造函数中检查它:
public Datapoint(
bool debug = false,
string pattern = "",
List<Dictionary<string, dynamic>> operations = null
)
{
Debug = debug;
Pattern = pattern;
this.operations = operations ?? new List<Dictionary<string, dynamic>>();
}
但是,请参阅 D Stanley 的回答的评论,以讨论一般情况下的缺点。
避免以这种方式使用可选参数
//constructor
public Datapoint(
bool debug = false, //dangerous
string pattern = "",//dangerous
List<Dictionary<string, dynamic>> operations = null
)
改为使用:
//constructor
public Datapoint(
bool? debug = null,
string pattern = null,
List<Dictionary<string, dynamic>> operations = null
)
{
Debug = debug.HasValue && debug.Value;
Pattern = pattern;
this.operations = (operations == null) ?
new List<Dictionary<string, dynamic>>()
: operations ;
}