C#基础知识汇总

 

简介

C# 是一种具有 C++ 特性,Java 样式及 BASIC 快速建模特性的编程语言。

编程结构

C# 是大小写敏感的。半角分号(;)是语句分隔符。C# 中所有内容都打包在类中,而所有的类又打包在命名空间中(正如文件存与文件夹中)。和 C++ 一样,有一个主函数作为你程序的入口点。C++ 的主函数名为 main,而 C# 中是大写 M 打头的 Main。类块或结构定义之后没有必要再加一个半角分号。C++ 中是这样,但 C# 不要求。

命名空间

每个类都打包于一个命名空间。可以用点(.)定界符访问命名空间中的类。

Using:比如using System。System 是最基层的命名空间,所有其他命名空间和类都包含于其中。System 命名空间中所有对象的基类是 Object。

变量

1.    C# 中(不同于 C++)的变量,总是需要你在访问它们前先进行初始化,否则你将遇到编译时错误。故而,不可能访问未初始化的变量。
2.    你不能在 C# 中访问一个“挂起”指针。
3.    超出数组边界的表达式索引值同样不可访问。
4.    C# 中没有全局变量或全局函数,取而代之的是通过静态函数和静态变量完成的。

数据类型
所有 C# 的类型都是从 object 类继承的。有两种数据类型:1. 基本/内建类型   2.  用户定义类型

C# 内建类型的列表

类型        字节        描述
byte        1          unsigned byte
sbyte      1          signed byte
short      2          signed short
ushort      2          unsigned short
int        4          signed integer
uint        4          unsigned integer
long        8          signed long
ulong      8          unsigned long
float      4          floating point number
double      8          double precision number
decimal    8          fixed precision number
string      -          Unicode string
char        -          Unicode char
bool        true, false boolean
用户定义类型文件包含:
1.    类 (class)
2.    结构(struct)
3.    接口(interface)

注意:以下类型继承时均分配内存:1. 值类型 2.  参考类型。

值类型
值类型是在堆栈中分配的数据类型。它们包括了:
•    除字符串,所有基本和内建类型
•    结构
•    枚举类型
引用类型
引用类型在堆(heap)中分配内存且当其不再使用时,将自动进行垃圾清理。
引用类型包括:
•    类
•    接口
•    集合类型如数组
•    字符串

枚举

通过关键字 enum 定义。比如:

  1. enum Weekdays
  2. {
  3.     Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday
  4. }

类与结构

类的对象在堆中分配,并使用 new 关键字创建。而结构是在栈(stack)中进行分配。C# 中的结构属于轻量级快速数据类型。当需要大型数据类型时,你应该创建类。

  1. struct Date
  2. {
  3.     int day;
  4.     int month;
  5.     int year;
  6. }
  7.         
  8. class Date
  9. {
  10.     int day;
  11.     int month;
  12.     int year;
  13.     string weekday; 
  14.     string monthName;
  15.     public int GetDay()
  16.     { 
  17.         return day;
  18.     }
  19.     public int GetMonth() 
  20.     { 
  21.         return month;
  22.     }
  23.     public int GetYear() 
  24.     { 
  25.         return year;
  26.     }
  27.     public void SetDay(int Day) 
  28.     { 
  29.         day = Day ;
  30.     }
  31.     public void SetMonth(int Month)
  32.     {
  33.         month = Month;
  34.     }
  35.     public void SetYear(int Year)
  36.     { 
  37.         year = Year;
  38.     }
  39.     public bool IsLeapYear()
  40.     {
  41.         return (year/4 == 0);
  42.     }
  43.     public void SetDate (int day, int month, int year)
  44.     {
  45.     }
  46.     ...
  47. }

属性

C# 提供了一种更加便捷、简单而又直接的属性访问方式。上述代码改成:

  1. using System;
  2. class Date
  3. {
  4.     public int Day{
  5.         get {
  6.             return day; 
  7.         }
  8.         set {
  9.             day = value; 
  10.         }
  11.     }
  12.     int day;
  13.     public int Month{
  14.         get { 
  15.             return month;
  16.         }
  17.         set {
  18.             month = value; 
  19.         }
  20.     }
  21.     int month;
  22.     public int Year{
  23.         get { 
  24.             return year;
  25.         }
  26.         set {
  27.             year = value;
  28.         }
  29.     }
  30.     int year;
  31.     public bool IsLeapYear(int year)
  32.     {
  33.         return year%4== 0 ? truefalse
  34.     }
  35.     public void SetDate (int day, int month, int year)
  36.     {
  37.         this.day = day;
  38.         this.month = month;
  39.         this.year = year;
  40.     }
  41. }

 

  1. class User
  2. {
  3.   public static void Main()
  4.   { 
  5.         Date date = new Date(); 
  6.         date.Day = 27; 
  7.         date.Month = 6; 
  8.         date.Year = 2003;
  9.         Console.WriteLine
  10.         ("Date: {0}/{1}/{2}", date.Day, date.Month, date.Year);
  11.     }
  12. }

修饰符

常用的 public、private 和 protected 修饰符。这里讨论一些 C# 引入的新的修饰符。

readonly
readonly 修饰符仅用于修饰类的数据成员。正如其名字说的,一旦它们已经进行了写操作、直接初始化或在构造函数中对其进行了赋值,readonly 数据成员就只能对其进行读取。readonly 和 const 数据成员不同之处在于 const 要求你在声明时进行直接初始化。看下面的代码:

  1. class MyClass
  2. {
  3.     const int constInt = 100; //直接进行
  4.     readonly int myInt = 5; //直接进行
  5.     readonly int myInt2;
  6.     
  7.     public MyClass()
  8.     {
  9.         myInt2 = 8;        //间接进行
  10.     }
  11.     public Func()
  12.     {
  13.         myInt = 7; //非法
  14.         Console.WriteLine(myInt2.ToString());
  15.     }
  16. }

sealed
带有 sealed 修饰符的类不允许你从它继承任何类。所以如果你不想一个类被继承,你可以对该类使用 sealed 关键字。
代码:

  1. sealed class CanNotbeTheParent
  2. {
  3.     int a = 5;
  4. }

unsafe
使用 unsafe 修饰符在 C# 中定义一个不安全上下文。在不安全上下文中,你可以插入不安全代码,如 C++ 的指针等。代码:

  1. public unsafe MyFunction( int * pInt, double* pDouble)
  2. {
  3.     int* pAnotherInt = new int;
  4.     *pAnotherInt  = 10;
  5.     pInt = pAnotherInt;
  6.     ...
  7.     *pDouble = 8.9;    
  8. }

接口

如果你有 COM 的思想,你马上就知道我在说什么了。接口是只包含函数签名而在子类中实现的抽象基类。在 C# 中,你可以用 interface 关键字声明这样的接口类。.NET 就是基于这样的接口的。C# 中你不能对类进行多重继承——这在 C++ 中是允许的。通过接口,多重继承的精髓得以实现。即你的子类可以实现多重接口。比如:

  1. using System;
  2. interface myDrawing
  3. {
  4.     int originx
  5.     {
  6.         get;
  7.         set;
  8.     }
  9.     int originy
  10.     {
  11.         get;
  12.         set;
  13.     }
  14.     void Draw(object shape);            
  15. }
  16. class Shape: myDrawing
  17. {
  18.     int OriX;
  19.     int OriY;
  20.     
  21.     public int originx
  22.     {
  23.         get{
  24.             return OriX;
  25.         }
  26.         set{
  27.             OriX = value;
  28.         }
  29.     }
  30.     public int originy
  31.     {
  32.         get{
  33.             return OriY;
  34.         }
  35.         set{
  36.             OriY = value;
  37.         }
  38.     }
  39.     public void Draw(object shape)
  40.     {
  41.         ... // 做要做的事 
  42.     }
  43.     
  44.     // 类自身的方法
  45.     public void MoveShape(int newX, int newY)
  46.     {
  47.     .....
  48.     }   
  49.     
  50. }

数组

数组分配于堆中,所以是引用类型的。你不能访问数组边界外的元素。所以 C# 防止你引发那种 bug。同时也提供了迭代数组元素的帮助函数。foreach 是这样的迭代语句之一。

特点:

(1)方括号在类型后面而不是在变量名后面
(2)创建元素使用 new 运算符

(3 )C# 支持一维、多维和交错数组(数组的数组)

比如:

  1. int[] array = new int[10]; // int 型一维数组
  2. for (int i = 0; i < array.Length; i++) 
  3.     array = i; 
  4. int[,] array2 = new int[5,10]; // int 型二维数组
  5. array2[1,2] = 5;
  6. int[,,] array3 = new int[5,10,5]; // int 型三维数组
  7. array3[0,2,4] = 9;
  8. int[][] arrayOfarray = new int[2]; // int 型交错数组 - 数组的数组
  9. arrayOfarray[0] = new int[4]; 
  10. arrayOfarray[0] = new int[] {1,2,15};

索引器

索引器用于书写一个可以通过使用 [] 像数组一样直接访问集合元素的方法。你所需要的只是指定待访问实例或元素的索引。索引器的语法和类属性语法相同,除了接受作为元素索引的输入参数外。

比如:

  1. 注意:CollectionBase 是用于建立集合的库类。List 是 CollectionBase 中用于存放集合列表的受保护成员。
  2. class Shapes: CollectionBase 
  3.     public void add(Shape shp)
  4.     { 
  5.         List.Add(shp);
  6.     }
  7.     //indexer
  8.     public Shape this[int index]
  9.     { 
  10.         get { 
  11.             return (Shape) List[index];
  12.         } 
  13.         set {
  14.             List[index] = value ;
  15.         }
  16.     }
  17. }

装箱/拆箱

装箱的思想在 C# 中是创新的。正如前面提到的,所有的数据类型,无论是内建的还是用户定义的,都是从 System 命名空间的基类 object 继承的。所以基础的或是原始的类型打包为一个对象称为装箱,相反的处理称为拆箱。例子:

  1. class Test
  2. {
  3.   static void Main() 
  4.   {
  5.       int myInt = 12;
  6.       object obj = myInt ;      // 装箱
  7.       int myInt2 = (int) obj;  // 拆箱
  8.   }
  9. }

例程展示了装箱和拆箱两个过程。一个 int 值可以被转换为对象,并且能够再次转换回 int。当某种值类型的变量需要被转换为一个引用类型时,便会产生一个对象箱保存该值。拆箱则完全相反。当某个对象箱被转换回其原值类型时,该值从箱中拷贝至适当的存储空间。
函数参数
C# 中的参数有三种类型:
1.    按值传递/输入参数
2.    按引用传递/输入-输出参数
3.    输出参数
如果你有 COM 接口的思想,而且还是参数类型的,你会很容易理解 C# 的参数类型。
按值传递/输入参数:传递的值复制到了新的地方并传递给函数。例子:

  1. SetDay(5);
  2. ...
  3. void SetDay(int day) 
  4.     ....
  5. }

按引用传递/输入-输出参数:可以传递一个引用地址,传递一个输入的值并通过函数得到一个输出的值。因此引用参数也被称为输入-输出参数。不能将未初始化的引用参数传递给函数。C# 使用关键字 ref 指定引用参数。同时还必须在传递参数给要求引用参数的函数时使用关键字 ref。例子:

  1. int a= 5;
  2. FunctionA(ref a); // 使用 ref,否则将引发编译时错误
  3. Console.WriteLine(a); // 打印 20


  1. void FunctionA(ref int Val)
  2. {
  3.     int x= Val; 
  4.     Val = x* 4;    
  5. }

输出参数:出参数是只从函数返回值的参数。输入值不要求。C# 使用关键字 out 表示输出参数。例子:

  1. int Val;
  2. GetNodeValue(Val);


  1. bool GetNodeValue(out int Val)
  2.     {
  3.         Val = value;
  4.         return true
  5.     }

参数和数组的数量变化
C# 中的数组使用关键字 params 进行传递。一个数组类型的参数必须总是函数最右边的参数。只有一个参数可以是数组类型。你可以传送任意数量的元素作为数组类型的参数。看了下面的例子你可以更好的理解:
注意:使用数组是 C# 提供用于可选或可变数量参数的唯一途径。代码:

  1. void Func(params int[] array)
  2.     {
  3.         Console.WriteLine("number of elements {0}", array.Length);
  4.     }
  1. Func(); // 打印 0
  2.     Func(5); // 打印 1
  3.     Func(7,9); // 打印 2
  4.     Func(new int[] {3,8,10}); // 打印 3
  5.     int[] array = new int[8] {1,3,4,5,5,6,7,5};
  6.     Func(array); // 打印 8

运算符与表达式
is 运算符:s 运算符是用于检查操作数类型是否相等或可以转换。is 运算符特别适合用于多态的情形。is 运算符使用两个操作数,其结果是布尔值。参考例子:

  1. void function(object param)
  2.     if(param is ClassA)
  3.         //做要做的事
  4.     else if(param is MyStruct)
  5.         //做要做的事        
  6.     }
  7. }

as 运算符
as 运算符检查操作数的类型是否可转换或是相等(as 是由 is 运算符完成的),如果是,则处理结果是已转换或已装箱的对象(如果操作数可以装箱为目标类型,参考 装箱/拆箱)。如果对象不是可转换的或可装箱的,返回值为 null。看看下面的例子以更好的理解这个概念。

  1. Shape shp = new Shape(); 
  2. Vehicle veh = shp as Vehicle; // 返回 null,类型不可转换
  3. Circle cir = new Circle(); 
  4. Shape shp = cir; 
  5. Circle cir2 = shp as Circle;  //将进行转换
  6. object[] objects = new object[2];
  7. objects[0] = "Aisha";
  8. object[1] = new Shape();
  9. string str;
  10. for(int i=0; i&< objects.Length; i++)
  11. {
  12.     str = objects as string;
  13.     if(str == null)
  14.         Console.WriteLine("can not be converted");
  15.     else
  16.         Console.WriteLine("{0}",str);
  17. }

代码:
Output:
Aisha
can not be converted
语句

foreach:用于迭代数组等集合。
代码:
foreach (string s in array)
        Console.WriteLine(s);
lock
在线程中使代码块称为重点部分。
(译注:lock 关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁。lock 确保当一个线程位于代码的临界区时,另一个线程不进入临界区。如果其他线程试图进入锁定的代码,则它将一直等待(即被阻止),直到该对象被释放。)
checked/unchecked:用于数字操作中的溢出检查。

  1. int x = Int32.MaxValue; x++; // 溢出检查 
  2. x++; // 异常
  3. }
  4. unchecked
  5. {
  6. x++; // 溢出
  7. }

Switch:
1.现在在执行一条 case 语句后,程序流不能跳至下一 case 语句。之前在 C++ 中这是可以的。

  1. int var = 100;
  2. switch (var) 
  3.     case 100: Console.WriteLine("<Value is 100>"); // 这里没有 break 
  4.     case 200: Console.WriteLine("<Value is 200>"); break
  5. }

2.然而你可以像在 C++ 中一样这么用:

  1. switch (var) 
  2. {
  3.     case 100: 
  4.     case 200: Console.WriteLine("100 or 200<VALUE is 200>"); break
  5. }

3.你还可以用常数变量作为 case 值:

  1. const string WeekEnd = "Sunday";
  2. const string WeekDay1 = "Monday";
  3. ....
  4. string WeekDay = Console.ReadLine();
  5. switch (WeekDay ) 
  6. case WeekEnd: Console.WriteLine("It's weekend!!"); break
  7. case WeekDay1: Console.WriteLine("It's Monday"); break;
  8. }

委托
委托让我们可以把函数引用保存在变量中。这就像在 C++ 中使用 typedef 保存函数指针一样。委托使用关键字 delegate 声明。看看这个例子,你就能理解什么是委托:


 

  1. delegate int Operation(int val1, int val2);
  2. public int Add(int val1, int val2) 
  3.     return val1 + val2; 
  4. }
  5. public int Subtract (int val1, int val2) 
  6.     return val1- val2;
  7. }
  8. public void Perform()
  9. {
  10.     Operation Oper;
  11.     Console.WriteLine("Enter + or - ");
  12.     string optor = Console.ReadLine();
  13.     Console.WriteLine("Enter 2 operands");
  14.             
  15.     string opnd1 = Console.ReadLine();
  16.     string opnd2 = Console.ReadLine();
  17.             
  18.     int val1 = Convert.ToInt32 (opnd1);            
  19.     int val2 = Convert.ToInt32 (opnd2);
  20.             
  21.     if (optor == "+")
  22.         Oper = new Operation(Add);
  23.     else
  24.         Oper = new Operation(Subtract);
  25.         
  26.     Console.WriteLine(" Result = {0}", Oper(val1, val2));
  27. }

继承与多态:C# 只允许单一继承。多重继承可以通过接口达到。
代码:
class Parent{
}

class Child : Parent
虚函数:虚函数在 C# 中同样是用于实现多态的概念的,除了你要使用 override 关键字在子类中实现虚函数外。父类使用同样的 virtual 关键字。每个重写虚函数的类都使用 override 关键字。

  1. class Shape
  2. {
  3.     public virtual void Draw()
  4.     {
  5.         Console.WriteLine("Shape.Draw")    ;
  6.     }
  7. }
  8. class Rectangle : Shape
  9. {
  10.     public override void Draw()
  11.     {
  12.         Console.WriteLine("Rectangle.Draw");
  13.     }            
  14. }
  15. class Square : Rectangle
  16. {
  17.     public override void Draw()
  18.     {
  19.         Console.WriteLine("Square.Draw");
  20.     }
  21. }
  22. class MainClass
  23. {
  24.     static void Main(string[] args)
  25.     {
  26.         Shape[] shp = new Shape[3];
  27.         Rectangle rect = new Rectangle();
  28.         
  29.         shp[0] = new Shape();
  30.         shp[1] = rect;
  31.         shp[2] = new Square();
  32.                     
  33.         shp[0].Draw();
  34.         shp[1].Draw();
  35.         shp[2].Draw();
  36.     }
  37. }
  38. Output:
  39. Shape.Draw
  40. Rectangle.Draw
  41. Square.Draw

使用“new”隐藏父类函数:可以隐藏基类中的函数而在子类中定义其新版本。关键字 new 用于声明新的版本。思考下面的例子,该例是上一例子的修改版本。注意输出,我用 关键字 new 替换了 Rectangle 类中的关键字 override。

  1. class Shape
  2. {
  3.     public virtual void Draw()
  4.     {
  5.         Console.WriteLine("Shape.Draw")    ;
  6.     }
  7. }
  8. class Rectangle : Shape
  9. {
  10.     public new void Draw()
  11.     {
  12.         Console.WriteLine("Rectangle.Draw");
  13.     }            
  14. }
  15. class Square : Rectangle
  16. {
  17.     //这里不用 override
  18.     public new void Draw() 
  19.     {
  20.         Console.WriteLine("Square.Draw");
  21.     }
  22. }
  23. class MainClass
  24. {
  25.     static void Main(string[] args)
  26.     {
  27.         Console.WriteLine("Using Polymorphism:");
  28.         Shape[] shp = new Shape[3];
  29.         Rectangle rect = new Rectangle();
  30.             
  31.         shp[0] = new Shape();
  32.         shp[1] = rect;
  33.         shp[2] = new Square();
  34.                         
  35.         shp[0].Draw();
  36.         shp[1].Draw();
  37.         shp[2].Draw();
  38.             
  39.         Console.WriteLine("Using without Polymorphism:");
  40.         rect.Draw();            
  41.         Square sqr = new Square();
  42.         sqr.Draw();
  43.     }
  44. }    
  45.     
  46. Output:
  47. Using Polymorphism
  48. Shape.Draw
  49. Shape.Draw
  50. Shape.Draw
  51. Using without Polymorphism:
  52. Rectangle.Draw
  53. Square.Draw

多态性认为 Rectangle 类的 Draw 方法是和 Shape 类的 Draw 方法不同的另一个方法,而不是认为是其多态实现。所以为了防止父类和子类间的命名冲突,我们只有使用 new 修饰符。
注意:不能在一个类中使用一个方法的两个版本,一个用 new 修饰符,另一个用 override 或 virtual。就像在上面的例子中,我不能在 Rectangle 类中增加另一个名为 Draw 的方法,因为它是一个 virtual 或 override 的方法。同样在 Square 类中,我也不能重写 Shape 类的虚方法 Draw。
调用基类成员
如果子类的数据成员和基类中的有同样的名字,为了避免命名冲突,基类成员和函数使用 base 关键字进行访问。看看下面的例子,基类构造函数是如何调用的,而数据成员又是如何使用的。

  1. public Child(int val) :base(val)
  2. {
  3.     myVar = 5;
  4.     base.myVar;
  5. }
  6. OR
  7. public Child(int val)
  8. {
  9.     base(val);
  10.     myVar = 5 ;
  11.     base.myVar;
  12. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值