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

 

Postlude

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

DeepCsharp360

 Chapter List

  1. Why C#?
    I Strong Typing & Type Safety
  2. Strong Typing
       Extract 
    Why Strong Typing
  3. Value & Reference
       Extract
    Value And Reference
  4. Structs & Classes
       Extract
    Structs & Classes 
  5. Inheritance
      
    Extract
    Inheritance
  6. Interfaces & Multiple Inheritance
      
    Extract Interface
  7. Controlling Inheritance
    II Casting & Generics
  8. Casting - The Escape From Strong Typing
      
    Extract Casting I
  9. Generics
  10. Advanced Generics
  11. Anonymous & Dynamic
    Typing
    III Functions
  12. Delegates 
  13. Multicast Delegates
  14. Anonymous Methods, Lambdas & Closures
    IV Async
  15. Threading,Tasks and Locking ***NEW!
  16. The Invoke Pattern
  17. Async Await
  18. The Parallel For
    V Data - LINQ, XML & Regular Expressions
  19. The LINQ Principle
  20. XML
  21. LINQ To XML
  22. Regular Expressions
    VI Unsafe & Interop
  23. Interop
  24. COM
  25. Custom Attributes
  26. Bit Manipulation
  27. Advanced Structs
  28. Pointers 

Extra Material
Passing Parameters ***NEW!

 <ASIN:B09FTLPTP9>

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.

Banner


JavaScript Survey Opens Ahead of JavaScript Day
29/09/2025

JetBrains is hosting JavaScript Day on October 2nd with all presentations being streamed live on You Tube, where they will remain available after the event. And anyone interested in the JavaScrip [ ... ]



Open Source Is Not Just About Software
23/09/2025

It's about infrastructure as well, something that although not attracting the limelight, is as important as the open source software it hosts. Today the stewards of the largest open sou [ ... ]


More News

pico book

 

Comments




or email your comment to: comments@i-programmer.info

 



Last Updated ( Saturday, 01 January 2022 )