一、c#中的特殊数据类型
C#中特殊数据类型有class类型、结构类型、数组类型、枚举类型、集合类型、委托类型、事件、Lambda表达式、接口类型。
1、class类型
1.1类定义
class Student : person//继承 { //默认构造函数,vs IDE中输入ator+tab两下可自动生成构造函数代码段 public Student() { //给字段赋值等初始化操作 } //自定义构造函数,b为可选参数 public Student(int a, string b = 5) { //初始化操作 } public Student(int a)//自定义构造函数 { //初始化操作 } //静态构造函数-安全的给静态字段赋值且只能被定义一个(采用普通构造函数每次实例化类对象时静态字段的值会被重置) static Student{aCuu=0.04}//不允许访问修饰符,不接受参数 //static关键字用来定义静态成员,这些成员只能通过类级别而不是对象引用调用。 //static关键字,静态字段由所有类实例共享 public static double aCuu = 0.04; public string petName;//字段,最好定义成私有的以保护 public void Name(string name) { }---方法 } //静态类-只包含静态成员,不能够使用new(分配新内存)关键字来创建类对象-适合做工具类 static class Student { }
1.2访问修饰符
类型成员隐式私有的,而类是隐式内部的。
private、protected、protected internal访问修饰符可以应用到嵌套类型上,非嵌套类型只能用public internal修饰符定义。
| public | 可以从对象、派生类、外部其它程序集访问 |
| private | 定义类访问 |
| protected | 定义类及子类可以访问,外部类无法通过点操作符访问 |
| internal | 只能在当前程序集中访问 |
| protected internal | 在定义该项的程序集、类及派生类中可用 |
1.3 this 关键字
提供对当前类实例的访问。
串联构造函数,使类更容易维护和简明。
class Motorcycle { public int driverIntensity; public string drivername; //构造函数 public Motorcycle(){} public Motorcycle(int intensity) : this(intensity, "") { } public Motorcycle(string name):this(0,name) { } //所有工作的主构造函数 public Motorcycle(int intensity,string name) { //操作 } }
2、结构类型
结构类型可以看成是轻量级的类类型,但是不能像类一样继承
struct point { //字段 public int x; //方法 public void Increment() { } } //创建结构变量 point p1; p1.x = 5;//需要为每一个共有字段成员赋值 point p2 = new point();
3、数组类型
一组相同类型的数据点:int[] myInt=new int[3]
int[] myInt=new int[3]{1,2,3} #初始化
3.1 特殊数组
| 隐式数组 | var a=new[]{1,2,3} |
| object数组 | object[] myob=new[3]{1,"string",new DateTime(2017,8,28)} |
| 多维数组 | int[,] myArray=new int[6,6] |
4、枚举类型
System.Enum类中包含有一些对枚举类型操作的方法
enum Emptype:Byte { manager=12,//=0,可任意,可不连续 student, teancher } Emptype ee = Emptype.manager; Enum.GetUnderlyingType(ee.GetType());//返回枚举类型值的数据类型 Console.WriteLine(ee.ToString());//获取枚举名字 Console.WriteLine((Byte)ee);//根据底层类型强制转换获取其数值 Array enumData = Enum.GetValues(ee.GetType());//获取所有枚举值 Console.WriteLine("this enum has {0} nums:", enumData.Length); Console.WriteLine("name{0};value{1}", enumData.GetValue(1));
5、集合类型-数据容器
System.Collections与System.Collection.Generic命名空间
简单数组通常是固定大小的,而集合类本身的尺寸是动态的,类似于python中的list类型,很多集合提供了更强的类型安全,进行了高度优化,可以以内存高效的方式处理所包含的数据。
集合可划分为两大种类:非泛型集合和泛型集合
5.1泛型集合System.Collections常用类
| ArrrayList | 动态大小对象集合,对象按顺序列出 | Ilist/Icollection/IEnumerable/ICloneable |
| BitArray | 管理位值的简单数组,位值bool类型 | ICollection/IEnumerable/ICloneable |
| Hashtable | 表示键值对的集合 | IDictionary/Icollection/IEnumerable/ICloneable |
| Queue | FIFO队列 | ICollection/IEnumerable/ICloneable |
| SortedList | 键值对集合按键排序 | IDictionary/Icollection/IEnumerable/ICloneable |
| Stack | 后进先出 | ICollection/IEnumerable/ICloneable |
System.Collections中类所支持的关键接口
| ICollection | 为所有非泛型集合定义基本特性(大小、枚举、线程安全) |
| ICloneable | 允许实现它的对象向调用者返回它的副本 |
| IDictionary | 非泛型集合使用键值对表示其内容 |
| IEnumerable | 返回实现了IEnumerable接口的对象 |
| IEnumerator | 允许子类型以foreach形式迭代 |
| IList | 为顺序列表中的对象提供添加、移除和索引项的行为 |
示例代码:
class Program { static void Main(string[] args) { ArrayList list = new ArrayList(); //集合:很多数据的一个集合 //数组:长度不可变、类型单一 list.Add(true); list.Add(1); list.Add(2); list.Add("张三"); Person person = new Person(); list.Add(person); list.Add(new int[] { 1, 2, 3 }); list.AddRange(list); list.AddRange(new int[] { 234, 23432 });//此种方法可以直接输出 for(int i=0; i<list.Count; i++) { if(list[i] is Person ) { ((Person)list[i]).SayKello(); } else if(list[i] is int[]) { for(int j=0;j<((int[])list[i]).Length;j++) { Console.WriteLine(((int[])list[i])[j]); } } //麻烦 else { Console.WriteLine(list[i]); } } Console.ReadKey(); //List元素打印出来的是所在类对应的命名空间 } class Person { public void SayKello() { Console.Write("你好"+"\r\n"); } } }
//其他类的用法类似,有相应的文档
HashTable示例:
static void Main(string[] args) { //键值对集合类似于字典,根据键去找值,就像根据拼音去找汉字 Hashtable ht = new Hashtable(); ht.Add(1,"张三"); ht.Add(2,"小王"); ht.Add(false,"错误的"); for (int i = 0; i < ht.Count; i++) { object a = ht[i]; Console.WriteLine(a); } //不能全部显示(0和false) Console.ReadKey(); Console.WriteLine(ht[false]); //也可以采用foreach循环遍历 foreach (var item in ht.Keys) { //C#是一门强类型语言:代码中每一个变量类型必须有明确的定义 //int n = 15; //string ste = "sfdd"; //bool n5 = true; //double n6 = 230.23; //Console.WriteLine(n.GetType()); //Console.WriteLine(ste.GetType()); //Console.WriteLine(n5.GetType()); //Console.WriteLine(n6.GetType()); //Console.ReadKey(); //根据值推断出类型 //var n = 15; //var ste = "sfdd"; //var n5 = true; //var n6 = 230.23; //Console.WriteLine(n.GetType()); //Console.WriteLine(ste.GetType()); //Console.WriteLine(n5.GetType()); //Console.WriteLine(n6.GetType()); //Console.ReadKey(); //var input; 声明var变量的时候必需给它赋值 //input = "人民"; Console.WriteLine(ht[item]); } Console.ReadKey(); }