Show C# Static and Instance methods call with simple program

(Last Updated On: September 16, 2018)

Answer:In C#, Static method is called using class name and instance method (non – static) must be called using a class object in a C# program.

Static method is also known as class method and is not a part of an object of a class. All instance method, also called as non-static methods are part of an object and must be called by an object of a class in C#.

In below class Car, run () method is static and is being called by class name and engine () method is non-static method that is being called by using an object.

 

 

NOTES: If you call static method using an object the compiler will complain.

As a good programming practice, if all methods of a class can be called by using class name only, solve our purpose and object is not required, it is better to have static function to avoid memory uses and unnecessary overhead of object creation. Also, make the constructor private of the class, so no one can create object.

c# code example:

 

NOTE: This concept can also be explained for the interview question static method vs non-static method in C# programming.