簡體   English   中英

為什么要創建沒有名稱的void類型的標簽目標?

[英]Why would you create a label target of type void with no name?

System.Linq.Expressions.Expression類中有一個特定的重載, System.Linq.Expressions.Expression所示:

public static LabelTarget Label();

文檔說: “創建一個LabelTarget表示一個帶有空類型且沒有名稱的標簽。”

那有什么意思? 為什么要創建一個沒有名稱的void類型的標簽目標?

文檔中可以看出,它就像沒有表達式的標簽一樣工作:

 // A label expression of the void type that is the target for Expression.Return(). LabelTarget returnTarget = Expression.Label(); // This block contains a GotoExpression that represents a return statement with no value. // It transfers execution to a label expression that is initialized with the same LabelTarget as the GotoExpression. // The types of the GotoExpression, label expression, and LabelTarget must match. BlockExpression blockExpr = Expression.Block( Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("Return")), Expression.Return(returnTarget), Expression.Call(typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }), Expression.Constant("Other Work")), Expression.Label(returnTarget) ); 

在這里,創建了一個標簽,然后在表達式塊的末尾對其進行了標記。 如果使用類型創建它,則在第二次調用Label時必須為其分配一個表達式。 因此,由於第二個標簽未分配表達式,因此類型必須為空。

至於名稱,標簽名稱不會被編譯,因此為方便起見,在不需要名稱時可以省略名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM