Hi,
Please give me some simple example for type conversion between two classes in C#
Thanks in advance
Sathish
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh TrivediRank #16 · 50.3K reputationPosted Jan 22, 2014, 2:41 AM EST
hi,
what do you mean by type casting between classes?
are you saying or looking solution for convert one class object to other class object?
so i think it is not possible you have to cast it property by property or you can use reflection.
hope this will help you.
VulpesRank #55 · 29.2K reputationPosted Jan 22, 2014, 4:46 AM EST
Eric Zimmerman25 reputationPosted Jan 22, 2014, 7:37 PM EST
One thing that comes to mind is base class + several derivatives...
e.g.
class AnimalBase;
class Donkey : AnimalBase
class Tiger : AnimalBase
void SomeFunction(AnimalBase animal)
{
if (animal is Tiger)
{
Tiger tiger = animal as Tiger;
tiger.DoSomethingTigerish();
}
}
sathishRank #4,713 · 329 reputationPosted Jan 26, 2014, 11:35 PM EST