I would like to initialize a list with a given number of items, all with value DBNull.Value, is this possible via AddRange?
This code initializes as nulls and not DBNull.Value
_cellList = new List<object>(new DBNull[_columns.Count]);
Whereas this does the job correctly, but with a for loop:
_cellList = new List<object>();
for(int i = 0; i<_columns.Count; i++)
{
_cellList.Add(DBNull.Value);
}
thanks
You can use Enumerable.Repeat
in combination with ToList
, like this:
_cellList = Enumerable
.Repeat(DBNull.Value, _columns.Count)
.Cast<object>()
.ToList();
Note the use of Cast<object>()
, which is necessary to construct List<object>
instead of List<DBNull>
.