Skip to content

C# Methods Tutorial

Methods in .NET 7.0
Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

A beginner-friendly C# Methods tutorial

Hi and welcome to this beginner-friendly C# Methods tutorial!

Time is short and valuable as always, so let’s dive right in!

A method is a block of code that performs a specific task, and it can be called by other parts of a program. In this article, we’ll cover the basics of methods in C#, including their definition, static and non-static types, calling a method, and return types. By understanding these concepts, you’ll be able to create and use methods in your own .NET 7.0 programs.

 

Definition of a method in C#

In C#, a method is defined as a block of code that performs a specific task. The general syntax of a method definition in C# is as follows:

The access modifier determines the visibility of the method, whether it can be accessed from outside the class or only from within the class. The return type specifies the type of value that the method will return. The method name is a unique identifier for the method, and the parameter list specifies the input parameters that the method requires.

 

Static and non-static methods

In C#, methods can be classified as static or non-static. Static methods are associated with the class itself, while non-static methods are associated with instances of the class.

Static methods are defined using the static keyword in the method signature. They can be called without creating an instance of the class. They are often used for utility functions that perform a specific task, such as mathematical calculations or string manipulations. Here’s an example of a static method that calculates the area of a rectangle:

Non-static methods, on the other hand, require an instance of the class to be created before they can be called. They are often used for operations that require access to the state of an object. Here’s an example of a non-static method that sets the color of a car object:

 

Calling a method

Once a method has been defined, it can be called by other parts of the program. To call a method in C#, you need to specify the method name and provide any required parameters. Here’s an example of how to call the CalculateArea method from earlier:

This code would calculate the area of a rectangle with a length of 5 and a width of 10, and assign the result to the area variable.

If the method is non-static, you first need to create an instance of the class that contains the method. Here’s an example of how to create an instance of a car class and call the SetColor method:

This code would create a new instance of the Car class, and then set the color of the car to “red”.

 

Method Return types

Methods in C# can have a return type, which specifies the type of value that the method will return when it completes its task. If a method doesn’t return a value, its return type is void.

Here’s an example of a method that returns a value:

This code would take two integer values as input parameters, add them together, and then return the result as an integer.

When calling a method that returns a value, you can store the result in a variable, like this:

This code would call the GetSum method, passing in 5 and 10 as input parameters, and then assign the result to the sum variable. The sum variable would then contain the value 15.

In addition to returning simple data types like integers and doubles, methods in C# can also return complex data types like arrays, lists, and custom objects.

Here’s an example of a method that returns an array:

This code would create an array of integers with a length specified by the count parameter, fill the array with numbers from 0 to count-1, and then return the array.

Here’s an example of a method that returns a custom object:

This code would search a database for a person with the given name, create a new Person object with the information from the database, and then return the Person object.

Conclusion: C# Methods Tutorial

And that’s it for our C# Methods Tutorial. You now have decent knowledge of methods, a fundamental concept in C# programming. If you want to learn more about C#, check out our C# related blog here.

Keep in mind, they are used to encapsulate functionality, make code more reusable, and improve the overall organization of a program. In this article, we covered the basics of methods in C#, including their definition, static and non-static types, calling a method, and return types. By understanding these concepts, you’ll be able to create and use methods in your own C# programs.

 

If you feel overwhelmed with coding, check out our developer membership (seriously, it’s worth it!). We help you master coding fast and easily.

 

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide