C#學習筆記總結(jié)
C#學習筆記之explicit 和 implicit 的含義?
explicit 和 implicit 屬于轉(zhuǎn)換運算符,如用這兩者可以讓我們自定義的類型支持相互交換,explicti 表示顯式轉(zhuǎn)換,如從 A -> B 必須進行強制類型轉(zhuǎn)換(B = (B)A),implicit 表示隱式轉(zhuǎn)換,如從 B -> A 只需直接賦值(A = B)
隱式轉(zhuǎn)換可以讓我們的代碼看上去更漂亮、更簡潔易懂,所以***多使用 implicit 運算符。不過!如果對象本身在轉(zhuǎn)換時會損失一些信息(如精度),那么我們只能使用 explicit 運算符,以便在編譯期就能警告客戶調(diào)用端
C#學習筆記之params 有什么用?
params 關(guān)鍵字在方法成員的參數(shù)列表中使用,為該方法提供了參數(shù)個數(shù)可變的能力,它在只能出現(xiàn)一次并且不能在其后再有參數(shù)定義,之前可以
示例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace ConsoleApplication1 {
- class App {
- //***個參數(shù)必須是整型,但后面的參數(shù)個數(shù)是可變的。
- //而且由于定的是object數(shù)組,所有的數(shù)據(jù)類型都可以做為參數(shù)傳入
- public static void UseParams(int id, params object[] list){
- Console.WriteLine(id);for (int i = 0; i < list.Length; i++){
- Console.WriteLine(list[i]);
- }
- static void Main(){
- //可變參數(shù)部分傳入了三個參數(shù),都是字符串類型UseParams(1, "a", "b", "c");
- //可變參數(shù)部分傳入了四個參數(shù),分別為字符串、整數(shù)、浮點數(shù)和雙精度浮點數(shù)數(shù)組
- UseParams(2, "d", 100, 33.33, new double[] { 1.1, 2.2 });
- Console.ReadLine();
- }
C#學習筆記之什么是反射?
反射,Reflection,通過它我們可以在運行時獲得各種信息,如程序集、模塊、類型、字段、屬性、方法和事件,通過對類型動態(tài)實例化后,還可以對其執(zhí)行操作
一般用于插件式框架程序和設(shè)計模式的實現(xiàn),當然反射是一種手段可以充分發(fā)揮其能量來完成你想做的任何事情(前面好象見過一位高人用反射調(diào)用一個官方類庫中未說明的函數(shù)……)
示例:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Example25Lib {
- public class Class1 {
- private string name;private int age;
- //如果顯式的聲明了無參數(shù)構(gòu)造函數(shù),客戶端只需要用程序集的CreateInstance即可實例化該類
- //在此特意不實現(xiàn),以便在客戶調(diào)用端體現(xiàn)構(gòu)造函數(shù)的反射實現(xiàn)//public Class1()
- }
- public Class1(string Name, int Age)
- { name = Name;age = Age;} public void ChangeName(string NewName)
- { name = NewName;} public void ChangeAge(int NewAge)
- { age = NewAge;} public override string ToString()
- { return string.Format("Name: {0}, Age: {1}", name, age);
- }
- using System;
- using System.Collections.Generic;
- using System.Text;
- //注意添加該反射的命名空間using System.Reflection;
- namespace Example25 { class Program { static void Main(string[] args)
- { //加載程序集Assembly tmpAss = Assembly.LoadFile
(AppDomain.CurrentDomain.BaseDirectory + "Example25Lib.dll");- //遍歷程序集內(nèi)所有的類型,并實例化Type[] tmpTypes = tmpAss.GetTypes();
- foreach (Type tmpType in tmpTypes)
- { //獲取***個類型的構(gòu)造函數(shù)信息ConstructorInfo[]
tmpConsInfos = tmpType.GetConstructors();- foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)
- { //為構(gòu)造函數(shù)生成調(diào)用的參數(shù)集合ParameterInfo[]
tmpParamInfos = tmpConsInfo.GetParameters();- object[] tmpParams = new object[tmpParamInfos.Length];
- for (int i = 0; i < tmpParamInfos.Length; i++)
- { tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
- if (tmpParamInfos[i].ParameterType.FullName == "System.String")
- { tmpParams[i] = "Clark";}
- //實例化對象object tmpObj = tmpConsInfo.Invoke(tmpParams);Console.WriteLine(tmpObj);
- //獲取所有方法并執(zhí)行foreach (MethodInfo tmpMethod in tmpType.GetMethods())
- { //為方法的調(diào)用創(chuàng)建參數(shù)集合tmpParamInfos = tmpMethod.GetParameters();
- tmpParams = new object[tmpParamInfos.Length];
- for (int i = 0; i < tmpParamInfos.Length; i++)
- { tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
- if (tmpParamInfos[i].ParameterType.FullName == "System.String")
- { tmpParams[i] = "Clark Zheng";
- } if (tmpParamInfos[i].ParameterType.FullName == "System.Int32")
- { tmpParams[i] = 27;} tmpMethod.Invoke(tmpObj, tmpParams);}
- //調(diào)用完方法后再次打印對象,比較結(jié)果Console.WriteLine(tmpObj);}
- Console.ReadLine();}
【編輯推薦】


















