Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

upcasting and downcasting in c#

/*
Upcasting: conversion from a derived class to a base class
Downcasting: conversion from a base class to a derived class
All objects can be implicitly converted to a base class reference

Upcasting is used when we need to develop a code that deals with only the parent class. 
Downcasting is used when we need to develop a code that accesses behaviors of the child class
*/
    public class Employee
    {
        public int EmployeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class Manager : Employee
    {
        public int ManagerID { get; set; }
        public string Posting { get; set; }
    }


    public class Program
    {
        static void Main(string[] args)
        {
            // upcasting
            Manager manager = new Manager();
            Employee employee = manager; // upcasting 
            /*
            both manager and employee object actullay pointng the same object in memory. 
            Just have diffrenct view. 
            employee does not have access to ManagerID,Posting
            manager have access all; 
			*/
            manager.FirstName = "John";
            employee.FirstName = "Sr. John";

            Console.WriteLine(manager.FirstName); // Sr. John
            Console.WriteLine(employee.FirstName); // Sr. John

            Manager manager2 = (Manager)employee; // downcast

            /*
             Casting can throw an exception if the conversion is not successful.We can use the as
             keyword to prevent this.If conversion is not successful, null is returned.
            */

            Manager manager3 = employee as Manager;
            if (manager3 != null)
            {
              // do something
            }
          
            // or
            if (employee is Manager)
            {
                var manager4 = (Manager)employee;
              // do something
            }
        }
    }
Comment

upcasting and downcasting in c#

class Employee
{
    // some code
}
class Manager : Employee
{
    //some code
}
// casting can be done by the following
if (employee is Manager)
{
    Manager m = (Manager)employee;
}
// or with the as operator like this:
Manager m = (employee as Manager);

Comment

PREVIOUS NEXT
Code Example
Csharp :: process which converts natural sugar into alcohol by yeast 
Csharp :: method declaration in c# 
Csharp :: c# xaml textblock new line 
Csharp :: c# linq get one object 
Csharp :: c sharp while statement 
Csharp :: Query Parent-GrandChild single 
Csharp :: secret 
Csharp :: as c# 
Csharp :: Permutation and Combination in C# 
Csharp :: copy properties from two subclasses c# 
Csharp :: c# stack 
Csharp :: regex ip rage detect c# 
Csharp :: universities in greece 
Csharp :: generate prime numbers 
Csharp :: maximum sum of non-adjacent 
Csharp :: get sites ip in C# 
Csharp :: Create an array with random values c# 
Csharp :: run async method parallel c# 
Csharp :: devexpress objectspace to session 
Csharp :: can lightning strike the same place twice 
Csharp :: how to use K2 games Games parallax background 
Csharp :: cancellationtoken.linkedtokensource c# example 
Html :: how to use unsplash images in html 
Html :: jquery ui cdn 
Html :: meta author 
Html :: html input float type 
Html :: alphanumeric input 
Html :: vscode user code snippet not working markdown 
Html :: laravel blade remove all html tags 
Html :: justify content 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =