Deep C# - Inheritance
Written by MIke James   
Monday, 27 December 2021
Article Index
Deep C# - Inheritance
Inherited Constructors
Is and As

Is and As

There are two operators that can help avoid run-time exceptions when casting. The is operator can be used to test the type of the reference before applying a cast. For example:

if (MyAnything is MyClassB)
 	MyClassB MyB = (MyClassB)MyAnything;

This is the basic form of the is but as well as a type it can also test for a more general pattern – see Pattern Matching in Chapter 2.

The as operator is an alternative way of writing a cast but with the difference that if it fails the reference is to null. For example:

MyClassB MyB =  MyAnything as MyClassB;

never throws an exception but you have to make sure that you don’t end up using a null reference because it didn’t actually work.

Notice that you can use as to write a cast method call more neatly:

(MyAnything as MyClassB).MethodB()

but notice that you will generate a run-time error if the cast fails and MethodB doesn’t exist. To avoid this you can use the null conditional operator:

(MyAnything as MyClassB)?.MethodB()

Now the result is a null if the method call fails.

Always use as in place of explicit casting.

In Chapter But Not In This Extract

  • Overriding and Abstract Class
  • Overriding – Virtual and Late Binding
  • Hiding Methods With new
  • Late or Early



Inheritance is simple when you restrict it to code reuse. Unless, that is, you allow overriding existing methods as well as adding new methods. If you allow overriding then you have to make a choice about which version of the method is called – early or late binding. This is a complication that can be difficult to understand and even more difficult to keep under control. This complication is entirely due to enforcing strong typing. In a language that doesn’t assign a type to variables, only to objects, then the only possibility is late binding.

Today inheritance is only partly about code reuse and abstract classes can be used to ensure that derived classes have a particular set of methods. This allows us to implement, rather than inherit the implementations of, those methods.


Deep C#

 Buy Now From Amazon


 Chapter List

  1. Why C#?

    I Strong Typing & Type Safety
  2. Strong Typing
    Why Strong Typing
  3. Value & Reference
  4.    Extract Value And Reference 
  5. Structs & Classes
    Structs & Classes 
  6. Inheritance
  7. Interfaces & Multiple Inheritance
    Extract Interface ***NEW!
  8. Controlling Inheritance

    II Casting & Generics
  9. Casting - The Escape From Strong Typing
  10. Generics
  11. Advanced Generics
  12. Anonymous & Dynamic

    III Functions
  13. Delegates
  14. Multicast Delegates
  15. Anonymous Methods, Lambdas & Closures

    IV Async
  16. Threading, Tasks & Locking
  17. The Invoke Pattern
  18. Async Await
  19. The Parallel For

    V Data - LINQ, XML & Regular Expressions
  20. The LINQ Principle
  21. XML
  22. LINQ To XML
  23. Regular Expressions

    VI Unsafe & Interop
  24. Interop
  25. COM
  26. Custom Attributes
  27. Bit Manipulation
  28. Advanced Structs
  29. Pointers 

Extra Material




To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


Deep Blue Became World Chess Champion 25 Years Ago

Today is the 25th Anniversary of IBM's chess playing supercomputer Deep Blue beating the reigning world chess champion Gary Kasparov at his own game, marking a milestone in the progress of artificial  [ ... ]

LinkedIn Open Sources Feathr Machine Learning Feature Store

LinkedIn has made Feathr open source. Feathr is the feature store LinkedIn built to simplify machine learning feature management and improve developer productivity.

More News





or email your comment to:


Last Updated ( Saturday, 01 January 2022 )