第一個工作正在進行,而第二個顯示錯誤,有什麼區別? 我閱讀文檔,並沒有發現任何關於它,它不是那麼重要,但要知道的一些功能顯式轉換和安全轉換之間的差異#
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