Learn static method in C# with example i.e. how to write static method in a class and why do we use static methods in C# programs.

Before explanation, see how we make a method static in a class. It’s simple, just write static keyword before the method.

class Utility {
	
	//static method
	public static void Scan(){
		System.out.println("static method in java");
	}
}

“Call Static methods of a class using class name without creating object”

We can call static methods of a class using only class name. We don’t need to create an object of the class in C# program and then call the static method.

Note that if method is not static, then you must create an object of the class then call the method. This is not the case with static method.

Below code example, demonstrate calling static method without creating an object of the class.

Example-1 of static method in C#

class Utility
    {

        // static method
        public static void Scan() {
            Console.WriteLine ("static method in C#");
	}
    }

    public class Sample
    {

        public static void Main(String[] args)
        {

            Utility.Scan();

        }
    }

Output
static method in C#

“Use static method if you don’t need to create an object of the class.”

Here is one of the examples you can see, where static method is mandatory.

Notice that the main () method, the starting point of the java programs is static. So, the CLR-(Common Language Runtime) can call the main () without creating an object of the class Sample.

public class Sample {
 
	public static void main(String[] args) {
		
		System.out.println("Hello World");
 
	}
}

Example-2 of static method in C#

  /*
 * Static method example C# program
 */
    /*
     * In this program we basically conclude that the
     * static method will execute before the execution of any other method. 
     */

   public class EmpDetails
    {
        private int salary;
        private String name;
        private static int id = 1021;

        // static method in which we are changing the
        // employee id statically.
      public  static void ChangeId()
        {
            id = 2017;
        }

        // constructor with salary and name passed as an arguments
      public  EmpDetails(int salary, String name)
        {
            this.salary = salary;
            this.name = name;
        }

        // non parameterized constructor
      public  EmpDetails()
        {

        }

        // ShowEmpDetails() showing the outcomes of the program
      public  void ShowEmpDetails() {
            Console.WriteLine ("Name of the employee is:" + name
				+ " earning a salary of $:" + salary + " whose Empid is:" + id);
	}
    }

    // client class
    public class Employee
    {
        public static void Main(String[] args) {
		// static method calling directly with the class name
		// it is also showing that static methods calls
		// before any other methods..
		EmpDetails.ChangeId();
        Console.WriteLine("Executed before any other method execution:");
 
		// passing the value to match the constructor
		EmpDetails ed = new EmpDetails(4000, "Abhinav");
		EmpDetails ed1 = new EmpDetails(5000, "Aisha");
 
		// calling methods with passing the references of the class
		ed.ShowEmpDetails();
		ed1.ShowEmpDetails();
	}

    }

Output

Executed before any other method execution:
Name of the employee is:Abhinav earning a salary of $:4000 whose Empid is:2017
Name of the employee is:Aisha earning a salary of $:5000 whose Empid is:2017

Related Posts