All Classes Derive from System.Object

Given any class, whether a custom class or one built into the system, the methods shown in Table 7.2 will be defined.

Table 7.2: Members of System.Object

Method Name

Description

public virtual bool Equals(object o)

Returns true if the object supplied as a parameter is equal in value, not necessarily in reference, to the instance.

public virtual int GetHashCode()

Returns an integer corresponding to an evenly spread hash code. This is useful for collections such as HashTable collections.

public Type GetType()

Returns an object of type System.Type corresponding to the type of the object instance.

public static bool ReferenceEquals( object a, object b)

Returns true if the two supplied parameters refer to the same object.

public virtual string ToString()

Returns a string representation of the object instance.

public virtual void Finalize()

An alias for the destructor; informs the object to prepare for termination. C# prevents you from calling this method directly.

protected object MemberwiseClone()

Clones the object in question by performing a shallow copy; references are copied, but not the data within a referenced type.

All of the methods listed in Table 7.2 appear on all objects through inheritance; all classes derive (either directly or via an inheritance chain) from object. Even literals include these methods, enabling somewhat peculiar-looking code such as this:

Console.WriteLine( 42.ToString() );

Even class definitions that don’t have any explicit derivation from object derive from object anyway. The two declarations for PdaItem in Listing 7.19, therefore, result in identical CIL.

Listing 7.19: System.Object Derivation Implied When No Derivation Is Specified Explicitly
1. public class PdaItem
2. {
3.     // ...
4. }
5. public class PdaItem : object
6. {
7.     // ...
8. }

When the object’s default implementation isn’t sufficient, programmers can override one or more of the three virtual methods. Chapter 10 describes the details involved in doing so.

Type Checking

Since C# 1.0, there have been multiple ways to check an object’s type including the is operator and the switch statement. In C# 8.0, the switch expression was introduced to enable a value return from the expression.

Type Checking with the is Operator

Since C# allows casting down the inheritance chain, it is sometimes desirable to determine what the underlying type is before attempting a conversion. Also, checking the type may be necessary for type-specific actions where polymorphism was not implemented. To determine the type, C# has included an is operator since C# 1.0 (see Listing 7.20).

Listing 7.20: is Operator Determining the Underlying Type
1. public class Person
2. {
3.     // ...
4. }
5.  
6. public class Employee : Person
7. {
8.     // ...
9. }
10.  
11. public class Program
12. {
13.     private static object? GetObjectById(string id)
14.     {
15.         // ...
16.     }
17.  
18.     public static void Main(params string[] args)
19.     {
20.         string id = args[0];
21.         object? entity = GetObjectById(id);
22.  
23.         if (entity is Person)
24.         {
25.             Person person = (Person) entity;
26.             Console.WriteLine(
27.                 $"Id corresponds to a 
28.                     nameof(Person) } object: {
29.                     person.FirstName} {
30.                     person.LastName}."
31.                 );
32.  
33.             if (entity is Employee employee)
34.             {
35.                 Console.WriteLine(
36.                     $"Id ({ employee.Id }) is also an {
37.                         nameof(Employee)} object.");
38.             }
39.         }
40.         else if(entity is null)
41.         {
42.             Console.WriteLine(
43.                 $"Id was unknown so null was returned.");
44.         }
45.         else
46.         {
47.             Console.WriteLine(
48.                 $"Id '{id}' not an {
49.                     nameof(Employee)} or a {nameof(Person)} object.");
50.         }
51.     }
52. }

After invoking GetObjectById(), which returns a nullable object based on the id specified, Listing 7.20 invokes the is operator on the returned instance. Initially, it checks that the value isn’t null. Next it determines if the return is a Person and then an Employee. If GetObjectById() returns a Person, then the output will be, “Id corresponds to a Person object….” When GetObjectById() returns, an Employee output will show both a Person and an Employee message; since Employee derives from Person, all Employee objects are also Person objects.

Type Checking with Declaration

With an explicit cast, it is the programmer’s responsibility to understand the code logic sufficiently to avoid an invalid cast exception. If an invalid cast might occur, it would be preferable to leverage a type check and avoid the exception entirely. Listing 7.20 demonstrates this by first checking whether entity is a Person and then casting Entity to a Person:

// ...

if (entity is Person)

{

     Person person = (Person) entity;

// ...

The advantage is that the is operator enables a code path for when the explicit cast might fail without the expense of exception handling. With the addition of declaration in C# 7.0 and later, the is operator (and switch syntax shown next) will make the type check and the assignment in the same step. Listing 7.20 demonstrates this with the is Employee code snippet:

if (entity is Employee employee)

{

// ...

}

Here, in checking whether the input operand is an employee, it also declares a new variable of type Employee. The result from GetObjectById(id), therefore, is checked against the Employee type—type pattern matching—and assigned to the variable employee in the same expression. If the result of GetObjectById(id) is null or not an Employee, then false is produced, and the else clause executes.

Note that the employee variable is available within and after the if statement; however, it would need to be assigned a value before accessing it inside or after the else clause. Also, you can implicitly specify the data type with var, as expected, but as with local variable declaration in general, use caution when the data type isn’t obvious.

Type Checking with switch Statements

The switch statement also allows for type checking, but it can check for multiple types in addition to null checking—all within the same statement. In Listing 7.21, we demonstrate this with a method that returns the time (using .NET 6.0’s TimeOnly), from a variety of types.

Listing 7.21: Type Checking with a switch Statement
1. public static TimeOnly GetTime(object input)
2. {
3.     switch (input)
4.     {
5.         case DateTime datetime:
6.             return TimeOnly.FromDateTime(datetime);
7.         case DateTimeOffset datetimeOffset:
8.             return TimeOnly.FromDateTime(datetimeOffset.DateTime);
9.         case string dateText:
10.             return TimeOnly.Parse(dateText);
11.         case null:
12.            throw new ArgumentNullException(nameof(input));
13.         default:
14.             throw new ArgumentException(
15.                 $"Invalid type - {input.GetType().FullName}");
16.     };
17. }

Listing 7.21 includes a case for null, but if none of the cases matches, then default provides a catchall and throws an exception. Also, like with the is operator, you can declare a variable simultaneously, providing access to an instance of the object that matches the case.

Starting in C# 8.0, the switch statement includes a more succinct expression form that returns a value.

Type Checking with switch Expressions

Like the switch statement, the switch expression (C# 8.0) provides multiple paths for each case—generically a match expression (for both switch expressions and statements). Unlike the switch statement, there are no labels, no break statements, and, in fact, no enclosed statements at all. Most importantly, the switch expression returns a value (except when it throws an exception). This makes it a preferable option to the switch statement in Listing 7.21, which allows for a return only because the function of the embedded return statements. You couldn’t, for example, assign the switch statement result to a variable. Listing 7.22 provides an example.

Listing 7.22: Using switch Expressions to Return a Value
1. public static TimeOnly GetTime(object input) =>
2.     input switch
3.     {
4.         DateTime datetime
5.             => TimeOnly.FromDateTime(datetime),
6.         DateTimeOffset datetimeOffset
7.                 => TimeOnly.FromDateTime(datetimeOffset.DateTime),
8.         string dateText => TimeOnly.Parse(
9.             dateText),
10.         null => throw new ArgumentNullException(nameof(input)),
11.         _ => throw new ArgumentException(
12.             $"Invalid type - {input.GetType().FullName}"),
13.     };

From Listing 7.22 you will notice that unlike a switch statement, the input operand for the switch statement appears before the switch keyword. Each match expression is simply identified by the type, optionally followed by a variable declaration. Also, instead of the colon designating a label, the switch expression uses a lambda operator =>. Lastly, instead of the default keyword, the switch expression uses the discard symbol: _.

Functionally, Listing 7.21 and Listing 7.22 are equivalent. They both check the type, allow for variable declaration, allow null checking, and have a default. One noteworthy switch expression characteristic, however, is that the _ (default) catchall must appear at the end or it will obscure later match expressions. In contrast, default on a switch statement provides the same functionality regardless of where it appears.

{{ snackbarMessage }}
;