The main purpose of "Dot Net Labs" provide the dot net program. You can learn dot net, .Net Core, C#, SQL, Linq step-by-step.

Saturday, 27 May 2023

Methods or Functions in C#


In C#, Methods and functions are both the same thing. In the article, we will learn everything about this topic. We will learn how to define a method in C#, what method is used for, how to call a method, and what different keywords are used with a method in C#.

Method

  • A method is a group of statements that together perform a task.
  • Methods are functions declared in a class and may be used to perform operations on class variables
  • They are blocks of code that can take parameters and may or may not return a value.
  • It is used to perform specific task.
  • Methods are reusable.
Remember: Every C# program has at least one class with a method named Main

Example:
How to create a method 

    class Program

    {

        //Declaring a method

        public void MyMethod()

        {

           //method body

        }

    }

public, it is an access specifier means it makes an accessible method.
void, it specifies what type of value a method returns. This method will not return any value.
MyMethod() is the name of the method which has a simple method and works as an identifier.

How to call a Method

If you have created a method named MyMethod() method then you need to call it. For calling you have to write the method's name followed by two parentheses () and a semicolon ; please see the example below.

    class Program
    {
        //Declaring a method
        public void MyMethod()
        {
            Console.WriteLine("My name is Method");
            Console.WriteLine("Methods are functions declared in a class only");
        }
 
        static void Main(string[] args)
        {
            //create class object
            Program program = new Program();

            program.MyMethod(); //Calling method

            Console.ReadLine();
        }
    }

        Output:
        My name is Method
        Methods are functions declared in a class only

You can call the method many times as per your requirement 
    
    class Program
    {
        //Declaring a method
        public void MyMethod()
        {
            Console.WriteLine("My name is Method");
            Console.WriteLine("Methods are functions declared in a class only");
        }
 
        static void Main(string[] args)
        {
            Program program = new Program();
            program.MyMethod(); //Calling method
            program.MyMethod(); //Calling method
            program.MyMethod(); //Calling method
            program.MyMethod(); //Calling method
            program.MyMethod(); //Calling method
            Console.ReadLine();
        }
    }

Method Parameters and Arguments
Parameters are variables or placeholders defined in a function or method declaration. Parameters are specified within the parentheses following the function or method name. They help define the input requirements and behavior of the function.

Arguments are the actual values passed to a function or method when it is called. When a function is called, the arguments provide the specific values that will be assigned to the function's parameters. The number and order of arguments must match the number and order of parameters defined in the function or method declaration.

we can also create a method with a single parameter

You can create a method with a single parameter or multiple parameters. In above picture you can see the method with a single parameter. Now you will see how can create a method with multiple parameters

using System;
using System.Xml.Linq;
 
namespace SoftwareEngineerDotNet
{
    class Program
    {
        //Declaring a method with multiple parameters
        public void MyMethod(string name, int age)
        {
            Console.WriteLine("Your name is "+ name);
        }
 
        static void Main(string[] args)
        {
            Program program = new Program();
            program.MyMethod("Amit Kumar", 35); //Calling method with multiple arguments
            program.MyMethod("John", 40); //Calling method with multiple arguments
            Console.ReadLine();
        }
    }
}

Many parameters should be separate with commas.
Method calls must have the same number of arguments as there are parameters  and the arguments must be passed in the same order and datatype.

What is default parameter value in C#

You can create a default parameter value using the equals sign ( = ). It is also known as an optional parameter. If you call the method without an argument then it will use default value.

default parameter value in c#

Method Return Values

Method may or may not return a value, we used void in above exmaple which indicates that the method should not return a value. If you want method return any value you can use primitive data types like int, double and use the return keyword inside the method.

how to return method in c#

Below is an example of two data parameters with return type method

class Program
    {
        //Declaring a method with the return value, multiple parameters
        public int Sum(int x, int y)
        {
            return 10 + x + y;
        }
 
        static void Main(string[] args)
        {
            Program program = new Program();
            Console.WriteLine(program.Sum(5, 10)); //Calling method
            Console.ReadLine();
        }
    }

Below is an example of two data parameters with return string but the return type is int

    class Program
    {
        //Declaring a method
        public int Sum(int x, int y)
        {
            return x + y + "Amit"//Compile time error
        }
 
        static void Main(string[] args)
        {
            Program program = new Program();
            Console.WriteLine(program.Sum(5, 10)); //Calling method
            Console.ReadLine();
        }
    }

Below is an example of a method where you can store the result in a variable.

    class Program
    {
        //Declaring a method
        public int Sum(int x, int y)
        {
            return x + y;
        }
 
        static void Main(string[] args)
        {
            Program program = new Program();
 
            //Store the result in a variable
            int z = program.Sum(5, 10);
 
            Console.WriteLine(z); //Calling method
            Console.ReadLine();
        }
    }


0 comments:

Post a Comment

Do not enter spam link

Popular Posts