Set/extend List length in c#

后端 未结 3 1251
广开言路
广开言路 2020-12-16 19:34

Given a List in c# is there a way to extend it (within its capacity) and set the new elements to null? I\'d like something that works like

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 20:30

    static IEnumerable GetValues(T value, int count) {
       for (int i = 0; i < count; ++i)
          yield return value;
    }
    
    list.AddRange(GetValues(null, number_of_nulls_to_add));
    
    
    

    This will work with 2.0+

    提交回复
    热议问题