简体   繁体   中英

Are the derived classes of `System.Exception` structurall equivalent to `System.Exception`?

i am looking at

Is it correct that all (or in the most cases) the derived classes of System.Exception , whether user defined or system defined, are structurally equivalent to their base class System.Exception , despite of them being non nominally equivalent ?

Thanks.

No. When I inherit from System.Exception I add new variables to the derived class. Some stock exceptions do the same.

Not Necessarily (and likely not at all). Any class that inherits off of System.Exception will, by definition, implement everything that System.Exception implements. However, it will also have its own differences.

Let's take a look at a simple example: ArgumentNullException. This is a System namespace exception, so was defined by the creators of C#. Comparing System.Exception to System.ArgumentNullException shows differences. For example:

ArgumentNullException has an additional constructor with the signature ArgumentNullException(String, String), allowing you to pass in the name of the parameter that is null.

ArgumentNullException has an additional Property: ParamName, used to hold the name of the parameter that is null and caused the exception.

Since I am just looking at the highest level, there are also likely differences in how the common methods between these are implemented.

Suffice it to say, a child class will very rarely look identical to its parent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM