第一个工作正在进行,而第二个显示错误,有什么区别? 我阅读文档,并没有发现任何关于它,它不是那么重要,但要知道的一些功能显式转换和安全转换之间的差异#
public static string GetConcat2<T>(T q)
{
if(q.GetType() == typeof(A))
{
var z = q as A;
}
if (q.GetType() == typeof(A))
{
var z = (A)q;
}
return "!!!!";
}
public interface CustomInteface
{
string InterfaceProperty1 { get; set; }
string InterfaceProperty2 { get; set; }
string Concat();
}
public class A : CustomInteface
{
public string InterfaceProperty1 { get; set; }
public string InterfaceProperty2 { get; set; }
public string Concat() {
return InterfaceProperty1 + InterfaceProperty2;
}
}
什么是错误,它在哪里抛出错误?尝试提供重现问题所需的所有信息和代码,包括[MCVE]。 – TheLethalCoder
@ TheLethalCoder var z =(A)q; 这里有一个错误,它无法强制类型A – GodlikeRabbit