Зміст курсу
C# Basics
C# Basics
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.
Дякуємо за ваш відгук!