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 880445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:05:57+00:00 2026-05-15T12:05:57+00:00

public class Parent { public virtual Parent me() { return this; } } public

  • 0
public class Parent
{
    public virtual Parent me()
    {
        return this;
    }
}

public class Child : Parent
{
}

new Child().me() is returning a Parent object. What do i need to have it return the Child object itself (without using extension and generic)??

  • 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-05-15T12:05:58+00:00Added an answer on May 15, 2026 at 12:05 pm

    The me method is returning a reference to the actual object, which is of the type Child, but the type of the reference is of the type Parent.

    So, what you have is a reference of the type Parent that points to an object of the type Child. You can use that to access any members that the Child class inherits from the Parent class. To access the members of the Child class you have to cast the reference to the type Child:

    Child c = (Child)someObject.me();
    

    You could make the me method return a Child reference and do the casting inside the method, but then it will of course not work to return a reference to a Parent object. If you don’t use generics, each method can only have one return type. Even if you override the method in the Child class, it still has to return the same data type as in the Parent class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a object model like this public class Parent{ public int Id; public
I have 2 objects like this public class Child { public virtual int ChildId
I have two entities: public class Parent() { public ICollection<Child> Children { get; set;
I have this entities: public class Parent: AllDependant { /*Properties goes here*/ } public
Supposed I have two classes like this: public class Parent { public string Id
New to FluentNHibernate =D I have a parent/children classes as follows: public class Parent
I have a model that looks something like this: public abstract class Parent {
I have two related POCOs public class Parent { public Guid Id {get; set;}
Let's say I have these class hierarchy : public abstract class Parent { }
i'm trying to assign a parent's variable from the parent's child //Parent public class

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.