Why use TryCast instead of DirectCast?

后端 未结 4 490
粉色の甜心
粉色の甜心 2020-12-03 02:53

When I am trying to cast Object obj to Type T, if it can not be cast then there is something wrong.

And after I cast the object I will be l

4条回答
  •  心在旅途
    2020-12-03 03:42

    (For C# developers, TryCast is similar to "as" and DirectCast is the equivalent of normal casting. As Mike pointed out in the comments, "as" works for nullable value types, but TryCast doesn't.)

    If the value really should be a T, then DirectCast is indeed the right way to go - it fails fast, with an appropriate error.

    TryCast is appropriate when it's legitimate for the target to be the "wrong" type. For instance, to get all the Button controls in a container, you could go through the control collection and try to cast each to Button. If it works, you do something with it - if it doesn't, you move on. (With LINQ you can just use OfType for this purpose, but you see what I mean...)

    In my experience direct casting is appropriate more often than TryCast - although with generics I find myself casting a lot less often than I used to anyway.

提交回复
热议问题