C# For Beginner Tutorial – Methods

Welcome to the final section of C # for beginner level, after this section, we will move from beginner to intermediate level.

Methods may require more repetition than other topics before starting the lesson, so study all the topics shown.

Topics To Learn

  • What Is Method?
  • Why We Are Use Methods?
  • What is Static Statement
  • Method Syntax
  • Calling Method
  • Return Statement
  • Method Parameters

What Is Method?

A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of data and behavior; these comprise an interface, which specifies how the object may be utilized by any of its various consumers.

In short, methods are blocks of code that are often used to store transactions that can be used later.

For example, you will add 2 times in your program, you can do it once with the function or you can write code over and over again.

Why We Are Use Methods?

Methods and functions are used to avoid rewriting our code over and over. Just as we use a loop for a job that will be done 100 times, we use methods within the repeating pieces of code.

What is Static?

As long as the static program is running, it is used to access a method or a class, so we can call multiple objects with one method.

In addition, a method that uses static will work as soon as the program runs, since it will be started before the objects you create, the main method is used with static.

Method Syntax

The methods are first determined to be private or public, then if they are to be added to a static environment, a static statement is added and the data is created by specifying the method.

public static void method()
{
   // Code 
}

Let’s start to explain the concepts. All of the concepts we will learn in this section will be used in the following sections.

public or private = determines whether or not accessible from a different class.

static = To use in the static main method

void = It will not return a value because we have given void as the data type.

Now let’s add code to the method and use the method in the main method, let’s see how it will turn out.

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

public static void method()
{
   Console.WriteLine("Hello");
   Console.WriteLine("World");
}
Hello
World

Now we can prevent the confusion in the main method by creating blocks of code. Excessive code in the Main method reduces readability and performance.

Calling Method

After creating a method, we need to call it to use it. To call it, it is enough to write the name and add parentheses, as we did before.

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

You can use the code block you have created in this way in the program by calling it in main. Uncharted code blocks cannot be used in the program.

Return Statement

Sometimes methods may need to return values, for example, you can use a method to return an int, string or bool value.

public static int intMethod()
{
   // Code
}

As you can see, this method should now return an int value, but if we leave the code blank like this, it will give an error.

Because a method with an int set must always return int, a return statement is used. Now let’s see the return statement on the code and find out what it does.

public static int intMethod()
{
   return(1 + 2);
}

Now when method runs, the value of this function will be 3. We can assign it to a variable and use it. Let’s try it.

static void Main(string[] args)
{
   int x = intMethod();
   Console.WriteLine(x);
}
3

We can use not only int but also other data types here, for example string, bool, float data types can also be used.

Method Parameters

We can add parameters inside the brackets of methods. These parameters are entered when the method is called, and the method uses these parameters in this method.

static void Main(string[] args)
{
   Console.WriteLine(stringMethod("David"));
}

public static string stringMethod(string name)
{
   return("Hello " + name);
}
Hello David

As you can see, the string value entered when the calling method is added to the return statement after the method runs.

Default Parameters

You can add values to parameters automatically, that value will be stored in the variable unless changed.

static void Main(string[] args)
{
   Console.WriteLine(stringMethod("David"));
}

public static string stringMethod(string name = "Default Name")
{
   return("Hello " + name);
}
Multiple Parameters

It is also possible to add more than one parameter. You can assign more than one parameter by separating the parameters with commas.

static void Main(string[] args)
{
   Console.WriteLine(stringMethod("David" , 13));
}

public static string stringMethod(string name, int age)
{
   return("Hello " + name + " " + age);
}

Leave a Reply

Your email address will not be published. Required fields are marked *