Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 您能为类使用类似数组的构造函数吗_C#_Constructor - Fatal编程技术网

C# 您能为类使用类似数组的构造函数吗

C# 您能为类使用类似数组的构造函数吗,c#,constructor,C#,Constructor,是否可以让类构造函数的行为类似于数组初始值设定项,例如Foo-Foo={1,2,3} 对于隐式转换,我非常接近:Foo-Foo=newint[]{1,2,3} 但是我想添加更多的语法糖,因为这一点将在我的代码中使用。使其更像JSON。如果类实现了IEnumerable和Add(T),则可以非常接近,其中T是集合中的项目类型 例如,鉴于此: public sealed class Foo: IEnumerable<int> { public void Add(int item)

是否可以让类构造函数的行为类似于数组初始值设定项,例如
Foo-Foo={1,2,3}

对于隐式转换,我非常接近:
Foo-Foo=newint[]{1,2,3}


但是我想添加更多的语法糖,因为这一点将在我的代码中使用。使其更像JSON。

如果类实现了
IEnumerable
Add(T)
,则可以非常接近,其中
T
是集合中的项目类型

例如,鉴于此:

public sealed class Foo: IEnumerable<int>
{
    public void Add(int item)
    {
        _items.Add(item);
    }

    public IEnumerator<int> GetEnumerator()
    {
        return _items.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    readonly List<int> _items = new List<int>();
}
遗憾的是,以下语法仅保留给数组:

Foo foo = {1, 2, 3}; // Won't compile. You need the "new Foo".

我想到了两种方法可以更接近原始数组初始值设定项。 第一个很简单,但不使用集合初始值设定项:

    private int[] arr;
    public FooArray(params int[] numbers)
    {
        arr = numbers;
    }

    var foo1 = new FooArray(1, 2, 3, 4, 2309, 12);

第二个选项是只使用params修饰符实现集合初始值设定项。您还必须实现IEnumerable
    public void Add(params int[] numbers)
    {
        arr = numbers;
    }

    public IEnumerator<int> GetEnumerator()
    {
        return ((IEnumerable<int>)arr).GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    var foo2 = new FooArray { 1, 2, 3, 4, 2309, 12 };
public void Add(参数int[]编号)
{
arr=数字;
}
公共IEnumerator GetEnumerator()
{
return((IEnumerable)arr.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
var foo2=新的FooArray{1,2,3,42309,12};

那么如果您执行
Foo-Foo={1,“2”,3.0}
会发生什么?编译器如何决定哪些是有效项,哪些不是?除了减少字符(但以牺牲可读性为代价)之外,我并没有真正看到你的建议的好处;你能?花括号之间的元素数是常量吗?@James编译器可以像处理数组一样处理它(
int[]b={1,“two”,2};
也不会编译)。@mydenomismonicarestate是的,这也很有用。元素的数量是可变的。@aXu\u AP那么编译器如何知道您需要一个
int[]
/
string[]
/
float[]
等?必须做出一些决定,很可能是检查数据类型。通过明确你正在做的事情,有很多话要说,也有很多收获。感谢这个例子,现在我可能不再像这样初始化我的对象了。调用add 3次感觉像是一种浪费,尤其是在没有容量或像ICollections那样的“CopyTo”的情况下。如果我们有另一个add(int number),那么它会选择第一个add(cast),我们可以不知何故(cast)使用哪个add方法吗?你必须告诉编译器接受一个对象而不是多个整数。像这样:新的FooArray{{{1,2,3,42309,12};
    public void Add(params int[] numbers)
    {
        arr = numbers;
    }

    public IEnumerator<int> GetEnumerator()
    {
        return ((IEnumerable<int>)arr).GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    var foo2 = new FooArray { 1, 2, 3, 4, 2309, 12 };