Skip to content

C# Methods Tutorial

Methods in .NET 7.0

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:

access_modifier return_type method_name(parameter_list)
{
  // method body
}

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:

public static double CalculateArea(double length, double width)
{
  return length * width;
}

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:

public void SetColor(string color)
{
  this.color = color;
}

 

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:

double area = CalculateArea(5, 10);

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:

Car myCar = new Car();
myCar.SetColor("red");

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:

public int GetSum(int a, int b)
{
  return a + b;
}

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:

int sum = GetSum(5, 10);

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:

public int[] GetArrayOfNumbers(int count)
{
  int[] numbers = new int[count];

  for (int i = 0; i < count; i++)
  {
    numbers[i] = i;
  }

  return numbers;
}

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:

public Person GetPersonByName(string name)
{
  // search for person with the given name in a database
  // create a new Person object with the information from the database
  // return the Person 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.