Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:18:28+00:00 2026-06-10T18:18:28+00:00

I have two classes ClassA and CLassB. ClassA is an abstract class. ClassB derives

  • 0

I have two classes ClassA and CLassB. ClassA is an abstract class. ClassB derives from ClassA. I have one DTO named ParentDTO.

public class ParentDTO
{
    public int UserId { get; set; }
}

public abstract class ClassA
{
    public abstract void CreateUser(ParentDTO dto);
}

public class ClassB : ClassA
{
    public override void CreateUser(ParentDTO dto)
    {
        Console.WriteLine("You are in class ClassB");
    }
}

Now I have one DTO(MyDTO), which derives from ParentDTO.

public class MyDTO: ParentDTO
{
    public int MyID { get; set; }
}

I have extended ClassB further:

public class ClassC : ClassB
{
    public override void CreateUser(ParentDTO dto)
    {
        var mydto = (MyDTO) dto;// This throws cast exception
        mydto.MyID=222;
    }
}

I am using the above code as :

    ClassC c = new ClassC();
    ParentDTO dto=new ParentDTO();
    c.CreateUser(dto);

Can someone please tell me how to cast DTO above in CreateUser method of ClassC. I want to use mydto.MyID in my code. I know I am doing something like casting Animal to Lion instead of Lion to Animal. But is there any way to use child DTO ID? Can someone please tell me what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:18:29+00:00Added an answer on June 10, 2026 at 6:18 pm

    The other answers are correct, but the reason for this behavior would be clearer with a better understanding of the difference between “static type” and “run-time type”, which they do not discuss. Static type is the type of a variable in your C# code, while run-time type is the type of an actual object in memory.

    We all know that static type and run-time type may differ. Consider:

    class A { }
    class B : A { }
    
    A a = new B();
    

    The variable a refers to an instance of type B. The static type of the variable is A, but the run-time type of the object is B.

    We can also think of a as holding a reference of type A that refers to an instance of type B. This is allowed if and only if type B is derived from (or equal to) type A.

    The following, therefore, is not allowed:

    B b = new A();
    

    Here, you are attempting to assign a reference to an instance of type A (run-time type) to a variable of (static) type B. A is not derived from or equal to B, so the assignment is not allowed. Similarly, the following assignments are not allowed:

    string s = new object();
    MyDTO dto = new ParentDTO();
    

    The cast operator complicates things somewhat, but the underlying issue is the same: you are attempting to set a reference to point to an object whose run-time type is not derived from or equal to the static type of the reference:

    A a1 = new A();
    B b1 = new B();
    
    A a2 = (A)b1;  // fine, because B is derived from A
    B b2 = (B)a1;  // not allowed, because A is not derived from B.
    

    Again, this might make more sense if we substitute object for A and string for B:

    object a1 = new object();
    string b1 = "Hello, World!";
    
    object a2 = (object)b1;  // a2 now points to the string "Hello, World!";
    string b2 = (string)a1;  // cast fails: the string variable can't point to an instance of `object`
    

    Similarly, with your types:

    ParentDTO a1 = new ParentDTO();
    MyDTO     b1 = new MyDTO();
    
    ParentDTO a2 = (ParentDTO)b1;  // works
    MyDTO     b2 = (MyDTO)a1;;     // doesn't
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes (MVC view model) which inherits from one abstract base class.
Suppose we have two classes: class Base { private: int x; public: void f();
I have two classes: public class MultilingualString { public int Id { get; set;
I have two classes: abstract Entity , abstract ClassA extends Entity , ClassB extends
I have two classes that are derived from an abstract generic class, which is
I have an abstract class Airplane, and two classes PassengerAirplane and CargoAirplane, which extend
I have two classes that I am testing (let's call them ClassA and ClassB).
I have two classes, ClassA and ClassB , as well as a many to
my code is like this: i have two classes first class: public class Box<E>
I have two classes class A { public string something { get; set; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.