0% found this document useful (0 votes)
2 views11 pages

Types and Casting

The document discusses inheritance in C# and how to work with base and derived classes. It covers using the Type class to get information about an object's type, explicitly casting objects between base and derived classes, and using the as operator to avoid exceptions when casting.

Uploaded by

SBnet Silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Types and Casting

The document discusses inheritance in C# and how to work with base and derived classes. It covers using the Type class to get information about an object's type, explicitly casting objects between base and derived classes, and using the as operator to avoid exceptions when casting.

Uploaded by

SBnet Silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 14

How to work
with inheritance

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 1
Objectives
Applied
1. Use any of the features of inheritance that are presented in this
chapter as you develop the classes for your applications.
Knowledge
2. Describe the use of the Type class.
3. Describe the use of explicit casting when working with the objects
of a base class and its derived classes.

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 2
Sample Inheritance Structure

Product
(base)

Book Software
(subclass) (subclass)

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 3
Type class

➢ Every object has a GetType() method that returns a Type


object that corresponds to the object’s Type
➢ Use the properties of the Type class to get information about
the object: Name, Namespace, FullName, BaseType
➢ There are more than 90 properties and methods of Type class
➢ Operator typeof() can be used to test an object’s type
➢ Operator nameof() can be used to test a name

Syntax:
if (p.GetType() == typeof(Book))
if (p.GetType().Name == nameof(Book))

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 4
The Type class
Property Description
Name Returns a string that contains the name of a type.
FullName Returns a string that contains the fully qualified
name of a type, which includes the namespace
name and the type name.
BaseType Returns a Type object that represents the class
that a type inherits.
Namespace Returns a string that contains the name of the
namespace that contains a type.
Method Description
GetType() Returns a Type object that represents the type of
an object.

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 5
Code that uses the Type class
to get information about an object
Product p;
p = new Book("CS15", "Murach's C# 2015",
"Anne Boehm", 56.50m);
Type t = p.GetType();
Console.WriteLine("Name: " + t.Name);
Console.WriteLine("Namespace: " + t.Namespace);
Console.WriteLine("FullName: " + t.FullName);
Console.WriteLine("BaseType: " + t.BaseType.Name);

The result that’s displayed on the console


Name: Book
Namespace: ProductMaintenance
FullName: ProductMaintenance.Book
BaseType: Product

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 6
How to test an object’s type
if (p.GetType() == typeof(Book))

Another way to test an object’s type


if (p.GetType().Name == nameof(Book))

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 7
Casting

➢ Base class → Product, SubClass → Book


➢ C# can implicitly cast a subclass to its base class.
You can specify a Book object when a Product object
is expected.
➢ You must explicitly cast a base class object when a
reference to one of its subclasses is required. You
must cast a Product object to Book if a Book object
is expected.
➢ If cast invalid, it will throw an exception.
Use keyword as to return a null if exception thrown.

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 8
Two methods that display product information
public void DisplayProduct(Product p) =>
MessageBox.Show(p.GetDisplayText());

public void DisplayBook(Book b) =>


MessageBox.Show(b.GetDisplayText());

Code that doesn’t require casting


Book b = new Book("CS15", "Murach's C# 2015",
"Anne Boehm", 56.50m);
DisplayProduct(b); // Casting is not required because Book
// is a subclass of Product.

Code that requires casting


Product p = new Book("CS15", "Murach's C# 2015",
"Anne Boehm", 56.50m);
DisplayBook((Book)p); // Casting is required because
// DisplayBook accepts a Book object.

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 9
Code that throws a casting exception
Product p = new Software("NPTK",
".NET Programmer's Toolkit", "4.5", 179.99m);
DisplayBook((Book)p); // Will throw a casting exception
// because p is a Software object,
// not a Book object.

Code that uses the as operator


Product p = new Book("CS15", "Murach's C# 2015",
"Anne Boehm", 56.50m);
DisplaySoftware(p as Software); // Passes null because p
// isn't a Software object

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 10
Using GetType, typeof and as in a program

Product class (base):

private virtual string DisplayData() => GetType().Name;


// displays either Product, Book or Software

List of Products called products. Referencing in form code:

// check that item in list is of type Book


if (products[i].GetType() == typeof(Book))
{ // reference property of Book (not Product)
(products[i] as Book).Author = “Joel Murach”;
}

© 2016, Mike Murach & Associates, Inc.


Murach's C# 2015 C14, Slide 11

You might also like