質問

Given the line:

let win = XamlReader.Parse(xaml) :?> Window

What is :?> doing?

I tried looking up ternary but not noticing the :?, it seems to be for type testing?. Also I know > is piping but Window does not have any constructor parameters.

役に立ちましたか?

解決

It is the dynamic cast operator which has nothing to do with piping. I quote MSDN section on downcasting:

The :?> operator performs a dynamic cast, which means that the success of the cast is determined at run time. A cast that uses the :?> operator is not checked at compile time; but at run time, an attempt is made to cast to the specified type. If the object is compatible with the target type, the cast succeeds. If the object is not compatible with the target type, the runtime raises an InvalidCastException.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top