標籤:des style blog http color 使用
【C# Common Keyword II】
1、as 運算子用於在相容的參考型別之間執行某些類型的轉換。
class csrefKeywordsOperators { class Base { public override string ToString() { return "Base"; } } class Derived : Base { } class Program { static void Main() { Derived d = new Derived(); Base b = d as Base; if (b != null) { Console.WriteLine(b.ToString()); } } } }
View Code
as 運算子類似於強制轉換操作。但是,如果無法進行轉換,則 as 返回 null 而非引發異常。請看下面的運算式:
expression as type// 它等效於以下運算式,但只計算一次 expression。expression is type ? (type)expression : (type)null
參考:http://msdn.microsoft.com/zh-cn/library/cscsdfbt(v=vs.90).aspx
2、Metadata is binary information describing your program that is stored either in a CLR(common language runtime ) portable executable (PE) file or in memory.When you compile your code into a PE file, metadata is inserted into one portion of the file, while your code is converted to Microsoft intermediate language (MSIL) and inserted into another portion of the file.When code is executed, the runtime loads metadata into memory and references it to discover information about your code‘s classes, members, inheritance, and so on.
Metadata描述儲存於CRL-PE或儲存于于記憶體中的program。當編譯時間,metadata被插入到檔案中。當執行時,runtime載入metadata到內在,根據metadata來發現你的程式的資訊。
Metadata is the key to a simpler programming model, eliminating the need for Interface Definition Language (IDL) files, header files, or any external method of component reference. Metadata allows .NET languages to describe themselves automatically in a language-neutral manner, unseen by both the developer and the user.
參考:http://msdn.microsoft.com/en-us/library/xcd8txaw(v=vs.90).aspx
3、public static class A,靜態類。靜態類的主要功能如下:
它們僅包含靜態成員。
它們不能被執行個體化。
它們是密封的。
它們不能包含執行個體建構函式(C# 編程指南)。
因此建立靜態類與建立僅包含靜態成員和私人建構函式的類大致一樣。私人建構函式阻止類被執行個體化。使用靜態類的優點在於,編譯器能夠執行檢查以確保不致偶然地添加執行個體成員。編譯器將保證不會建立此類的執行個體。靜態類不能包含建構函式,但仍可聲明靜態建構函式以分配初始值或設定某個靜態狀態。
class SimpleClass{ // Static constructor static SimpleClass() { //... }}
View Code
參考:
1)http://msdn.microsoft.com/zh-cn/library/79b3xss3(VS.80).aspx
2)http://msdn.microsoft.com/zh-cn/library/k9x6w0hc(v=vs.80).aspx
4、is關鍵字用於判斷對象是否為從 MyObject 派生的一個類型:
class Class1 {}class Class2 {}class Class3 : Class2 { }class IsTest{ static void Test(object o) { Class1 a; Class2 b; if (o is Class1) { Console.WriteLine("o is Class1"); a = (Class1)o; // Do something with "a." } else if (o is Class2) { Console.WriteLine("o is Class2"); b = (Class2)o; // Do something with "b." } else { Console.WriteLine("o is neither Class1 nor Class2."); } } static void Main() { Class1 c1 = new Class1(); Class2 c2 = new Class2(); Class3 c3 = new Class3(); Test(c1); Test(c2); Test(c3); Test("a string"); }}/*Output:o is Class1o is Class2o is Class2o is neither Class1 nor Class2.*/
View Code
參考:http://msdn.microsoft.com/zh-cn/library/scekt9xw(v=vs.90).aspx
5、方法參數不能有預設值。如果要獲得同樣的效果,請使用方法重載。
// CS0241.cspublic class A{ public void Test(int i = 9) {} // CS0241}public class B{ public void Test() { Test(9); } public void Test(int i) {}}public class C{ public static void Main() { B x = new B(); x.Test(); }}
View Code
參考:http://msdn.microsoft.com/zh-cn/library/294000kk(v=vs.90).aspx
6、using有以下2個用途:
除此外,還有一個using語句的用法。using 指令的範圍限制為包含它的檔案。
參考:http://msdn.microsoft.com/zh-cn/library/sf0df423(v=vs.90).aspx