In simple words, upcast means passing value of lower datatype to upper datatype variable. The datatype of value/variable can be value type or reference type.
In case of reference types:
- Upper datatype is also known as Super type or Parent type or Base type.
- Lower datatype is also known as Sub type or Child type or Derived type.
- These types are related types where lower datatype inherits upper datatype.
- These types shows relationship between types based on inheritance.
- Upcasting and Downcasting are related to these relationships.
Upcasting w.r.t. Value Types
First, we see examples of value type data which are casted to another value type. The upcast operation is always implicit. You do not need to use cast operator.
class Program
{
static void Main(string[] args)
{
int x = 10;
double y = x;
long z = x;
Console.WriteLine($"The values of x, y and z are {x}, {y} and {z} respectively.");
}
}
The conversion of int type into double and long is implcit. We did not use cast operator. The above example is all about upcasting. Now we look at downcast example. The following code shows errors at compile time. We cannot downcast a upper datatype data into lower type without explicit cast operator:
class Program
{
static void Main(string[] args)
{
double x = 10.25;
int y = x; // Error at compile time
long z = x;// Error at compile time
Console.WriteLine($"The values of x, y and z are {x}, {y} and {z} respectively.");
}
}
Now we use cast operator to resolve the errors:
class Program
{
static void Main(string[] args)
{
double x = 10.25;
int y = (int)x;
long z = (int)x;
Console.WriteLine($"The values of x, y and z are {x}, {y} and {z} respectively.");
}
}Upcasting w.r.t. Reference Types
If you have understood this far with value type upcast and downcast, the reference type upcast and downcast is similar. Look at the following code:class Program
{
static void Main(string[] args)
{
// Lower reference type employee1 object upcasted to Person type
Person person = new Employee();
person.Id = 1;
Console.WriteLine(person.GetType()); // Employee
//person.Name = "Error when accessed Name"; // Error
}
}
class Person
{
public int Id { get; set; }
}
class Employee : Person
{
public string Name { get; set; } = "Unknown";
}Some facts about Upcasting w.r.t. Reference Types
- Person is Upper reference type and Employee is Lower reference type.
- person is a reference variable which can be assigned null, a Person instance or any instance of subtypes of Person type e.g. Employee.
- No Upcast/Downcast: Person person = new Person();
- Upcast: Person person = new Employee(); because an employee object is assigned to its upper type i.e. Person reference variable named person.
- The Console.WriteLine(person.GetType()); returns Employee because person is pointing to an employee object (subtype object of Person). The person reference variable is of type Person but it is pointing to an object of Employee type, so person.GetType() returns Employee, not Person.
- The disadvantage of upcast: Upcasting brings loss, person reference variable cannot access Name or any field that belongs exclusively to Employee type. This is why person.Name = "Error when accessed Name"; throws error.
Downcasting w.r.t. Reference Types
There are two important points to remember about Downcasting w.r.t. Reference Types.
- An instance/object of Upper reference type cannot be assigned to Lower reference type without cast. It will throw error during compilation.
- A Lower reference type object that has be upcasted can be downcasted without any compile time error.
Now we see the first case.
CASE I
1. Downcasting Throwing Error At Compile time
class Program
{
static void Main(string[] args)
{
Employee employee = new Person();
}
}
class Person
{
public int Id { get; set; }
}
class Employee : Person
{
public string Name { get; set; } = "Unknown";
}
The compiler throws error message as follows:
"Cannot implicitly convert type 'Person' to 'Employee'. An explicit conversion exists (are you missing a cast?)" 2. Downcasting Throwing Error At Runtime
To hide this error, we can cast the person object i.e. new Person() to Employee type.
class Program
{
static void Main(string[] args)
{
Employee employee = (Employee)new Person();
}
}
class Person
{
public int Id { get; set; }
}
class Employee : Person
{
public string Name { get; set; } = "Unknown";
}
This is a command to compiler that the person object is of Employee type. But if this type conversion is not correct then at runtime, there is error message: "Unhandled exception. System.InvalidCastException: Unable to cast object of type 'Person' to type 'Employee'."
3.
CASE II
3. Downcasting Without Errors
A Lower reference type object that has be upcasted can be downcasted without any compile time error. Look at the following example in this regard:class Program
{
static void Main(string[] args)
{
// Lower reference type employee1 object upcasted to Person type
// person is upcasted variable
Person person = new Employee { Id = 1, Name = "Ajeet" };
//Console.WriteLine(person.Name); // Error after upcast
Console.WriteLine(person.GetType()); // Employee
// Downcast upcasted person variable
Employee employee = (Employee)person;
Console.WriteLine(employee.Id);
Console.WriteLine(employee.Name);
}
}
class Person
{
public int Id { get; set; }
}
class Employee : Person
{
public string Name { get; set; } = "Unknown";
}
No comments:
Post a Comment