Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What are Methods? | Methods
C# Basics
course content

Course Content

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

What are Methods?

If you're familiar with other programming languages, you might have come across the concept of functions. Methods are very similar to functions however they are tied to a class. You don't need to know about classes at this level.

In C#, when we want to reuse a piece of code many times at relevant places then we use something called a method.

Consider a method like a labeled block of code which we can execute on demand whenever we want:

If you look at the above image, we used countToTen as a reference for the boxed code on the right. Every method has a name, and we can execute the code in that method using the following syntax methodName();. There are cases where the syntax will be a bit different but we will discuss the syntax in detail in later chapters. Over here, the main idea is that a piece of code was labeled as countToTen and we executed it multiple times by writing countToTen() instead of writing the while code.

Did You Know?

In programming, it is a good practice to follow the DRY principle. DRY means Don't Repeat Yourself. Therefore, methods and loops are an excellent method of eliminating repetitive code.

What is the closest definition of a method in C#?

Select the correct answer

Everything was clear?

Section 6. Chapter 1
We're sorry to hear that something went wrong. What happened?
some-alt