how to down cast in c#

c# inheritance upcasting and downcasting
c# downcast interface
c# is downcasting bad
polymorphism upcasting
c# downcasting not working
dynamic downcast c#
c# downcast list
c# cast parent to child

Possible Duplicate: Typecasting in C#

how do I down cast in c#?


You just use the normal (cast) operator:

class HigherTypeInHierarchy {}
class LowerTypeInHierarchy : HigherTypeInHierarchy {}

var lowerTypeInHierarchy = new LowerTypeInHiearchy();

// OK downcast:
HigherTypeInHierarchy someObject = lowerTypeInHierarchy;
var okDowncast = (LowerTypeInHiearchy)someObject; // OK

Of course, for the cast to be valid, the object you try to cast must be of the type you cast to, or you will get a runtime Exception:

class AnotherLowerTypeInHierarchy : HigherTypeInHierarchy {}
var anotherLowerTypeInHierarchy = new AnotherLowerTypeInHiearchy();

// Failing downcast:
HigherTypeInHierarchy someObject = anotherLowerTypeInHierarchy;
var failedDowncast = (LowerTypeInHiearchy)someObject; // Exception!

If you just want to "try-cast" (i.e. are prepared to handle the situation where the type you attempt to downcast is not in fact of the designated type, use the as-cast instead:

var lowerTypeInHiearchyOrNULL = someObject as LowerTypeInHiearchy;

Downcasting is generally a bad idea, since it implies knowledge of the type outside the type system. Operations on higher levels in the hierarchy are supposed to work equally for all derived types (see Liskov substitution principle), yet by downcasting you state a contrary requirement that the type is in fact of a specific derived type).

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 the  This article describes a simple approach to downcasting in C#; downcasting merely refers to the process of casting an object of a base class type to a derived class type. Upcasting is legal in C# as the process there is to convert an object of a derived class type into an object of its base class type.


Class Animal {  }

Class Dog : Animal {  }

Dog dog = new Dog();
Animal animal = dog; //upcasting
Dog sameDog = (Dog)animal; //Downcasting

Downcasting in C#, This article describes a simple approach to downcasting in C#; downcasting merely refers to the process of casting an object of a base class type to a derived class type. Upcasting is legal in C# as the process there is to convert an object of a derived class type into an object of its base class type. C# provides two operators for this - is which tells you if the downcast works, and return true/false. And as which attempts to do the cast and returns the correct type if possible, or null if not. C# provides the is and as operators for casting. the as operator is more efficient because it actually returns the cast value if the cast can be made


http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/

Have a look at the URL above.

Also, think link that @Barry has posted would be my second choice for links because it is very simple example to follow.

Upcast And Downcast Basics, Child child = (Child) parent; //Down cast parent ref. variable to child. child.sleep(); //Calling child class sleep method and it will work fine. Answer: Explanation of Up-casting and Down-casting in C# inheritance with program examples. C# Up-Casting. An assignment of derived class object to a base class reference in C# inheritance is known as up-casting. The up-casting is implicit and any explicit typecast is not required.


Down Casting in C#, Just to clarify, you are referring as downcasting as the ability to take an instance of a base class and cast it to an instance of a class that derives  Downcasting means casting from a base class (or interface) to a subclass or leaf class. An example of a downcast might be if you cast from System.Object to some other type. Downcasting is unpopular, maybe a code smell: Object Oriented doctrine is to prefer, for example, defining and calling virtual or abstract methods instead of downcasting.


Upcasting and downcasting in C#, Upcasting and downcasting in C#. Upcasting converts an object of a specialized type to a more general type. Downcasting converts an object from a general  The original aim of this article was to explain Up-casting and Down-casting. Having started writing the article, I was struggling to find an ideal way to describe these two concepts. Having thought further about it, one way I found was to introduce the concept of polymorphism to help explain up and down-casting.


Up-casting and Down-casting in C# Inheritance, C# Up-Casting An assignment of derived class object to a base. So, we have to downcast from base to child class, then call specialized  You can also use LINQ to iterate through the elements of the base class and downcast each element, like this: var baseList = new List<BaseClass>(); var derivedList = new List<DerivedClass>(); baseList.ForEach(v => derivedList.Add((DerivedClass)v)); If you need to check the derived type of each element before casting: