在Roslyn Pattern Matching spec它指出:模式匹配變量範圍
的圖案可變的範圍如下:
如果圖案出現在一個if語句的條件下,它的範圍 是if語句的條件和受控語句,但不包含其他語句。
但是微軟最新的「最新消息」 posts和presentations都出現了這個例子:
public void PrintStars(object o)
{
if (o is null) return; // constant pattern "null"
if (!(o is int i)) return; // type pattern "int i"
WriteLine(new string('*', i));
}
足見模式匹配外的模式匹配的,如果水平範圍使用i
變量。
這是一個疏忽,還是已經從規範改變範圍?
這將是if語句的_controlled語句_ – juharr
@juharr,那麼受控語句只會返回嗎? –
是的,我錯過了。實際上,我認爲這可能是指更高範圍的「i」,但很難說,因爲它是測試版功能的代碼片段。我想你可以預覽並測試它。 – juharr