Home
C#
object Examples
This page was last reviewed on Sep 3, 2024.
Dot Net Perls
Object. Suppose you have a C# 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 Array
object.ReferenceEquals
Clone
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.
using System; using System.Text; class Program { static void Main() { // Use an object reference. object val = new StringBuilder(); Console.WriteLine(val.GetType()); } }
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.
Note The string variable is assigned a reference to a literal string. This is passed as an object type to the Test method.
Info Test() checks its parameter against null. It then uses the is-cast to test the most derived type of the object.
null
is
Next The first 2 Test calls will evaluate the (value is string) expression as true. The second 2 calls will match (value is int).
int
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); } } }
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.
Start This program uses strings as the arguments to object.Equals. It uses 2 strings.
Info One argument is a string literal. The second is a character that is converted to a string with ToString.
Result 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.
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); } }
True False
Boxing. The object type has some complexities. Value types cause no heap allocations. But without the heap allocation, the object cannot exist.
Detail 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.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 3, 2024 (simplify).
Home
Changes
© 2007-2024 Sam Allen.