Create a method taking alternative parameter variable types
c#
Solution
It's called an overload, and you just create a method with the same name but different parameters:
/// <summary>
/// Writes a string followed by a newline to the console
/// </summary>
/// <param name="s">The value to write</param>
public void WriteLine(string s)
{
//Do something with a string
}
/// <summary>
/// Writes the string representation of an object followed by a newline to the console
/// </summary>
/// <param name="o">The value to write</param>
public void WriteLine(object o)
{
//Do something with an object
}
To get nice intellisense descriptions, you can add XML Documentation to each method.
Problem
In the Console class of the NET framework, `Console.WriteLine()` takes many different object types in as parameters. This is obvious when typing in Visual Studio and intellisense shows arrow keys with the different data types. Or in methods that take multiple parameters, you can see the intellisense description update depending on what object types are already entered A screenshot to illustrate what I am trying to explain: How do I write a method that can take a multiple types in?