Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Defining and Calling Simple Methods | Functions with Different Signatures
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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookDefining and Calling Simple Methods

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1
some-alt