Ale*_*aro 3 .net c# oop constructor class
但是,无论如何都要像数组或字典那样初始化我的类
private class A
{
private List<int> _evenList;
private List<int> _oddList;
...
}
Run Code Online (Sandbox Code Playgroud)
并说
A a = new A {1, 4, 67, 2, 4, 7, 56};
Run Code Online (Sandbox Code Playgroud)
并在我的构造函数中填充_ evenList和_ oddList及其值.
要使用集合初始值设定项,您的类必须:
IEnumerable
Add
方法例如:
class A : IEnumerable
{
private List<int> _evenList = new List<int>();
private List<int> _oddList = new List<int>();
public void Add(int value)
{
List<int> list = (value & 1) == 0 ? _evenList : _oddList;
list.Add(value);
}
// Explicit interface implementation to discourage calling it.
// Alternatively, actually implement it (and IEnumerable<int>)
// in some fashion.
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException("Not really enumerable...");
}
}
Run Code Online (Sandbox Code Playgroud)