Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Overriding and Optional Parameters

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

like image 654
pneuma08 Avatar asked Dec 11 '12 17:12

pneuma08


People also ask

What is a optional parameter?

A method that contains optional parameters does not force to pass arguments at calling time. It means we call method without passing the arguments. The optional parameter contains a default value in function definition. If we do not pass optional argument value at calling time, the default value is used.

What are the differences between mandatory parameters and optional parameters?

A mandatory parameter must be explicitly given on the command-line, otherwise an error message will be printed to prompt the user to re-enter the command. If an optional parameter is not specified, the default is used. Note: Mandatory parameters have a default value for use in the GUI.

How do you pass optional parameters to a method?

By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method.

What is an optional parameter in a function?

What are Optional Parameters? By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.


1 Answers

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.

like image 71
Jon Skeet Avatar answered Oct 07 '22 21:10

Jon Skeet