How to Create Static and Non-Static Methods with C# Programming Language
A Comprehensive Guide to Implementing Static and Non-Static Methods in C#"
Prerequisite
You should be familiar with the basic principles of the C# programming language, including its key terms, data types, variables, and so forth. Read this detailed guide to discover more Getting Started with C# or
Prior knowledge of any programming language
To understand static and non-static methods in C#, You must be aware that C# is an Object-Oriented Programming (OOP) language that enables programmers to design objects and create classes. Each object derived from a class is referred to as an instance. Let's get into it ππ€
Understanding Methods in CSharp
What is a Method?
A C# method is a block of code that is declared in a class, interface, or struct using access modifiers such as public, protected, internal, etc., return value, method identifier, parentheses (), and curly braces {}.
Main Method
When we execute our application, the Common Language Runtime (CLR) starts compiling the code in the main method of the C# program class and continues until it is completed.
Let's declare a variable of type string for better understanding.
class Program
{
static void Main(string[] args)
{
string name = "Mathematics";
Console.WriteLine(name);
}
}
//output is Mathematics
Method Parameter
A method parameter is a variable specified between parentheses. If there are more than one, they are separated by commas(,).
class Program
{
static void Main()
{
//instantiation
var method = new Method();
//invocation
method.Addition();
method.Subtraction(5,8);
}
}
public class Method
{
public void Addition()
{
int a = 2;
int b = 3;
Console.WriteLine(a + b);
}
public void Subtraction(int x,int y)
{
Console.WriteLine(x - y);
}
}
//output is:
// 5 for addition method
//-3 for subtraction method
In the example above, the Addition method has no parameter(s), while the Subtraction method has a parameter list of type int and arguments 5 and 8, respectively.
Static Methods
In C#, a static method is called directly on its class without creating an object of that same class. It means that there is only one copy of its member identified among all instances of the class.
Here is an example of a static method:
class Program
{
static void Main()
{
Employee.GetName();
}
}
public static class Employee
{
//private static field
private static readonly string _fullName;
//constructor
static Employee()
{
_fullName = "Mustapha Ayisat";
Console.WriteLine("Employee full name is {0}", _fullName);
}
public static string GetName()
{
return _fullName;
}
}
//output
//Employee full name is Mustapha Ayisat
Non-Static Methods (Instance Methods)
Non-static methods can also be referred to as instance methods. They require an instance of the class to be invoked to operate on the unique instance data.
Example of a non-static method
public class Employee
{
//private instance field
private string firstName;
//constructor
public Employee(string firstName)
{
this.firstName = firstName;
}
public string GetName()
{
return firstName;
}
}
Difference between Static methods and Non-Static methods in CSharp
In C#, Static and Non-static methods have various distinct functions. The key distinctions between them are listed below:
Keywords | Static Method | Non-Static Method |
Static keyword | When defining a static method, the static keyword comes before the return type | It is declared without the static keyword but with a return type |
Instantiation | It cannot be instantiated because it is not an instance method. | The method class can be instantiated using the new keyword. |
Declaration | A static method can be declared in both static and instance classes. | An instance method cannot be declared in a static class. |
Accessing Instance Members | The instance variables are not directly accessible or manipulated using this method | Instance variables of the class are directly accessible and can be changed thanks to this. |
Invocation | It is invoked directly on the class itself without creating an object | It is invoked using the dot notation on the object of the class, which is created using the new keyword |
Here's a simple example to illustrate the differences:
class Program
{
static void Main()
{
//Calling the static method directly without an instance.
Method.Student(10, 5);
}
}
//static class
public static class Method
{
//returns compiler error
public void Addition()
{
int a = 2;
int b = 3;
Console.WriteLine(a + b);
}
//static method
public static void Student(int parameter1, int parameter2)
{
int totalStudents = parameter1 + parameter2;
Console.WriteLine("The total number of students in mathematics class is:" + totalStudents);
}
}
//output:
//The total number of students in mathematics class is:15
class Program
{
static void Main()
{
//Creating an instance of the class to call the non-static method.
Book book = new Book();
//calling the instance method on the object book.
book.Novel();
//Calling the static method directly without an instance.
Book.Literature("Lion King", "BestFriends");
}
}
//non-static class
public class Book
{
//non-static method
public void Novel()
{
string name = "Beauty";
Console.WriteLine(name);
}
//static method
public static void Literature(string parameter1,string parameter2)
{
Console.WriteLine("The literature names are: {0} and {1}", parameter1,parameter2);
}
}
//output:
//Beauty
//The literature names are: Lion King and BestFriends
Conclusion
Wow! We made itπ . We can now tell the difference between static and non-static methods. Keep in mind that the design of your application and its particular requirements will determine whether static or non-static approaches should be utilized.