小Q的博客

  • 首页
  • net编程
    • 产品和框架
    • 开发实例
    • 经验技巧
    • 开源组件
  • wp独立站
  • 自媒体
  • 日记本
  • 工具箱
每个程序员,都应该有一个自己的博客站
  1. 首页
  2. net编程
  3. 经验技巧
  4. 正文

net中c#教程 list泛型的常用操作

2022年10月5日 618点热度 0人点赞 0条评论

上一篇讲的是非常基础的string字符串操作,这在c#的应用非常广泛。今天深入一点,讲下List泛型的一些用法。对于泛型的增删改查操作,找本c#的基础教程书籍,看看就行了。

目录

  • 1、List泛型
    • 1.1、同时增加多项
    • 1.2、循环中删除某项
    • 1.3、排序
    • 1.4、去重
    • 1.5、分页
  • 2、Dictionary类型

1、List泛型

1.1、同时增加多项

增加一项,可以使用Add或Insert。如果同时增加多个项,推荐使用InsertRange函数

//1,增加项
List<string> listSource31 = new List<string>();
for (int i = 1; i <= 20; i++)
{
    listSource31.Add(i.ToString());
}
int pCount31 = listSource31.Count;

List<string> listAdd31 = new List<string>() { "21", "22", "23" };
listSource31.InsertRange(listSource31.Count, listAdd31);
Console.WriteLine(string.Format("原个数:{0},现在个数:{1}", pCount31, listSource31.Count));

 

1.2、循环中删除某项

正向删除会报错,提示循环体结构发生改变。因此需要反向循环,从最后一项开始往前循环

//2,循环中删除某几项            
List<string> listP21 = new List<string>();
for (int i = 1; i <= 50; i++)
{
    listP21.Add(i.ToString());
}
int pCount21 = listP21.Count;

List<string> listDel21 = new List<string>() { "1", "3", "14", "16", "6", "9", "11", };
for (int i = listP21.Count - 1; i >= 0; i--)
{
    string p = listP21[i];

    if (listDel21.Contains(p))
        listP21.Remove(p);
}
Console.WriteLine(string.Format("合计:{0}个,删除:{1}个,剩余:{2}个", pCount21, listDel21.Count, listP21.Count));

 

1.3、排序

其实用Sort这个函数就搞定了,之前还是写代码实现的,有点2

//3,排序
List<int> listP31 = new List<int>() { 13, 4, 1, 5, 7, 12, 4 };
listP31.Sort();

 

1.4、去重

List泛型是允许子项有重复数据的,因此有时是需要去掉重复项的。之前也是写代码实现的,直到发现了Distinct这个函数

//去重
List<int> listP32 = new List<int>() { 1, 1, 1, 2, 2, 12, 4 };
List<int> listP321 = listP32.Distinct().ToList();

 

1.5、分页

涉及2个函数,分别是Skip和Take,一个是跳过多少条数据,一个是要取多少条数据。看代码吧,一目了然

List<string> listP33 = new List<string>();
for(int i = 1; i <= 1000; i++)
{
    listP33.Add(i.ToString());
}
//取第201-300的数据
List<string> listP331 = listP33.Skip(200).Take(100).ToList();
string p331s = string.Join(",", listP331);

 

2、Dictionary类型

泛型的另一个对象就是Dictionary类型,用法其实和List差不多。有一个区别就是Key值不允许重复。如果想直接排序,可以使用SortDictionary(可用于开放平台生成sign值)

//相比List,Dictionary的要求是Key值不能重复,如果重复就会抛出异常。如果想排序的话,还可以使用SortedDictionary类,直接排序好
SortedDictionary<int, string> sdicP33 = new SortedDictionary<int, string>();
sdicP33.Add(13, "1313");
sdicP33.Add(1, "11");
sdicP33.Add(2, "22");
sdicP33.Add(8, "88");
sdicP33.Add(5, "55");

//显示结果
//Key:1,Value:11
//Key:2,Value:22
//Key:5,Value:55
//Key:8,Value:88
//Key:13,Value:1313
foreach (KeyValuePair<int,string>dic in sdicP33)
{
    Program.WriteLine(string.Format("Key:{0},Value:{1}", dic.Key, dic.Value));
}

 

相关阅读

net中c#教程 string字符串的常用操作

标签: c#教程 List泛型 net教程
最后更新:2022年10月7日

小Q

80后中年不油腻大叔,喜欢编写代码、打羽毛球、做木制玩具。目前定居浙江杭州

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022 小Q的博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备2022019157号-2