Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我刚刚遇到了另一种在 C# 中转换对象的方法。我一直用(CastType)variable. 我刚刚注意到一些使用variable as CastType. 后者让我想起了VB。
(CastType)variable
variable as CastType
除了语法之外,这两种转换方法之间有区别吗?
InvalidCastException如果类型不匹配,第一个将抛出(请参阅文档- “显式转换”部分)。第二个(as操作员)将产生null价值。
InvalidCastException
as
null