object Examples
This page was last reviewed on Apr 16, 2023.
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.
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 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()); } }
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.
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, uint
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
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.
Detail 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.
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.
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.
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.
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 Apr 16, 2023 (edit).
© 2007-2024 Sam Allen.