Cast
In this chapter you will learn:
Up casting
Up casting is a cast towards its super class or parent class.
using System;/*j a va 2 s.c om*/
class Person
{
public string name;
}
class Employee : Person
{
public string companyName;
}
class Program
{
static void Main(string[] args)
{
Employee e = new Employee();
e.name = "java2s.com";
Person p = e;
Console.WriteLine(e.name);
Console.WriteLine(p.name);
}
}
The output:
Down casting
Down cast happens when casting to subclass or child class. Down cast needs an explicit cast.
using System;//from j a v a2s . c o m
class Person
{
public string name;
}
class Employee : Person
{
public string companyName;
}
class Program
{
static void Main(string[] args)
{
Person p = new Employee();
Employee e = (Employee)p;
}
}
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » Class