Downcasting C# .NET

Here is my example:

public class Person
{
    public string Name { get; set; }
}

public class Client : Person
{
    public string LastName { get; set; }
}

public class Test
{
    Person p = new Person();
    Client c = (Client)p; //throws exception
}

Since Client inherits from Person, why i can't do this? if i can and this is the wrong way, how can i do it?

OBS: I am aware that the example above will do:

Person p = new Client();
Client c = (Client)p;

Polymorphism, Up-casting and Down-casting, First we need to design our base class, this will be called Shape. Our shape class will implement a constructor that will accept an X and Y co-� Downcasting however has to be done at run time generally as the compiler may not always know whether the instance in question is of the type given. C# provides two operators for this - is which tells you if the downcast works, and return true/false.

A Person is not a Client hence how do you expect the compiler to cast it? Try this and you will get a null assigned:

Client c = p as Client;

In other words: compiler error gone, but c will always be null.

downcast and upcast, Downcasting is where you take a base class and then try and turn it into C# provides two operators for this - is which tells you if the downcast� .net c# downcasting uppercasting. Views: 8256 Total Answered: 3 Total Marked As Answer: 0 Posted On: 20-Apr-2017 02:36 Share:

This throws an exception, because p is not a client. p has been created as person. You could create a client-object and cast it to its baseclass person, but not this way round.

What is a proper use of downcasting?, Also, downcasting is very safe in C#. We have a strong guarantee that the downcast will be verified at runtime and if it cannot be verified then� Downcasting however has to be done at run time generally as the compiler may not always know whether the instance in question is of the type given. C# provides two operators for this - is which tells you if the downcast works, and return true/false.

A child can inherit all the properties of its parent class, but parent can only inherit common properties. that does not means you can assign directly parent object to it's child object.

Up-casting and Down-casting in C# Inheritance, Up-casting and Down-casting in C# Inheritance. June 21, 2019 April 23, 2016 by Rakesh Singh. (Last Updated On: June 21, 2019). Answer: Explanation of� This article gives an introduction to polymorphism in C#. Using the example created in this introduction, the article then goes on to describe how we up-cast and down-cast objects.

you can do it by using as and is operatos.

By using as operator, if variable is not null then the class convertible to other class

Person p = new Person();
var conversionResultFromPersonToClient = p as Client;
if(conversionResultFromPersonToClient != null)
   //it means you can cast from person to client

Second way is using is operator

Person p = new Person();
Client c = new Client();
if(p is Client)
   //Then you can cast person to client
   c = (Client)p;

Down Casting in C#, Re: Down Casting in C#. Sep 23, 2016 08:12 PM|Rion Williams|LINK. Just to clarify, you� C# is an object-oriented programming language developed by Microsoft, which runs under .NET platform. Now, C# can be run on Mac, Linux/Unix and Windows using .NET Core. If you want to crack your C# interview, you’ve come across the right book. This is the book with which you will be confident to answers the questions on C# language.

Upcasting and downcasting in C#, Upcasting and downcasting in C# of a specialized type to a more general type. Downcasting converts an object from a general type to a more specialized type� .NET Framework supports automatic conversion from derived types to their base types and back to the derived type, as well as from types that present interfaces to interface objects and back. It also includes a variety of mechanisms that support custom conversions. For more information, see Type Conversion in .NET Framework.

What is downcasting and upcasting? (.NET interview questions with , "DownCasting" is opposite to "Upcasting" moving the parent object to the child object Duration: 1:33 Posted: Dec 23, 2013 I think, things are really changing when new .NET release comes out. I did both upcast and downcast Prefix vs. AS performance tests and Prefix always wins. ( C# .NET 3.5 release mode) Just try this simple test: Average Prefix: 113 000 ticks Average AS: 135 000 ticks private void button1_Click(object sender, EventArgs e)

Upcasting and Downcasting in C# 5, Find .Net 4.5 C Sharp C# 5 Tutorials, Codes, Resources and Downloads on various topics. Downcasting however has to be done at run time generally as the compiler may not always know whether the instance in question is of the type given. C# provides two operators for this - is which tells you if the downcast works, and return true/false.

Comments
  • What exception is thrown?
  • In your first example, if it did work what should happen when you do c.LastName?
  • See it this way: "all clients are persons but not all persons are clients"; hence, the compiler cannot be sure if the person you are trying to convert is indeed a client.
  • If this would work, using the c.LastName property would access memory that does not belong to the object. Since Person doesn't have that property. The gravest possible type safety problem you could imagine. That is how you corrupt a heap and make your program fail in impossible to diagnose ways.
  • @HansPassant - So you are saying there is a chance :P
  • But as per the problem Person is a parent class and client is the child class, what is the reason of introducing a new parent class BasePerson?
  • @NitinAggarwal: it is necessary because otherwise you can also not compile, as you cannot make an implicit/explicit operator from or to a BaseClass. This was in essence just a trick to make it work, as i said, it might not have been what the OP was after, just a solution to make the code compilable + to enable the (Client)p statement
  • I appreciate your answer, i think what Fabio asked was different what you have answered, you have removed parent-child relationship between person and client.
  • @NitinAggarwal I agree with you on that one, and i also mentioned it in the answer and in the comments, in his case i would choose to create rather a helper method like Client GetClientFromPerson(Person p)to make it clear to other persons that this isn't a cast and it is rather a conversion, or a constructor with public Client(Person p) but, i thought i would try thinking out of the box on this one :)
  • I would vote up but i don't have enough reputation yet. Hehe.
  • I thought that since Client inherits Person, when you try to cast the framework would do thejob of filling Client's Name property with the value of Person's Name Property. But its ok i got it, thanks for your time :D.
  • I thought that since Client inherits Person, when you try to cast the framework would do thejob of filling Client's Name property with the value of Person's Name Property. But its ok i got it, thanks for your time :D.