Click here to Skip to main content
13,196,730 members (56,940 online)
Rate this:
Please Sign up or sign in to vote.
public class Eagle : Bird
        public Eagle():base()
        { }
        public Eagle(string name, string id, double age, CategoryType category, GenderType gender, BirdSpecies birdSpecie)
            : base(name, id, age, category, gender)
            birdSpecie = BirdSpecies.Eagle;
        public override string ToString()
            //return string.Format("{0}", base.ToString());
            //return string.Format("{0} {1} {2} {3} {4}", Name, ID, Age, Category,                   Gender);

Why is it that the second formatted string works
return string.Format("{0} {1} {2} {3} {4}", Name, ID, Age, Category,                   Gender);

return string.Format("{0}", base.ToString());

fails to do so. Is it due to some mix-up with the constructors? Is the default empty constructor that is causing the error?
Posted 9-Feb-13 12:06pm
PIEBALDconsult 9-Feb-13 18:18pm
What do you expect to happen?
It really does not matter, taking into account base class based "implementation". So the situation is worse: this code does not make sense no matter why it would be written. Please see my answer where I explain it.
leprechauny 9-Feb-13 18:54pm
Having the input from run-time being print as name, age, cat, gender simply by calling base, instead of the second. Basically, I'm wondering why the second one works and not the first one, ought they not to work in the same way? What am I missing?
Isn't that obvious? If not, I would question if you understand virtual methods at all, maybe not. I answered.
PIEBALDconsult 9-Feb-13 19:43pm
Yeah, that's not going to happen.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

No, there is no "mix-up". The problem is trivial. These two code lines have almost nothing in common, so the "answer" would depend on what you want to achieve. Who told you base.ToString() is supposed to do anything useful, especially in you did not override this method in your base class.

To help your to understand what's going on, I'll tell you: if you return string.Format("{0}", base.ToString()); from your ToString implementation functionally is strictly equivalent to not overriding this method at all. You simply try to return exact same value the base class returns. Isn't that obvious?

leprechauny 9-Feb-13 20:06pm
Well, it is overridden in the base class as well. Somehow I thought this would nicely follow in a descending manner when working with polymorph and inheritance. Thanks, though. For clarifying it.
You are welcome,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171019.1 | Last Updated 9 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100