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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:36:44+00:00 2026-06-15T20:36:44+00:00

Would someone care to explain how this code produces the folowing output? using System;

  • 0

Would someone care to explain how this code produces the folowing output?

using System;

namespace ConsoleApplication1
{
    class Test
    {
        public override string ToString() { return "ToString override"; }
        public string ToString(string optional = "")
          { return String.Format("ToString with optional parameter {0}", optional); }
    }

    class Test2
    {
        public new string ToString() { return "ToString new"; }
        public string ToString(string optional = "")
          { return String.Format("ToString with optional parameter {0}", optional); }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test one = new Test();
            Test2 two = new Test2();
            Console.WriteLine(one);
            Console.WriteLine(one.ToString());
            Console.WriteLine(one.ToString("foo"));
            Console.WriteLine("--");
            Console.WriteLine(two);
            Console.WriteLine(two.ToString());
            Console.WriteLine(two.ToString("bar"));
            Console.ReadKey();
        }
    }
}

ToString override

ToString with optional parameter

ToString with optional parameter foo

—

ConsoleApplication1.Test2

ToString new

ToString with optional parameter bar

  • 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-15T20:36:45+00:00Added an answer on June 15, 2026 at 8:36 pm

    Okay, as there’s general interest, here’s a quick version:

    Console.WriteLine(one)

    This will use the WriteLine(object) overload, which will in turn execute the object.ToString() virtual method, overridden in One – hence the output of ToString override

    Console.WriteLine(one.ToString())

    This will look at One and see which methods have newly declared methods – discounting overrides. There’s exactly one such method which is applicable – the one with the optional parameter. So that gets executed, using the default value, leading to output of ToString with optional parameter.

    Console.WriteLine(one.ToString(“foo”))

    Same again, but this time the compiler doesn’t need to use the default value, hence ToString with optional parameter foo

    Console.WriteLine(two)

    Again, this will call the virtual object.ToString() method from WriteLine(object). The method hasn’t been overridden, so the default implementation returning the name of the type is used, leading to output of ConsoleApplication1.Test2.

    Console.WriteLine(two.ToString())

    The compiler looks at all the method declared in Two which aren’t overriding virtual methods. In this case, there are two such methods – the parameterless one and the one with the optional parameter. The parameterless one is included because it’s new rather than overriding a base class method.

    The parameterless method is deemed a “better” candidate because the compiler prefers to call a method which doesn’t need any optional parameters filling in. Hence the output is ToString new

    Console.WriteLine(two.ToString(“bar”))

    Again, the compiler looks at all the method declared in Two which aren’t overriding virtual methods. In this case, there are two such methods – but the parameterless one isn’t applicable, leaving just the one with the optional parameter. The compiler doesn’t need to use the default value of the optional parameter here, as it’s got an argument anyway… so the output is ToString with optional parameter bar

    For much more on this, read the C# language specification – or for a half-way house, see my article on overloading.

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

Sidebar

Related Questions

would someone please write this code: this.Loaded += (s, e) => this.loaded = true;
Would someone kindly explain why the following bit of code works, I've tested it
Would someone care to explain the difference to be between a VertexBuffer , a
I'm not understanding this behavior. Maybe someone can explain to me why my current
I have this line of code in MATLAB, written by someone else: c=a.'/b I
Would someone be able to break down the Ruby specifics of what each of
Would someone be able to help here? I would like to fill a div
would someone be able to help me with a button to clear /delete the
How would someone go about adding check boxes to only the children of a
Hi i am a c# novice would someone politely tell me how to convert

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.