C# Methods

What is a Method?

A method is a block of code that performs a specific task.

Methods help you organize your code into reusable pieces instead of writing the same code multiple times.

Loading...
Output:

Calling a Method

After defining a method, you need to call it to run the code inside it.

The method name followed by parentheses executes the method.

Loading...
Output:

Methods with Parameters

Methods can take parameters, which are inputs passed into the method.

This allows methods to work with different values each time they are called.

Loading...
Output:

Return Values

Some methods return a value using the return keyword.

Instead of just printing something, these methods send a result back to where they were called.

Loading...
Output:

Void vs Return Methods

A void method does not return a value. It simply performs an action.

A method with a return type sends a value back.

Loading...
Output:

Method Overloading

Method overloading allows you to create multiple methods with the same name but different parameters.

This makes your code cleaner and easier to use.

Loading...
Output:

Why Use Methods?

Methods are essential for writing clean and maintainable code.

  • Reduce repetition
  • Improve readability
  • Make debugging easier
  • Allow code reuse

Practice

Create a method that takes two numbers and returns their average.

Loading...
Output:

Need Help?