Essential Object Oriented Programming concepts in C#
What are Static & Non-Static functions in C Sharp?
Create a new project
Give the project name as OOPS
Right click Program.cs
Click Rename and rename it to Static_NonStatic
Hit enter, the class name should now be changed to Static_NonStatic
As you can see above, the main function is a static function because it has the static keyword associated with it. We can also make your own static functions.
The sum() function below is static since it has static keyword associated with it
We can also make non-static functions, example, sendEmail() function below, it does NOT have static keyword associated with it
So there are 2 types off functions: static and non-static
What are local variables?
We can also have local variables inside a static and non-static function. The life/scope of these local variables is only inside the function they are defined
What are global variables?
Global variables are present inside the class but outside the functions viz they are present parallel to the functions inside the class, generally declared at the top of the class, at the very beginning of the class.
Global variables can be used across all the functions with some conditions. If global variables satisfy those conditions than they can be used across all the functions inside the class.
Let us define a non-static Global variable (line 12)
Let us define a static Global variable ‘age’ (line 13)
In summary, we have:
2 types of functions: static and non-static
2 types of variables: local and global
2 types of global variables: static and non-staticThe local variables are neither static nor non-static. If you try to make a local variable static, you will see an error
Some rules
Static function can only access the static stuff (static global variables, static functions):
Since sum() is a static function, therefore it can be called inside the static main function (line 17)
But we will get an error if we call sendEmail() function inside static main function, since sendEmail() function is non-static (line 18)
Similarly the static variable age can be accessed inside static main function (line 19)
However we will get an error if we try to access non-static name variable ‘name’ inside static main function (line 20)
There is no rule associated with non-static stuff:
Look at lines 33-35, we are able to access static and non-static stuff inside non-static function sendEmail()
How can we access non-static stuff inside static Main function? To understand this, lets create a class name Car (right click project >Add >New Item)
Let us make the Car class public
Now what all a Car can do? It can start, it can accelerate etc. So let us create 2 non-static functions inside the Car class: start() and accelerate()
Let us also create a static Main function
Now Car will have some properties like: model, price, wheels. So let us define global variables of Car class
Now inside the Main function we can create the object of a class. To do that, we can use the keyword new followed by the classname
As soon as an object is created, a memory location gets associated with this object. Inside this object, all the non-static stuff will be residing
Let us create 3 Car objects, so 3 locations are assigned in the memory, each location having non-static stuff
We can also say Car c1 = new Car()
Note: ‘new Car()’ is the actual object, c1 is the reference pointing towards the new object Car (c1 is not the object)Now just type c1. You will see all the non static stuff associated with this object [example, model, price, start() etc]
So we can access the non-static stuff inside the static Main function only by creating an object of the class, not otherwise
Notice line 26 below. We are trying to access non-static start() function directly inside the static Main function, hence getting an error (we saw this while discussing rules)
Similarly we can create another object and assign the properties (non-static stuff)
Let us now try to print the models of both the cars (lines 25, 34)
In a project, we can have only 1 Main function. Right now we have 2 Main function: in Car.cs and in Static_NonStatic.cs
To execute the Main function of Car.cs class, we can either comment the Main function in Static_NonStatic.cs class or double click ‘Properties’ (seen under Project on RHS) and select OOPS.Car from the ‘Startup object’ dropdown
Save the changes and run the project, notice that models are printed in the o/p
Similarly we can print other properties as well.
Thank you for reading!