C# 中的对象转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1854412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:24:49  来源:igfitidea点击:

Object Casting in C#

c#

提问by Agamand The True

what is the difference as well as the pros and cons between

有什么区别以及优缺点

 LinkButton lb = (LinkButton)ctl;

and

 LinkButton lb = ctl as LinkButton;

I tried using the first one and it gives me error, then I tried the other one with the keyword as, it work just fine.

我尝试使用第一个,但它给了我错误,然后我用关键字 as 尝试了另一个,它工作得很好。

Thank You in Advance.

先感谢您。

采纳答案by AaronLS

The first is an explicit cast, and the second is a conversion. If the conversion fails for the askeyword, it will simply return nullinstead of throwing an exception.

第一个是显式转换,第二个是转换。如果as关键字的转换失败,它将简单地返回null而不是抛出异常。

This is the documentation for each:

这是每个的文档:

Note in the linked documentation above, they state the askeyword does not support user-defined conversions. +1 to Zxpro :) This is what a user-defined conversion is:

请注意,在上面的链接文档中,他们声明as关键字不支持用户定义的转换。+1 到 Zxpro :) 这就是用户定义的转换:

User-Defined Conversions Tutorial

用户定义的转换教程

回答by Crowe T. Robot

I believe that casting using the first method throws an exception if it can't cast the object properly (trying to cast the wrong type), whereas using the as keyword will simply set the variable to null if it couldn't cast it properly.

我相信,如果使用第一种方法无法正确转换对象(尝试转换错误类型),则使用第一种方法进行转换会引发异常,而如果无法正确转换,则使用 as 关键字只会将变量设置为 null。

So make sure that if you use the as keyword cast, you check

因此,请确保如果您使用 as 关键字强制转换,请检查

if(lb == null)
    return null; // or throw new Exception()

and if you use the () cast, you surround it with

如果你使用 () 演员表,你用

try
{
    LinkButton lb = (LinkButton)ctl;
}
catch(InvalidCastException ex)
{
    //TODO: Handle Exception
}

回答by ALOR

The second one is called safe cast, which, instead of throwing exception, will put "null" to your variable. So it does NOT work fine, but sets your LinkButton lbto null

第二个称为安全转换,它不会抛出异常,而是将“null”放入您的变量中。所以它不能正常工作,但将您设置LinkButton lb为空

回答by bobbymcr

My usual guidance on using the asoperator versus a direct cast are as follows:

我对使用as运算符与直接强制转换的通常指导如下:

  1. If the cast mustsucceed (i.e. it would be an error to continue if the cast failed), use a direct cast.
  2. If the cast might fail and there needs to be programmatic detection of this, use the asoperator.
  1. 如果强制转换必须成功(即,如果强制转换失败,继续执行将是错误的),请使用直接强制转换。
  2. 如果转换可能会失败并且需要对此进行编程检测,请使用as运算符。

The above is true for reference types. For value types (like boolor int), asdoes not work. In that case, you will need to use an ischeck to do a "safe cast", like this:

以上适用于引用类型。对于值类型(如boolint),as不起作用。在这种情况下,您将需要使用is检查来执行“安全转换”,如下所示:

int y;
if (x is int)
{
    y = (int)x;
}
else
{
    // ...
}

I do not recommend trying to catch InvalidCastException, as this is generally the sign of a programmer error. Use the guidance above instead.

我不建议尝试 catch InvalidCastException,因为这通常是程序员错误的标志。请改用上述指南。