How to empty a list in C#?

前端 未结 7 1091
旧时难觅i
旧时难觅i 2020-12-09 00:24

I want to empty a list. How to do that?

7条回答
  •  悲哀的现实
    2020-12-09 00:55

    To give an alternative answer (Who needs 5 equal answers?):

    list.Add(5); 
    // list contains at least one element now
    list = new List();
    // list in "list" is empty now
    

    Keep in mind that all other references to the old list have not been cleared (depending on the situation, this might be what you want). Also, in terms of performance, it is usually a bit slower.

提交回复
热议问题