Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Defining and Calling Simple Methods | Functions with Different Signatures
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Methods & Modular Thinking

bookDefining and Calling Simple Methods

Methods are a fundamental building block in C#. You use methods to break your code into smaller, reusable pieces, making it easier to read, test, and maintain. By organizing your logic into methods, you avoid repeating yourself and make your programs more modular. This modular approach allows you to focus on one piece of functionality at a time and makes your code much easier to understand.

Program.cs

Program.cs

copy
123456789101112131415161718
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { PrintGreeting(); } public static void PrintGreeting() { Console.WriteLine("Hello! Welcome to the C# methods chapter."); } } }

To understand how methods work in C#, look closely at the structure of the PrintGreeting method from the example above. Each method has a few key parts:

  • The return type is written before the method name and tells you what kind of value the method will give back when it runs. If the method does not return anything, you use void.
  • The method name comes after the return type and describes what the method does. In this case, it is PrintGreeting.
  • The parentheses after the method name can include parameters—values you pass into the method to use in its logic. If there are no parameters, the parentheses are empty.
  • The method body, inside curly braces {}, contains the statements that will be executed when you call the method.

You call a method by writing its name followed by parentheses. In the example, PrintGreeting(); is a method call. This tells the program to jump to the PrintGreeting method, run its code, and then come back to where it left off.

Program.cs

Program.cs

copy
123456789101112131415161718
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { PrintPersonalGreeting("Sam"); } public static void PrintPersonalGreeting(string name) { Console.WriteLine("Hello, " + name + "! Glad you're learning C# methods."); } } }
Note
Definition

Definition:
A method in C# is a named sequence of statements that can be executed by calling the method from elsewhere in your code.
A parameter is a value you provide to a method so it can use that value in its logic. Parameters are declared inside the parentheses of a method definition.

1. What is the main purpose of using methods in programming?

2. Which part of a method specifies what type of value it returns?

3. What keyword is used to define a method in C#?

question mark

What is the main purpose of using methods in programming?

Select the correct answer

question mark

Which part of a method specifies what type of value it returns?

Select the correct answer

question mark

What keyword is used to define a method in C#?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 1

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookDefining and Calling Simple Methods

Desliza para mostrar el menú

Methods are a fundamental building block in C#. You use methods to break your code into smaller, reusable pieces, making it easier to read, test, and maintain. By organizing your logic into methods, you avoid repeating yourself and make your programs more modular. This modular approach allows you to focus on one piece of functionality at a time and makes your code much easier to understand.

Program.cs

Program.cs

copy
123456789101112131415161718
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { PrintGreeting(); } public static void PrintGreeting() { Console.WriteLine("Hello! Welcome to the C# methods chapter."); } } }

To understand how methods work in C#, look closely at the structure of the PrintGreeting method from the example above. Each method has a few key parts:

  • The return type is written before the method name and tells you what kind of value the method will give back when it runs. If the method does not return anything, you use void.
  • The method name comes after the return type and describes what the method does. In this case, it is PrintGreeting.
  • The parentheses after the method name can include parameters—values you pass into the method to use in its logic. If there are no parameters, the parentheses are empty.
  • The method body, inside curly braces {}, contains the statements that will be executed when you call the method.

You call a method by writing its name followed by parentheses. In the example, PrintGreeting(); is a method call. This tells the program to jump to the PrintGreeting method, run its code, and then come back to where it left off.

Program.cs

Program.cs

copy
123456789101112131415161718
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { PrintPersonalGreeting("Sam"); } public static void PrintPersonalGreeting(string name) { Console.WriteLine("Hello, " + name + "! Glad you're learning C# methods."); } } }
Note
Definition

Definition:
A method in C# is a named sequence of statements that can be executed by calling the method from elsewhere in your code.
A parameter is a value you provide to a method so it can use that value in its logic. Parameters are declared inside the parentheses of a method definition.

1. What is the main purpose of using methods in programming?

2. Which part of a method specifies what type of value it returns?

3. What keyword is used to define a method in C#?

question mark

What is the main purpose of using methods in programming?

Select the correct answer

question mark

Which part of a method specifies what type of value it returns?

Select the correct answer

question mark

What keyword is used to define a method in C#?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 1
some-alt