-2

I'm positive that this has been asked before, but I was not able to find the question since I don't know how to formulate this question in more technical terms, as am learning C# from the 0 level.

class A {}
class B {}
B xyz = new A();

What does 3rd statement actually do? What is the point of initializing class like this?

  • 10
    The statement produces a [compiler error](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0266) because `A` is not assignable to `B`. – DiplomacyNotWar Jun 03 '22 at 09:40
  • 2
    Perhaps you're actually trying to ask [this question](https://stackoverflow.com/questions/31227894/why-would-one-create-a-base-class-object-with-reference-to-the-derived-class), which is also covered [here](https://stackoverflow.com/questions/10734603/why-to-assign-an-instance-of-a-derived-class-to-a-variable-which-type-is-not-the)? – DiplomacyNotWar Jun 03 '22 at 09:42
  • 6
    Are you missing a `: A` in `class B`? You probaby need to have a basic understanding of Polymorphism; to make sense of any potential answer to your question; a good place to start is [here](https://www.w3schools.com/cs/cs_polymorphism.php) – InBetween Jun 03 '22 at 09:49

0 Answers0