Click here to Skip to main content
11,718,589 members (84,276 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# format Enum strings
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 at 9-Feb-13 18:18pm
What do you expect to happen?
Sergey Alexandrovich Kryukov at 9-Feb-13 19:18pm
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 at 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?
Sergey Alexandrovich Kryukov at 9-Feb-13 19:19pm
Isn't that obvious? If not, I would question if you understand virtual methods at all, maybe not. I answered.
PIEBALDconsult at 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 at 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.
Sergey Alexandrovich Kryukov at 9-Feb-13 20:13pm
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
0 Sergey Alexandrovich Kryukov 651
1 OriginalGriff 210
2 F-ES Sitecore 185
3 Jochen Arndt 160
4 Maciej Los 130
0 OriginalGriff 535
1 Maciej Los 529
2 Richard MacCutchan 440
3 CHill60 415
4 F-ES Sitecore 415

Advertise | Privacy | Mobile
Web04 | 2.8.150901.1 | Last Updated 9 Feb 2013
Copyright © CodeProject, 1999-2015
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