C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
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, uintC# program that uses object type argument
using System;
class Program
{
static void Main()
{
// Use string type as object.
string value = "The Dev Codes";
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: The Dev Codes
True
String value: The Dev Codes
True
Int value: 100
True
Int value: 100
False
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
Note: Class hierarchies in object-oriented programming allow us to reference derived types by their base type.
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.
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.