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

Course Content

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

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:

  • 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:

cs

main

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

To call this method, simply use:

cs

main

copy
1
PrintHello();

The result of execution such method is following:

cs

main

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:

cs

main

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

Invoke this method using:

cs

main

copy
1
CountToTen();

Method in a Class

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

cs

main

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.

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

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 6. Chapter 2
We're sorry to hear that something went wrong. What happened?
some-alt