C# Object Examples

Understand the object base class, object.Equals and ReferenceEquals. All types inherit from object.

Object. Suppose you have a class instance (like a StringBuilder). It can be used as an object because it inherits from the object class—all classes do.Class

We can pass any type as an object parameter—the cast is performed automatically (implicitly) by the compiler. Object is an important feature of the C# type hierarchy.

Object example. Any C# type can be used as an object. Each class is derived from the base class object. Each reference is both an object and its derived types.
Next: The program shows that as an object, the reference still is an instance of the more-derived StringBuilder type.
C# program that uses object using System; using System.Text; class Program { static void Main() { // Use an object reference. object val = new StringBuilder(); Console.WriteLine(val.GetType()); } } Output System.Text.StringBuilder

Parameter example. Next this program introduces a method that receives an object type parameter. It shows how to use objects as arguments to functions.
Program: The string variable is assigned a reference to a literal string. This is passed as an object type to the Test method.
Test method: Checks its parameter against null. It then uses the is-cast to test the most derived type of the object.
Next: The first 2 Test calls will evaluate the (value is string) expression as true. The second 2 calls will match (value is int).
StringsInt, uint
C# program that uses object type argument using System; class Program { static void Main() { // Use string type as object. string value = "Dot Net Perls"; Test(value); Test((object)value); // Use integer type as object. int number = 100; Test(number); Test((object)number); // Use null object. Test(null); } static void Test(object value) { // Test the object. // ... Write its value to the screen if it is a string or integer. Console.WriteLine(value != null); if (value is string) { Console.WriteLine("String value: {0}", value); } else if (value is int) { Console.WriteLine("Int value: {0}", value); } } } Output True String value: Dot Net Perls True String value: Dot Net Perls True Int value: 100 True Int value: 100 False

Object.Equals. This method compares the contents of objects. It first checks whether the references are equal. Object.Equals calls into derived Equals methods to test equality further.
Example: This program uses strings as the arguments to object.Equals. It uses 2 strings.
Arguments: One argument is a string literal. The second is a character that is converted to a string with ToString.
Locations: The string literal "a" and the result of ToString are in different memory locations. Their references are thus not equal.
Important: Equals is the same as object.ReferenceEquals, but it goes further. With ReferenceEquals the derived equality method is not used.
C# program that uses object.Equals method using System; class Program { static void Main() { // Equals compares the contents (and references) of 2 objects. bool b = object.Equals("a", 'a'.ToString()); Console.WriteLine(b); // ReferenceEquals compares the references of 2 objects. b = object.ReferenceEquals("a", 'a'.ToString()); Console.WriteLine(b); } } Output True False

Derived types. Each type is logically derived from the object type. If you create a class and do not specify what it derives from, it is implicitly derived from object.
Note: Class hierarchies in object-oriented programming allow us to reference derived types by their base type.

Object.ReferenceEquals. This just compares memory locations, not the data stored in this locations. So it may return a different value than Equals.object.ReferenceEquals
Reference: A reference is a simple value that indicates a memory location. It is 4 or 8 bytes.
Tip: We use object.ReferenceEquals if we want to know if the 2 variables are the same object in the same memory location.

Equals, internals. Equals internally compares the 2 parameters for referential equality. Equals is the same as ReferenceEquals except it also calls a virtual Equals method.Virtual

Clone. Cloning is not just a subject of science fiction movies. It can be done with the Clone method. The object type provides the Clone method.Clone

Arrays. You can use arrays of objects and pass these arrays around in your programs. Object arrays are necessary for some types. We create and use object arrays.Object Array

Cast parameters. It is possible to use the object type in the formal parameter list of a method. But this incurs a loss of type information.
Tip: In the method, you will have to try to determine the derived type of the object. You can use the is-cast or as-cast for this.

Boxing. The object type has some complexities. Value types cause no heap allocations. But without the heap allocation, the object cannot exist.
Therefore: The object type is more a logical instead of physical derivation. It is a concept.
And: Because of this lack of a type pointer, value types must be allocated in a new object to be cast to their base object type.
Note: This is termed boxing. The opposite process is termed unboxing. Both of these operations impact performance.

A summary. We examined the object type. We can use an object as an argument. We can receive any type as an object type in the formal parameter list.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.