Skip to main content

I am new to C# (and OOP). When iI have some code like the following:

class Employee
{
    // some code
}


class Manager : Employee
{
    //some code
}

Question 1: if iIf I have other code that does this:

   Manager mgr = new Manager();
   Employee emp = (Employee)mgr;

Here employeeEmployee is a managerManager, but when iI cast it like that to an Employee it means iI am upcasting it?

Question 2:

When iI have several Employee class objects and some but not all of them are Manager's, how can iI downcast them where possible?

I am new to C# (and OOP). When i have some code like the following:

class Employee
{
    // some code
}


class Manager : Employee
{
    //some code
}

Question 1: if i have other code that does this:

   Manager mgr = new Manager();
   Employee emp = (Employee)mgr;

Here employee is a manager, but when i cast it like that to an Employee it means i am upcasting it?

Question 2:

When i have several Employee class objects and some but not all of them are Manager's, how can i downcast them where possible?

I am new to C# (and OOP). When I have some code like the following:

class Employee
{
    // some code
}


class Manager : Employee
{
    //some code
}

Question 1: If I have other code that does this:

   Manager mgr = new Manager();
   Employee emp = (Employee)mgr;

Here Employee is a Manager, but when I cast it like that to an Employee it means I am upcasting it?

Question 2:

When I have several Employee class objects and some but not all of them are Manager's, how can I downcast them where possible?

Added tags 'downcasting' and 'up-casting' as question is about Upcasting and Downcasting in C#. Updated formatting.
Source Link

I am new to C#C# (and OOPOOP). When i have some code like the following:

class Employee
{
    // some code
}


class Manager : Employee
{
    //some code
}

Question 1Question 1: if i have other code that does this:

   Manager mgr = new Manager();
   Employee emp = (Employee)mgr;

Here empemployee is a manager, but when i cast it like that to an EmployeeEmployee it means i am upcasting it?

Question 2Question 2:

When i have several EmployeeEmployee class objects and some but not all of them are ManagersManager's, how can i downcast them where possible?

I am new to C# (and OOP). When i have some code like the following:

class Employee
{
    // some code
}


class Manager : Employee
{
    //some code
}

Question 1: if i have other code that does this:

   Manager mgr = new Manager();
   Employee emp = (Employee)mgr;

Here emp is a manager, but when i cast it like that to an Employee it means i am upcasting it?

Question 2:

When i have several Employee class objects and some but not all of them are Managers, how can i downcast them where possible?

I am new to C# (and OOP). When i have some code like the following:

class Employee
{
    // some code
}


class Manager : Employee
{
    //some code
}

Question 1: if i have other code that does this:

   Manager mgr = new Manager();
   Employee emp = (Employee)mgr;

Here employee is a manager, but when i cast it like that to an Employee it means i am upcasting it?

Question 2:

When i have several Employee class objects and some but not all of them are Manager's, how can i downcast them where possible?

deleted 8 characters in body
Source Link
RCIX
  • 39.2k
  • 50
  • 151
  • 209

I am new to C# ( ofcourseand OOP).When When i have asome code aslike the following:

class Employee
{
    // some code
}


class Manager : Employee
{
    //some code
}

Question :

(i) Employee emp=new Manager( );

(or) Question 1: if i have other code that does this:

Manager mgr=new Manager();

   Manager mgr = new Manager();
   Employee emp = (Employee)mgr;

EmployeeHere emp =(Employee)mgr;is a manager, but when i cast it like that to an Employee it means i am upcasting it?

Here manager is an employee.so when a we cast the derived type to base type will it mean upcasting ? Question 2:

(ii) When i have several Employee class objects and some but not all of them are Managers, how can i downcast them where possible?

When all Employees could not be a Manager,what is the example of Downcast?

I am new to C# ( ofcourse OOP).When i have a code as

class Employee
{
  // some code
}


class Manager :Employee
{
  //some code
}

Question :

(i) Employee emp=new Manager( );

(or)

Manager mgr=new Manager();

Employee emp =(Employee)mgr;

Here manager is an employee.so when a we cast the derived type to base type will it mean upcasting ?

(ii)

When all Employees could not be a Manager,what is the example of Downcast?

I am new to C# (and OOP). When i have some code like the following:

class Employee
{
    // some code
}


class Manager : Employee
{
    //some code
}

Question 1: if i have other code that does this:

   Manager mgr = new Manager();
   Employee emp = (Employee)mgr;

Here emp is a manager, but when i cast it like that to an Employee it means i am upcasting it?

Question 2:

When i have several Employee class objects and some but not all of them are Managers, how can i downcast them where possible?

Source Link
user184805
  • 1.9k
  • 4
  • 19
  • 16
Loading