简体   繁体   English

从IEnumerable列表添加值数组列表

[英]adding values array list from a IEnumerable list

I have the following code. 我有以下代码。

public IEnumerable<int> BillLevel { get; set; }

I want to add values like this 我想添加这样的值

BillLevel = [1, 2, 3, 4, 5],

What is the right syntax to assign an array of int to this list? 将int数组分配给此列表的正确语法是什么?

IEnumerable is an Interface so first you need to declare it with a class which implements IEnumerable like List and then simply add to list. IEnumerable是一个接口,因此首先需要使用一个实现IEnumerable类(如List来声明它,然后简单地添加到list。

public IEnumerable<int> BillLevel { get; set; }

BillLevel = new List<int>();
BillLevel.AddRange(new int[]{1, 2, 3, 4, 5});

Or you can Add the numbers in declaration 或者您可以在声明中添加数字

BillLevel = new List<int>(){1, 2, 3, 4, 5};

这将使用整数数组引入BillLevel属性。

BillLevel = new[] {1, 2, 3, 4}; 

You must to pass the reference of a class which implement IEnumerable Interface. 您必须传递实现IEnumerable接口的类的引用。 Such as List Class implement this interface so you can pass the instance of List of int into this 例如List Class实现此接口,因此您可以将int List的实例传递给此接口

public IEnumerable<int> BillLevel { get; set; }

BillLevel = new List<int>(){1,2,3,4,5};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM