Learn C# with OOPS Concept(Static Constructor, ways to return values by a function/method)-Part-4
Static Constructor
First of all let's elaborate basics about static constructor that all programmers must have:-
static constructor does not have arguments or parameters.
static constructor does not have return type.
Due to above points static constructor can't be overloaded and inherited.
static constructor does not have any access specifier.
static constructor is called automatically or we can say invoked automatically, it is meant to be called by the Common Language Runtime(CLR).
static constructor is called or runs before the Main() function/method or we can say static constructor initialize the class before creating the object/instance of an class.
Important Points:-
Keep in mind that, CLR calls a static constructor not more than one in the single application before the instance/object. It is better not to add any type of blocks or body of code inside the static constructor that halt the working of application. The blocks or code may be :- Managing current thread and initialization, events, block of code for parallel operations.
Keep in mind that, initialization or re-initialization of static readonly variables are only done by static constructor.
Now the question arise what is the use of static constructor at application level?
Suppose we have an application/software and that application have some pre-requisites for running in any system that can't be checked at compile time.
How that pre-requisites are going to be checked before installing the software on system.? The answer is static constructor.
Static constructor check the pre-requisites, whether the system have enough RAM, Hard-Disk, suitable system etc. that are applicable for a software / application.
We have to write a block of codes in the static constructor that checks the pre-requisites before installing or before executing the software.
One more real time scenario is log files. static constructor helps the class to write the log entries into the log files.
Lets take an example:-
As you can see by an above example, the static constructor block of codes are executed first, before the Main() function block of codes.
Lets take a question :-
Is it possible to call static function and Non-static function by an static constructor which is in same class?
The answer is yes we can call static and Non-static function by an static constructor:-
If we are calling Non-static function, then create an object of a class within a static constructor and access the same.
We can call static function directly by an static constructor.
Lets take an example of how we can call Non-static function:-
As you can see in above example, a void CheckDrives() function/method is called by an MyApp() static constructor. In the MyApp() static constructor we have create an instance of a class and that instance is accessing the CheckDrives() function.
Output:-
Lets take an example of calling static function directly :-
As above example, we have called directly static CheckDrives() function/method by an Static MyApp() constructor.
Note:- If we have Non-static and static function in different class and want to call by an static constructor which is also in different class How it will possible?
If we have static function in different class then:-
- Use class Name instead of object of class within static constructor for accessing the static function.
If we have Non-static function in different class then:-
- Create an object of a class within static constructor for accessing the Non-static function.
Important Remarks for both the cases:-
Keep in mind that the access specifier of static and Non-static functions will either be internal or public.
If the class in which static and Non-static functions are declared is in a same solution(.sln) than use internal as access specifier.
If the class in which static and Non-static functions are declared is in different solution(.sln) than use public as access specifier.
Lets take an example of static and Non-static functions in single program:-
Output:-
Ways to return values by an function/method
Returning Single Row.
Returning Multiple Rows.
Returning Single Row through function/method:-
Suppose client want to retrieve Single row from database and to be shown on browser then how we can code it via function?
There are two ways we can perform the task:-
By using "out" keyword in function parameter if function does not have any return type(void).
By using class Name as a return type in function.
- By Using "out" keyword:-
If function does not have any return type (void) and want to return single row data then "out" keyword helps to perform the task as desired.
What we do is write "out" keyword before defining the datatype of a variables:-
public void EmpDetails(out int ID, out string EmpName, out int Age, out long PhoneNo)
- "out" keyword is a similar to "output" keyword in database when we want to return single row data through procedure.
Example:-
Note:- The above approach is not good at project level because when the variables in function parameter grows than memory of stack goes overflow and later there is chance of exceptions. Also code become complex and unreadable.
- By Using Class Name as return type in function:-
Using Class Name as return types is a good approach as compared to "out" keyword because the variables that we define at class level is become the members of class and it is known as class member variables.
It become easy for a programmer to code whenever the requirement of client or variables grows.
We need to define the class Name in separate folder and the folder name is :- Models.
The reason for creating a Models folder for a class in which only variables are declared/defined is that it become easier to understand and this class will have single responsibility to gather client requirement and declare within it.
Later on we will pass the class Name that we define in Models folder as a return type of a function/method.
Example:-
As you can see above example image we have created a Models folder in which we created a class "Requirement".
In this Requirement class we only declare Variables as per the client requirement or information gathered.
This class is only responsible for declaring the variables not the logical operations and validations, that's why it is it follows Single Responsibility Principle(SRP).
Now below is the image of logical operation we perform in program.cs file for returning single row by using class Name as return type in function
Returning Multiple rows by a function/method:-
- Suppose client wants to retrieve multiple rows from database and want to display on the browser then how we can do it?
There are multiple ways to retrieve multiple row from database :-
Through ArrayList.
Through Generic List<> Collection.
ArrayList:-
When we use ArrayList as return type for a function to return multiple rows we have to do multiple things:-
ArrayList are strongly type.
Initializing or declaring data are fixed because every data in Array holds a address which starts from 0(zero).
For output or fetching the data we need to do Typecasting (boxing and unboxing) which will be the time consuming task due to this it will decrease the performance of application (we will talk about boxing and unboxing in next part).
Generic List<>
It is best to use Generic List<> to perform operation because size is not fixed as compare to ArrayList.
We don't need to do about Typecasting (boxing and unboxing) of data while output or fetching the data.
As above explanation we will use Generic List<> as return type in a function/method for returning multiple rows.
Example:-
As you can see above, in line number 5 we have use Generic List<> and inside List we have define Class Name "Requirement". After that we have hold multiple rows data in requirement class object and return the same, after that in Controller class we create an object employee of Employee class and then calling the EmpDetails() function then holding the multiple data into temp variable which in var type. We have used foreach() loop for retrieving the data and console the same.
Output:-
That's all in this Blog, i tried enough to elaborate the concept of static constructor and how we can return values by an function/method.
Hope you understand well.
In next part i will discuss on Assembly, Shared Assembly, how can generate public key etc. in terms of DotNet Framework Architecture.
For Part-3 please click on this link:- Part-3 where we discuss about Types of Variables in Programming.
Thanks Everyone!!
Subscribe to my newsletter
Read articles from abhishek kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
abhishek kumar
abhishek kumar
Serves 5+ years in IT field as a Website Administrator, E-Office Administrator, Web Developer, Full stack developer in different phase of Software Development Life Cycle(SDLC) including development, Implementation and testing.