c#删除 list中的元素和怎么去除空元素

for (int i = list.Count - 1; i >= 0; i--)
{
  if (list[i].NO == item.NO)
  {
  list.RemoveAt(i);
  }
}
 public void RemoveItemFromList(ref List <A> list, A item)
        {
            List <A> tempList = new List <A>();
            foreach (A a in list)
            {
                if (a.NO != item.NO && !tempList.Contains(a))
                    tempList.Add(a);
            }
            list = tempList;
        }

如果支除空元素,可以使用Split参数StringSplitOptions.RemoveEmptyEntries去实现

用法:

 string test = "程$晓$";
  使用:string[] temp = test.Split(new string[] { "$" }, StringSplitOptions.RemoveEmptyEntries);
  输出结果:数组长度为2 temp[0]="" temp[1]="";
 

  使用:string[] temp = test.Split(new string[] { "$" }, StringSplitOptions.None);或string[] temp = test.Split('$');


  输出结果:数组长度为3 temp[0]="" temp[1]="" temp[2]="";
  

string[] inputpids = productIds.IndexOf(',') > 0 ? productIds.Split(',').Distinct().ToArray() : new string[] { productIds };
int[] outputpids = Array.ConvertAll<string, int>(inputpids, delegate(string s)
{
var val = 0;
int.TryParse(s, out val);
return val;
});
req.ProductIds = outputpids.Where(c=>c>0).ToList();

原文地址:https://www.cnblogs.com/shy1766IT/p/5186363.html