Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Creating and Calling Methods | Methods
C# Basics

book
Creating and Calling Methods

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:

csharp
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
static void PrintHello()
{
Console.WriteLine("Hello, World!");
}
1234
static void PrintHello() { Console.WriteLine("Hello, World!"); }

To call this method, simply use:

main.cs

main.cs

copy
PrintHello();
1
PrintHello();

The result of execution such method is following:

main.cs

main.cs

copy
using System;

namespace ConsoleApp
{
class Program
{
static void PrintHello()
{
Console.WriteLine("Hello, World!");
}

static void Main(string[] args)
{
PrintHello();
}
}
}
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
static void CountToTen()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
}
1234567
static void CountToTen() { for (int i = 1; i <= 10; i++) { Console.WriteLine(i); } }

Invoke this method using:

main.cs

main.cs

copy
CountToTen();
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
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();
}
}
}
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)

static void countThree()
{
for (int i = 1; i < 5; i++)
{
Console.Write(i + " ");
}
Console.WriteLine("");
}

static void Main(string[] args)
{
countThree();
}

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 6. Kapitel 2

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

some-alt