国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# 4.0新特性

2019-11-17 04:02:10
字體:
來源:轉載
供稿:網友
 回顧C#發展的歷史,C#1.0完全是模仿java,并保留了C/C++的一些特性如struct,新學者很容易上手;C#2.0加入了泛型,也與Java1.5的泛型如出一轍;C#3.0加入了一堆語法糖,并在沒有修改CLR的情況下引入了Linq,簡直是神來之筆,雖然很多項目出于各種各樣如性能之類的原因沒有采用,但非常適合小型程序的快速開發,減輕了程序員的工作量,也提高了代碼的可讀性;C#4.0增加了動態語言的特性,從里面可以看到很多javascript、python這些動態語言的影子。雖然越來越偏離靜態語言的道路,但從另一個角度來說,這些特性也都是為了提高程序員的生產力。至于被接受與否,還是讓時間來說話吧。



PS:這里面還有一點版本號的小插曲——VS2008所對應的.Net Framework是3.5,C#是3.0,CLR是2.0,及其混亂,MS終于下決心在VS2010中把這三個版本號都統一成了4.0,于是CLR3不知所終……

Dynamically Typed Object
C#4.0加入了dynamic關鍵字,可以申明一個變量的static類型為dynamic(有點繞口)。

在3.0及之前,如果你不知道一個變量的類型,而要去調用它的一個方法,一般會用到反射:

object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add",
BindingFlags.InvokeMethod, null,
new object[] { 10, 20 });
int sum = Convert.ToInt32(res);


有了dynamic,就可以把上面代碼簡化為:

dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);


使用dynamic的好處在于,可以不去關心對象是來源于COM, IronPython, HTML DOM或者反射,只要知道有什么方法可以調用就可以了,剩下的工作可以留給runtime。下面是調用IronPython類的例子:

ScriptRuntime py = Python.CreateRuntime();
dynamic helloworld = py.UseFile("helloworld.py");
Console.WriteLine("helloworld.py loaded!");

dynamic也可以用在變量的傳遞中,runtime會自動選擇一個最匹配的overload方法。
這里有一個demo:把一段Javascript代碼拷到C#文件中,將var改成dynamic,function改成void,再改一下構造函數的調用方式(new type()改為win.New.type()),去掉javascript中的win.前綴(因為這已經是C#的方法了),就可以直接運行了。

dynamic的實現是基于IDynamicObject接口和DynamicObject抽象類。而動態方法、屬性的調用都被轉為了GetMember、Invoke等方法的調用。

public abstract class DynamicObject : IDynamicObject
{
public virtual object GetMember(GetMemberBinder info);
public virtual object SetMember(SetMemberBinder info, object value);
public virtual object DeleteMember(DeleteMemberBinder info); public virtual object UnaryOperation(UnaryOperationBinder info);
public virtual object BinaryOperation(BinaryOperationBinder info, object arg);
public virtual object Convert(ConvertBinder info); public virtual object Invoke(InvokeBinder info, object[] args);
public virtual object InvokeMember(InvokeMemberBinder info, object[] args);
public virtual object CreateInstance(CreateInstanceBinder info, object[] args); public virtual object GetIndex(GetIndexBinder info, object[] indices);
public virtual object SetIndex(SetIndexBinder info, object[] indices, object value);
public virtual object DeleteIndex(DeleteIndexBinder info, object[] indices); public MetaObject IDynamicObject.GetMetaObject();
}




Named and optional parameters
這似乎不是什么很難實現或很新穎的特性,只要編譯器的支持就可以(VB很早就支持了)。估計加入的原因是群眾的呼聲太高了。

帶有可選參數方法的聲明:

public StreamReader OpenTextFile(
string path,
Encoding encoding = null,
bool detectEncoding = true,
int bufferSize = 1024);


命名參數必須在最后使用:

OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096);


順序不限:

OpenTextFile(bufferSize: 4096, path: "foo.txt", detectEncoding: false);


ImPRoved COM Interoperability
在C#中在調用COM對象如office對象時,經常需要寫一堆不必要的參數:

object fileName = "Test.docx";
object missing = System.Reflection.Missing.Value;
doc.SaveAs(ref fileName,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);


4.0中就可以直接寫成:

doc.SaveAs("Test.docx");


C#4.0對COM交互做了下面幾方面的改進:

Automatic object -> dynamic mapping
Optional and named parameters
Indexed properties
Optional “ref” modifier
Interop type embedding (“No PIA”)
對第1點和第5點的簡單解釋如下:

在COM調用中,很多輸入輸出類型都是object,這樣就必須知道返回對象的確切類型,強制轉換后才可以調用相應的方法。在4.0中有了dynamic的支持,就可以在導入這些COM接口時將變量定義為dynamic而不是object,省掉了強制類型轉換。

PIA(Primary Interop Assemblies)是根據COM API生成的.Net Assembly,一般體積比較大。在4.0中運行時不需要PIA的存在,編譯器會判斷你的程序具體使用了哪一部分COM API,只把這部分用PIA包裝,直接加入到你自己程序的Assembly里面。

Co- and Contra-Variance
實在是不知道怎么翻譯這兩個詞。

在C#中,下面的類型轉換是非法的:

IList strings = new List();
IListobjects = strings;


因為你有可能會這樣做,而編譯器的靜態檢查無法查出錯誤:

objects[0] = 5;
string s = strings[0];


4.0中在聲明generic的Interface及Delegate時可以加in及out關鍵字,如:

public interface IEnumerable : IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator : IEnumerator
{
bool MoveNext();
T Current { get; }
}

public interface IComparer
{
public int Compare(T left, T right);
}


out關鍵字的意思是說IEnumerable中T只會被用在輸出中,值不會被改變。這樣將IEnumerable轉為IEnumerable類型就是安全的。

in的意思正好相反,是說IComparer中的T只會被用在輸入中,這樣就可以將IComparer安全的轉為IComparer類型。

前者被稱為Co-Variance, 后者就是Contra-Variance。

.Net4.0中使用out/in聲明的Interface:

System.Collections.Generic.IEnumerable
System.Collections.Generic.IEnumerator
System.Linq.IQueryable
System.Collections.Generic.IComparer
System.Collections.Generic.IEqualityComparer
System.IComparable


Delegate:

System.Func
System.Action
System.Predicate
System.Comparison
System.EventHandler


Compiler as a Service
4.0中增加了與編譯器相關的API,這樣就可以將字符串作為代碼動態編譯執行,跟javascript好像。

Video的最后,Anders做了一個很酷的demo,大概只用了二三十行代碼,就實現了在控制臺中直接執行C#語句,定義并調用函數,動態創建windows form,添加button等功能,看起來完全不遜色于Python,Ruby之類語言的控制臺
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米脂县| 荆门市| 延川县| 潍坊市| 高陵县| 贡觉县| 甘南县| 时尚| 义马市| 谢通门县| 元氏县| 昭苏县| 家居| 安溪县| 庐江县| 莆田市| 沙河市| 岗巴县| 白河县| 鄯善县| 文成县| 高尔夫| 北川| 高陵县| 都匀市| 灌云县| 福海县| 景德镇市| 赤壁市| 东城区| 珲春市| 兴海县| 县级市| 正阳县| 湖南省| 伊宁县| 马关县| 高安市| 高阳县| 韶山市| 连城县|