跳过正文
  1. 文章/

C#

2024

12、集合
·2439 字·5 分钟· loading · loading
C#
类 描述和用法 动态数组(ArrayList) 它代表了可被单独索引的对象的有序集合。它基本上可以替代一个数组。但是,与数组不同的是,您可以使用索引在指定的位置添加和移除项目,动态数组会自动重新调整它的大小。它也允许在列表中进行动态内存分配、增加、搜索、排序各项。 哈希表(Hashtable) 它使用键来访问集合中的元素。当您使用键访问元素时,则使用哈希表,而且您可以识别一个有用的键值。哈希表中的每一项都有一个键/值对。键用于访问集合中的项目。 排序列表(SortedList) 它可以使用键和索引来访问列表中的项。排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。如果您使用索引访问各项,则它是一个动态数组(ArrayList),如果您使用键访问各项,则它是一个哈希表(Hashtable)。集合中的各项总是按键值排序。 堆栈(Stack) 它代表了一个后进先出的对象集合。当您需要对各项进行后进先出的访问时,则使用堆栈。当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素。 队列(Queue) 它代表了一个先进先出的对象集合。当您需要对各项进行先进先出的访问时,则使用队列。当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队。 点阵列(BitArray) 它代表了一个使用值 1 和 0 来表示的二进制数组。当您需要存储位,但是事先不知道位数时,则使用点阵列。您可以使用整型索引从点阵列集合中访问各项,索引从零开始。 ArrayList # using System; using System.Collections; namespace CollectionApplication { class Program { static void Main(string[] args) { ArrayList al = new ArrayList(); Console.WriteLine("Adding some numbers:"); al.Add(45); al.Add(78); al.Add(33); al.Add(56); al.Add(12); al.Add(23); al.Add(9); Console.WriteLine("Capacity: {0} ", al.Capacity); Console.WriteLine("Count: {0}", al.Count); Console.Write("Content: "); foreach (int i in al) { Console.Write(i + " "); } Console.WriteLine(); Console.Write("Sorted Content: "); al.Sort(); foreach (int i in al) { Console.Write(i + " "); } Console.WriteLine(); Console.ReadKey(); } } } 属性 # 属性 描述 Capacity 获取或设置 ArrayList 可以包含的元素个数。 Count 获取 ArrayList 中实际包含的元素个数。 IsFixedSize 获取一个值,表示 ArrayList 是否具有固定大小。 IsReadOnly 获取一个值,表示 ArrayList 是否只读。 IsSynchronized 获取一个值,表示访问 ArrayList 是否同步(线程安全)。 Item[Int32] 获取或设置指定索引处的元素。 SyncRoot 获取一个对象用于同步访问 ArrayList。 方法 # 序号 方法名 & 描述 1 public virtual int Add( object value ); 在 ArrayList 的末尾添加一个对象。 2 public virtual void AddRange( ICollection c ); 在 ArrayList 的末尾添加 ICollection 的元素。 3 public virtual void Clear(); 从 ArrayList 中移除所有的元素。 4 public virtual bool Contains( object item ); 判断某个元素是否在 ArrayList 中。 5 public virtual ArrayList GetRange( int index, int count ); 返回一个 ArrayList,表示源 ArrayList 中元素的子集。 6 public virtual int IndexOf(object); 返回某个值在 ArrayList 中第一次出现的索引,索引从零开始。 7 public virtual void Insert( int index, object value ); 在 ArrayList 的指定索引处,插入一个元素。 8 public virtual void InsertRange( int index, ICollection c ); 在 ArrayList 的指定索引处,插入某个集合的元素。 9 public virtual void Remove( object obj ); 从 ArrayList 中移除第一次出现的指定对象。 10 public virtual void RemoveAt( int index ); 移除 ArrayList 的指定索引处的元素。 11 public virtual void RemoveRange( int index, int count ); 从 ArrayList 中移除某个范围的元素。 12 public virtual void Reverse(); 逆转 ArrayList 中元素的顺序。 13 public virtual void SetRange( int index, ICollection c ); 复制某个集合的元素到 ArrayList 中某个范围的元素上。 14 public virtual void Sort(); 对 ArrayList 中的元素进行排序。 15 public virtual void TrimToSize(); 设置容量为 ArrayList 中元素的实际个数。 Hashtable # using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add("001", "Zara Ali"); ht.Add("002", "Abida Rehman"); ht.Add("003", "Joe Holzner"); ht.Add("004", "Mausam Benazir Nur"); ht.Add("005", "M. Amlan"); ht.Add("006", "M. Arif"); ht.Add("007", "Ritesh Saikia"); if (ht.ContainsValue("Nuha Ali")) { Console.WriteLine("This student name is already in the list"); } else { ht.Add("008", "Nuha Ali"); } // 获取键的集合 ICollection key = ht.Keys; foreach (string k in key) { Console.WriteLine(k + ": " + ht[k]); } Console.ReadKey(); } } } 属性 # 属性 描述 Count 获取 Hashtable 中包含的键值对个数。 IsFixedSize 获取一个值,表示 Hashtable 是否具有固定大小。 IsReadOnly 获取一个值,表示 Hashtable 是否只读。 Item 获取或设置与指定的键相关的值。 Keys 获取一个 ICollection,包含 Hashtable 中的键。 Values 获取一个 ICollection,包含 Hashtable 中的值。 方法 # 序号 方法名 & 描述 1 public virtual void Add( object key, object value ); 向 Hashtable 添加一个带有指定的键和值的元素。 2 public virtual void Clear(); 从 Hashtable 中移除所有的元素。 3 public virtual bool ContainsKey( object key ); 判断 Hashtable 是否包含指定的键。 4 public virtual bool ContainsValue( object value ); 判断 Hashtable 是否包含指定的值。 5 public virtual void Remove( object key ); 从 Hashtable 中移除带有指定的键的元素。 SortedList # using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { SortedList sl = new SortedList(); sl.Add("001", "Zara Ali"); sl.Add("002", "Abida Rehman"); sl.Add("003", "Joe Holzner"); sl.Add("004", "Mausam Benazir Nur"); sl.Add("005", "M. Amlan"); sl.Add("006", "M. Arif"); sl.Add("007", "Ritesh Saikia"); if (sl.ContainsValue("Nuha Ali")) { Console.WriteLine("This student name is already in the list"); } else { sl.Add("008", "Nuha Ali"); } // 获取键的集合 ICollection key = sl.Keys; foreach (string k in key) { Console.WriteLine(k + ": " + sl[k]); } } } } 属性 # 属性 描述 Capacity 获取或设置 SortedList 的容量。 Count 获取 SortedList 中的元素个数。 IsFixedSize 获取一个值,表示 SortedList 是否具有固定大小。 IsReadOnly 获取一个值,表示 SortedList 是否只读。 Item 获取或设置与 SortedList 中指定的键相关的值。 Keys 获取 SortedList 中的键。 Values 获取 SortedList 中的值。 方法 # 序号 方法名 & 描述 1 public virtual void Add( object key, object value ); 向 SortedList 添加一个带有指定的键和值的元素。 2 public virtual void Clear(); 从 SortedList 中移除所有的元素。 3 public virtual bool ContainsKey( object key ); 判断 SortedList 是否包含指定的键。 4 public virtual bool ContainsValue( object value ); 判断 SortedList 是否包含指定的值。 5 public virtual object GetByIndex( int index ); 获取 SortedList 的指定索引处的值。 6 public virtual object GetKey( int index ); 获取 SortedList 的指定索引处的键。 7 public virtual IList GetKeyList(); 获取 SortedList 中的键。 8 public virtual IList GetValueList(); 获取 SortedList 中的值。 9 public virtual int IndexOfKey( object key ); 返回 SortedList 中的指定键的索引,索引从零开始。 10 public virtual int IndexOfValue( object value ); 返回 SortedList 中的指定值第一次出现的索引,索引从零开始。 11 public virtual void Remove( object key ); 从 SortedList 中移除带有指定的键的元素。 12 public virtual void RemoveAt( int index ); 移除 SortedList 的指定索引处的元素。 13 public virtual void TrimToSize(); 设置容量为 SortedList 中元素的实际个数。 Stack # using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Stack st = new Stack(); st.Push('A'); st.Push('M'); st.Push('G'); st.Push('W'); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); st.Push('V'); st.Push('H'); Console.WriteLine("The next poppable value in stack: {0}", st.Peek()); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); Console.WriteLine("Removing values "); st.Pop(); st.Pop(); st.Pop(); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } } } } 属性 # 属性 描述 Count 获取 Stack 中包含的元素个数。 方法 # 序号 方法名 & 描述 1 public virtual void Clear(); 从 Stack 中移除所有的元素。 2 public virtual bool Contains( object obj ); 判断某个元素是否在 Stack 中。 3 public virtual object Peek(); 返回在 Stack 的顶部的对象,但不移除它。 4 public virtual object Pop(); 移除并返回在 Stack 的顶部的对象。 5 public virtual void Push( object obj ); 向 Stack 的顶部添加一个对象。 6 public virtual object[] ToArray(); 复制 Stack 到一个新的数组中。 Queue # using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Queue q = new Queue(); q.Enqueue('A'); q.Enqueue('M'); q.Enqueue('G'); q.Enqueue('W'); Console.WriteLine("Current queue: "); foreach (char c in q) Console.Write(c + " "); Console.WriteLine(); q.Enqueue('V'); q.Enqueue('H'); Console.WriteLine("Current queue: "); foreach (char c in q) Console.Write(c + " "); Console.WriteLine(); Console.WriteLine("Removing some values "); char ch = (char)q.Dequeue(); Console.WriteLine("The removed value: {0}", ch); ch = (char)q.Dequeue(); Console.WriteLine("The removed value: {0}", ch); Console.ReadKey(); } } } 属性 # 属性 描述 Count 获取 Queue 中包含的元素个数。 方法 # 序号 方法名 & 描述 1 public virtual void Clear(); 从 Queue 中移除所有的元素。 2 public virtual bool Contains( object obj ); 判断某个元素是否在 Queue 中。 3 public virtual object Dequeue(); 移除并返回在 Queue 的开头的对象。 4 public virtual void Enqueue( object obj ); 向 Queue 的末尾添加一个对象。 5 public virtual object[] ToArray(); 复制 Queue 到一个新的数组中。 6 public virtual void TrimToSize(); 设置容量为 Queue 中元素的实际个数。
11、异常处理
·695 字·2 分钟· loading · loading
C#
异常处理 # 异常是在程序执行期间出现的问题。C# 中的异常是对程序运行时出现的特殊情况的一种响应,比如尝试除以零。
10、接口
·81 字·1 分钟· loading · loading
C#
接口 # 接口除了实现是用:,其余和Java中的使用基本一致。
9、类
·2135 字·5 分钟· loading · loading
C#
类Class # using System; namespace BoxApplication { class Box { public double length; // 长度 public double breadth; // 宽度 public double height; // 高度 } class Boxtester { static void Main(string[] args) { Box Box1 = new Box(); // 声明 Box1,类型为 Box Box Box2 = new Box(); // 声明 Box2,类型为 Box double volume = 0.0; // 体积 // Box1 详述 Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; // Box2 详述 Box2.height = 10.0; Box2.length = 12.0; Box2.breadth = 13.0; // Box1 的体积 volume = Box1.height * Box1.length * Box1.breadth; Console.WriteLine("Box1 的体积: {0}", volume); // Box2 的体积 volume = Box2.height * Box2.length * Box2.breadth; Console.WriteLine("Box2 的体积: {0}", volume); Console.ReadKey(); } } } ToString # C#中ToString方法返回一个对象实例的字符串,在默认情况下将返回类类型的限定名
1、C#
·1685 字·4 分钟· loading · loading
C#
C# 是一个现代的、通用的、面向对象的编程语言,它是由微软(Microsoft)开发的,由 Ecma 和 ISO 核准认可的。
8、枚举
·206 字·1 分钟· loading · loading
C#
枚举 # 枚举是一组命名整型常量。枚举类型是使用 enum 关键字声明的。
7、结构体
·1531 字·4 分钟· loading · loading
C#
结构体 # 在 C# 中,结构体是值类型数据结构,这样使得一个单一变量可以存储各种数据类型的相关数据。
6、字符串
·1817 字·4 分钟· loading · loading
C#
在 C# 中,您可以使用字符数组来表示字符串,但是,更常见的做法是使用 string 关键字来声明一个字符串变量。string 关键字是 System.String 类的别名。
5、数组
·845 字·2 分钟· loading · loading
C#
数组基础 # 声明 # datatype[] arrayName; datatype 用于指定被存储在数组中的元素的类型。 [] 指定数组的秩(维度)。秩指定数组的大小。 arrayName 指定数组的名称。 初始化 # 声明一个数组不会在内存中初始化数组。当初始化数组变量时,可以赋值给数组。
4、方法
·1322 字·3 分钟· loading · loading
C#
定义方法 # <Access Specifier> <Return Type> <Method Name>(Parameter List) { Method Body } Access Specifier:访问修饰符,这个决定了变量或方法对于另一个类的可见性。 Return type:返回类型,一个方法可以返回一个值。返回类型是方法返回的值的数据类型。如果方法不返回任何值,则返回类型为 void。 Method name:方法名称,是一个唯一的标识符,且是大小写敏感的。它不能与类中声明的其他标识符相同。 Parameter list:参数列表,使用圆括号括起来,该参数是用来传递和接收方法的数据。参数列表是指方法的参数类型、顺序和数量。参数是可选的,也就是说,一个方法可能不包含参数。 Method body:方法主体,包含了完成任务所需的指令集。 using System; namespace HelloWorldApplication { class Program { static void Main(string[] args) { Program p = new Program(); int c = p.add(1,2); Console.WriteLine(c); } public int add(int a,int b){ return a + b; } } } 参数传递 # 当调用带有参数的方法时,您需要向方法传递参数。在 C# 中,有三种向方法传递参数的方式: