C#学习笔记

来源:互联网 发布:js闭包使用 编辑:程序博客网 时间:2024/04/30 05:08
C#学习笔记

1.key word:delegate

2.System.Exception
System

try{}//place your code which my throw exception in try block
catch(Exception ex){}//catch excetpion
finally{}//allow cleaning up and other code execute regardless of exception is thrown.

try和catch必须一起出现,finally可以不要
一个try block 可以有多个catch,越是特殊的异常捕捉越往最前放
如果某段代码有可能throw Exception 就应该把该段代码放入 try block但编译器不强制要求的
如果某段代码有可能throw Exception 而有没有相应的catch(YourException ex)或catch(Exception ex),system 会向搜索上一层的catch,直到知道为止
如果到top层还是没有找到的话,异常处理程序会自己处理,并结束应用程序。

和Java异常处理不同的是Csharp里没有 throws关键字,编译器不强行检查是否捕捉异常


3 Method
value parameter, can't chang the value of paramter it will pass a copy of parameter into a function.
值传递:是在堆栈上建立一份参数拷贝,所有对参数的操作都是对copy的操作。

引用类型:传递的是引用类型的地址,对于referance Type 默认传递的是 地址
如果对于ref+referanceType传递的是const referance

4 Destructor

GC 作内存清理
Destructor关闭文件,断开数据库等

5 参数传递
Parameter 参数
必须 加param
放在最后一个,实参为数组a 或是多个数据值1,2,3

ref参数//------------------------------------------------
和 value parameter不同,编译器不会为ref parameter再分配新的存储空间,即使用new 新申请一块都不行(但编译器不会报错)。

对于referance Type 不使用ref关键字仍是按引用传递(在C#中所有referance Tyep 都是通过referance来携带的),但可以给该引用指向新的空间。

out 参数
因为ref 参数和 value 参数都不能改变实参值,但有时候这种改变又是很有用的就引入了out 参数。用来改变传入的参数值。

6 static constructor

The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain.

* An instance of the class is created.

* Any of the static members of the class are referenced.

7
new keyword
Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier
用来隐藏继承中,父类或父interface的同名方法,仅仅是隐藏
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override

8
abstract and virtual Method
Although an abstract method is implicitly also a virtual method, it cannot have the modifier virtual.
nonabstract derived classes are required to provide their own implementation by overriding that method.

9
inherit
当基类的构造器是私有时,不能被继承,或者通过其非私有构造器继承。abstract default类构造器是破rtected

10
interface
Explicity impliment:实现借口的隐藏,只能通过接口访问。
//HideInterfaceMember
using System;
namespace NCS.Csharp.Test
{
    class HideInterfaceMember
    {
        interface CanFly
        {
            void Fly();
        }
       
        interface CanSwim
        {
            void Swim();
        }
       
        interface WaterBired:CanFly,CanSwim
        {
            new void Swim();
        }
        class WB:WaterBired
        {
            void WaterBired.Swim()
            {
                Console.WriteLine("WaterBired Can Swim");
            }
           
            public void Fly()
            {
                Console.WriteLine("WB Can Fly");
            }
           
            void CanSwim.Swim()
            {
                Console.WriteLine("Can Swim Can Swim");
            }
        }   
        public static void Main()
        {
            WB wb= new WB();
            WaterBired waterBired=wb;
            CanSwim cs=wb;
            //!wb.Swim();//error 不能通过类访问,Swim()只是Explicity impliment
            wb.Fly(); // WB Can Fly
            waterBired.Swim();//WaterBired Can Swim
            cs.Swim();//Can Swim Can Swim
        }
    }
}