【发布时间】:2017-08-30 19:47:23
【问题描述】:
namespace Rextester
{
public class BaseException : Exception
{
public BaseException() { }
}
public class Program
{
public static void MethodA(BaseException e)
{
Console.WriteLine("BaseException");
}
public static void MethodA(Exception e)
{
Console.WriteLine("Exception");
}
public static void Main(string[] args)
{
try
{
throw new BaseException();
}
catch (Exception e)
{
Console.WriteLine(e.GetType());
MethodA(e);
}
}
}
}
大家好,根据上述执行代码的结果,我有一个问题:
e.GetType() == Rextester.BaseException
MethodA 写入控制台:异常
因此,即使异常的类型是派生类,为什么在运行时不调用具有 BaseException 作为参数的特定重载方法并被称为带有异常的方法?
【问题讨论】:
-
你的问题不清楚。请添加更多描述
-
Idk 这里有什么不清楚的地方:o
标签: c# inheritance exception-handling overloading