首页 > 编程语言 >C#笔记 其三

C#笔记 其三

时间:2022-10-27 12:01:39浏览次数:45  
标签:dictionary 索引 C# 其三 list 笔记 int IList foreach

自定义集合

集合接口

IList<T>列表

关键词:变长数组、有序集合、任意访问、索引

常用方法:

list.Add(T);

list.Remove();
list.RemoveAt(int);

list.Sort();
list.Sort(IComparer<T>);

list.Contains(T);
list.IndexOf(T);
list.LastIndexOf(T);
list.BinarySearch(T);

list.FindAll(Predicate<T>);

list[int];
foreach (T item in list);
IDictionary<TKey, TValue>字典

关键词:键值对

常用方法:

dictionary.Add(TKey, TValue);

dictionary.Remove(TKey);

dictionary[TKey];
foreach (KeyValuePair<TKey, TValue> pair in dictionary);

dictionary.Keys;
dictionary.Values;
SortedDictionary<TKey, TValue> & SortedList<T>已排序集合

前者按排序,后者按排序,二者分别是IDictionary<TKey, TValue>IList<T>的派生

sortedDictionary.Keys返回IList<TKey>sortedDictionary.Values返回IList<TValue>

Stack<T>

Push()Pop()为压入和弹出

Peek(), Contains(), foreach都不会改变栈

Queue<T>队列

Enqueue()Dequeue()为入队和出队

Peek(), Contains(), foreach都不会改变队列

LinkedList<T> & LinkedListNode<T>双向链表

索引器

使用关键词this跟方括号中的参数列表

public class A<TKey, TValue> {
    public TValue this[TKey key] {
        // 索引跟属性相似
        get { ... }
        set { ... }
    }
    // 可以重载和使用多个参数
    public TValue this[int i, int j] {
        get { ... }
    }
    // 可变参数也行
    public IList<T> this[params int[] indexes] {
        get { ... }
    }
}

实际上索引器就是名为Item的特殊属性,所以不能同时创建索引器和Item属性,除非另外指定索引器名称

迭代器

IEnumerable<T>

实现了此接口的类和方法可以使用foreach

标签:dictionary,索引,C#,其三,list,笔记,int,IList,foreach
From: https://www.cnblogs.com/violeshnv/p/16831737.html

相关文章

  • mysql用select的子查询结果作为where后筛选条件
    mysqlselect查询语句where子句除了写子查询,还有没有更好的代替子查询的?一使用SELECT子句进行多表查询SELECT字段名FROM表1,表2…WHERE表1.字段=表2.字段AND其它查询条件SELE......
  • C#笔记 其一
    类类的声明和实例化classA{}//anotherfileF(){Aa=newA();//实例化类,new<类名>(实例化参数)Aaa;aa=newA();//先声明后实例化同样可行......
  • C#中的 `true` 和 `false` 运算符
    C#中的true和false运算符基础用法我们先定义一个示例用的类publicclassBoolTest{publicintX{get;set;}publicBoolTest(intx){X=x;}publ......
  • C#笔记(输入输出、格式化、注释)
    输入输出ConsoleKeyInfoc;do{c=Console.ReadKey();//读取按键}while(c.Key!=ConsoleKey.Escape);//等待输入Esc键strings=Console.ReadLine();i......
  • csharp-webuploader+csharp如何实现分片+断点续传
    ​文件夹数据库处理逻辑public class DbFolder{    JSONObjectroot;       public DbFolder()    {        this.root= new JSONOb......
  • C语言结构体中的零长数组
    C语言结构体中的零长数组实例structA{intlen;intvar[0];};structB{intlen;int*var;}其中,结构体A使用了零长数组,结构体B用了指针。为......
  • fs01 FreeSWITCH中APP和API
    PART1APP和API的区别 简单来说,一个APP是一个程序,它作为一个Channel一端与另一端的UA进行通信,相当于它工作在Channel内部;而一个API则是独立于一个Channel之外的,它只能通......
  • C语言的单引号问题
    C语言的单引号问题单引号的原理C语言的单引号实际上时将''内的字符转化为ASCII码对应的整型值,并且在存储时占据一个字节,即sizeof(char)//第一个例子intmain(){......
  • rancher 部署k8s
    yuminstall-yyum-utils  device-mapper-persistent-data  lvm2curl-o/etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-7.repoyum-co......
  • C#在调用UI刷新时启用了不同的线程,导致数据异常的解决方案
    将原先的刷新函数封装如下原先的调用方式publicvoidRefreshGrid(){System.Diagnostics.Debug.WriteLine("CurrentThreadID:"+System.Threadi......