You’ve been using methods since you started learning C#! Commands like
Math.Min() are methods.
Each method has a different behavior: The first method prints something to the console, and the second finds the smallest of two given numbers. We activate a method’s behavior by calling it. In C# we do this by adding parentheses to the end of a method name.
Some methods accept inputs called arguments.
Console.WriteLine() accepts one string argument. That argument will be printed to the console.
// This prints "I'm hungry!" Console.WriteLine("I'm hungry!");
Other methods accept multiple arguments, like
Math.Min(). It expects two number inputs.
You’ve probably seen built-in methods for each data type too. Every string has access to methods like
string name = "beatrice"; name.Substring(0, 3); // returns "bea"
Math.Min() with two arguments (any two integers will do). You can view the documentation if you’re not sure how to use this method.
Console.WriteLine() using the variable
msg as an argument.
Get the first letter of the
msg string using
Substring(). You can view the documentation if you’re not sure how to use this method.