Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Erstellen und Aufrufen von Methoden | Methoden
C# Grundlagen

bookErstellen und Aufrufen von Methoden

In this chapter, we will delve into the creation and invocation of methods in C#. Methods are essential building blocks in programming, allowing us to encapsulate code for reuse and better organization. Let's explore the syntax and practical examples to understand how methods work.

Method Syntax

A basic method in C# can be defined using the following syntax:

static returnDataType MethodName(parameters)
{
    // Code to be executed when the method is called
}
  • static: indicates that the method belongs to the class itself rather than an instance of the class;
  • returnDataType: specifies the type of data the method will return. Use void if no data is returned;
  • MethodName: the name of the method, which should be descriptive of its function;
  • parameters: optional inputs to the method, enclosed in parentheses.

A Simple Method

Let's create a simple method called PrintHello that prints a greeting message:

main.cs

main.cs

copy
1234
static void PrintHello() { Console.WriteLine("Hello, World!"); }

To call this method, simply use:

main.cs

main.cs

copy
1
PrintHello();

The result of execution such method is following:

main.cs

main.cs

copy
1234567891011121314151617
using System; namespace ConsoleApp { class Program { static void PrintHello() { Console.WriteLine("Hello, World!"); } static void Main(string[] args) { PrintHello(); } } }

Method with a Loop

Consider a method CountToTen that prints numbers from 1 to 10:

main.cs

main.cs

copy
1234567
static void CountToTen() { for (int i = 1; i <= 10; i++) { Console.WriteLine(i); } }

Invoke this method using:

main.cs

main.cs

copy
1
CountToTen();

Method in a Class

Methods are often part of a class. Here's how CountToTen fits into a simple program:

main.cs

main.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { class Program { static void CountToTen() { for (int i = 1; i <= 10; i++) { Console.WriteLine(i); } } static void Main(string[] args) { CountToTen(); } } }

In this example, CountToTen is a static method within the Program class. The Main method is the entry point of the program, where CountToTen is called.

Understanding methods is crucial for writing efficient and organized code. As you progress, you'll learn about methods with parameters and return types, enhancing your ability to create dynamic and reusable code blocks.

question mark

What will be the output of the following code? (This quiz can be a lesson in itself that meaningful method naming matters)

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 6. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain how to add parameters to a method in C#?

What is the difference between static and non-static methods?

Can you show an example of a method that returns a value?

bookErstellen und Aufrufen von Methoden

Swipe um das Menü anzuzeigen

In this chapter, we will delve into the creation and invocation of methods in C#. Methods are essential building blocks in programming, allowing us to encapsulate code for reuse and better organization. Let's explore the syntax and practical examples to understand how methods work.

Method Syntax

A basic method in C# can be defined using the following syntax:

static returnDataType MethodName(parameters)
{
    // Code to be executed when the method is called
}
  • static: indicates that the method belongs to the class itself rather than an instance of the class;
  • returnDataType: specifies the type of data the method will return. Use void if no data is returned;
  • MethodName: the name of the method, which should be descriptive of its function;
  • parameters: optional inputs to the method, enclosed in parentheses.

A Simple Method

Let's create a simple method called PrintHello that prints a greeting message:

main.cs

main.cs

copy
1234
static void PrintHello() { Console.WriteLine("Hello, World!"); }

To call this method, simply use:

main.cs

main.cs

copy
1
PrintHello();

The result of execution such method is following:

main.cs

main.cs

copy
1234567891011121314151617
using System; namespace ConsoleApp { class Program { static void PrintHello() { Console.WriteLine("Hello, World!"); } static void Main(string[] args) { PrintHello(); } } }

Method with a Loop

Consider a method CountToTen that prints numbers from 1 to 10:

main.cs

main.cs

copy
1234567
static void CountToTen() { for (int i = 1; i <= 10; i++) { Console.WriteLine(i); } }

Invoke this method using:

main.cs

main.cs

copy
1
CountToTen();

Method in a Class

Methods are often part of a class. Here's how CountToTen fits into a simple program:

main.cs

main.cs

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { class Program { static void CountToTen() { for (int i = 1; i <= 10; i++) { Console.WriteLine(i); } } static void Main(string[] args) { CountToTen(); } } }

In this example, CountToTen is a static method within the Program class. The Main method is the entry point of the program, where CountToTen is called.

Understanding methods is crucial for writing efficient and organized code. As you progress, you'll learn about methods with parameters and return types, enhancing your ability to create dynamic and reusable code blocks.

question mark

What will be the output of the following code? (This quiz can be a lesson in itself that meaningful method naming matters)

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 6. Kapitel 2
some-alt