Export (0) Print
Expand All
Expand Minimize

Code: Simulating Default Parameters (Visual C#)

Visual Studio .NET 2003
 

This example demonstrates the use of method overloading to simulate default parameters, which is not allowed in C#.

Example

class MyClass
{
    static string myMethod(string precip, string country, string location)
    {
        return string.Format("The {0} in {1} stays mainly in the {2}.",
            precip, country, location );
    }

    static string myMethod(string precip, string country )
    {
        return myMethod(precip, country, "plain");
    }

    static string myMethod()
    {
        return myMethod("rain", "Spain", "plain");
    }
 
    static void Main(string[] args)
    {
        Console.WriteLine(myMethod());
        Console.WriteLine(myMethod("snow", "Walla Walla"));
    }
}

Compiling the Code

Copy the class and paste it over Class1 in a console application.

See Also

Comparison Between C++ and C# | Compiler Error CS0241 | General Language Example Topics

Show:
© 2014 Microsoft