Methods
Explore how to define and use methods in C#. Understand method syntax, how to call methods, and the difference between void methods and those that return values. This lesson helps you write reusable code by implementing simple and parameterized methods with return types.
We'll cover the following...
Create methods
A variable is a named area of memory that holds a value of some type. Methods are similar, but instead of holding a value, methods are named blocks of instructions.
The basic syntax for creating a method is as follows:
[modifiers] return_type method_name([parameters]){// Method body}
modifiers: Optional keywords (likestaticorpublic) that define accessibility and behavior.return_type: The data type of the value the method sends back (orvoidif it returns nothing).method_name: The identifier used to call the method.parameters: Optional input variables defined within parentheses.Method body: The code block enclosed in curly braces
{}.
Beyond using the Console.WriteLine() method to print text, we have already used one method that serves as the entry point in structured .NET programs:
static void Main(string[] args){}
...