从列表字符串c#中删除null

roy*_*.me 0 c#

似乎已经回答了几次,但它对我不起作用......

我有一个名为combox的列表.我想删除空项目.

我写:

   List<String> comboxTable = new List<string>();  
    comboxTable = comboxTable.RemoveAll(item => item == null);
Run Code Online (Sandbox Code Playgroud)

但我得到了:

错误14无法将类型'int'隐式转换为'System.Collections.Generic.List'

我试过这个:

comboxTable = comboxTable.RemoveAll(string.IsNullOrWhiteSpace);
Run Code Online (Sandbox Code Playgroud)

同样的错误.'int'在哪里?我究竟做错了什么?

(Combox在代码中的其他地方得到它的字符串值,并且在这里和那里都有Null.我想删除它们)

dim*_*cas 8

查看文档.RemoveAll()返回一个整数,您的代码会尝试将其强制转换为类型的对象System.Collections.Generic.List

将您的代码更改为:

comboxTable.RemoveAll(string.IsNullOrWhiteSpace);
Run Code Online (Sandbox Code Playgroud)

它会起作用.这些null物品将被删除.RemoveAll在实例上运行它不会返回新的实例


g.p*_*dou 5

RemoveAll() 修改实例本身并返回移除的元素数