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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:11:35+00:00 2026-05-11T13:11:35+00:00

I have a problem (it’s my fault, I just can’t spot what I’m doing

  • 0

I have a problem (it’s my fault, I just can’t spot what I’m doing wrong) where ‘ToString’ isn’t calling the correct method…

public class ClassA {    public override ToString()    {       return 'Hello, I'm class A.';    } }  public class ClassB : ClassA {    public override ToString()    {        return 'Hello, I'm class B.';    } }  ClassB myClassB = new ClassB(); List<ClassA> myClassAList = new List<ClassA>();  myClassAList.Add((ClassA) myClassB); ClassA tempClassA = myClassAList[0]; Console.WriteLine(tempClassA.ToString()); 

I’m getting the ‘ToString’ from ‘ClassB’ and not ‘ClassA’ 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. 2026-05-11T13:11:36+00:00Added an answer on May 11, 2026 at 1:11 pm

    You are overriding ToString in ClassB instead of hiding it from the original which will cause the overridden method to take precedence. What you could do is..

    public class ClassA {     public override string ToString()     {         return 'Hello, I'm class A.';     } }  public class ClassB : ClassA {     public new string ToString()     {         return 'Hello, I'm class B.';     } }  ...  List<ClassA> theList = new List<ClassA> {     (ClassA)new ClassB(),     (ClassA)new ClassB() };  ClassA a = theList[0]; Console.WriteLine(a.ToString());  // OR...   Console.WriteLine(new ClassA().ToString());  // I'm Class A Console.WriteLine(new ClassB().ToString());  // I'm Class B Console.WriteLine(((ClassA)new ClassB()).ToString()); // I'm Class A 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with my code. I don't know what am I doing wrong.
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with Android using the class HttpUrlConnection my method is this: public
I have problem with context menu. I have got: @Override public void onCreateContextMenu(ContextMenu menu,
i have problem i can't solve for a while. For one of my C++
I have problem in Listview onListItemClick() method. I am not getting the position of
I have problem with this method in NLog library: NLog.Targets.Wrappers.AsyncTargetWrapper.ProcessPendingEvents(object state) It consume too
I have problem with loading my jqGrid. It just loads two parallel lines, and
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This

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.