Lets Learn C# With OOPS from basics
First we will start with some simple basic code and then we will elaborate each line of code one by one.
Basic C# code:-
- Write a Code in Notepad :-
using System;
class Employee
{
public int id;
public static void Main()
{
Employee obj=new Employee();
obj.id=101;
Console.WriteLine(obj.id);
}
}
- Now lets elaborate the "using System;"
System is a inbuild Library in c# which consists so many classes, functions/methods etc, which are used by programmer to code better.
As you can see in above program we have a class "Console" which comes from System Library and we "WriteLine" as function/method which also comes from System Library.
Console.WriteLine is used to print/output a message on a developer command prompt/vs code command prompt that are given within its opening or closing brackets.
- Lets elaborate "class Employee"
In c# every program starts with class Name. The class name must be logical and understandable so that coding/logics will be performed based on the name of class. class name is also a user defined data type.
Here class name is "Employee" and we are going to code based on the employee behavior in logical manner. The name of class should start with capital letter.
Internally class access modifier is either internal or public, if we give class access modifier as protected and private we will get compile time error.
This class is a base class for any other class which are going to inherit and the class that inherit the base class is known as sub-class/derived class.
The concept of base class and sub class is came into picture at this point of time because every class that we define in c# is always a sub class of "Object class".
The Object class(System.Object) is the ultimate base class of all the .Net classes.
It provides low level services to its derived classes. There are 8 types of services that are provided by Object class which will be discussed later.
Note:- Know suppose someone ask how you know that Object class is base/ultimate class of all the .net classes? How you will proof?
Lets do practical on it for explanation.
- Lets save the above code in a particular folder
Write a simple c# program in notepad as above and save by "ApplicationName.cs" (within double codes) and select all files as image below:-
-
open the developer command prompt and then compile the program.
"csc" is a compiler in to compiler the c# program in command prompt.
Go to a folder where your program is save and compile the program as:- csc emp.cs .
As you can see in below image after compilation our emp.cs is converted into .exe file which is nothing just a native language(machine language)which are understandable by our system.
Now we have Intermediate Language Dassembler (ildasm), which is a tool and is used to break low level language and we can see the low level language by this tool.
Write ildasm in command prompt within the folder where c# program is written and enter then open your program in ildasm as below:-
- In ildasm--->open the c# program and click on class name then click on first (.class) as image below:-
- when you click on that .class you can see the System.Object class which are inherit by our class Employee class, see image for proof:-
- By giving above explanation now we can proof that the class we define in.net c# is a derived class/sub class of Object class.
- know lets elaborate the "public static void Main()". What is it?
public is access modifier which means that the compiler can execute the Main() function from anywhere.
static is a keyword, when we write the static keyword before any function/method it means that we don't need to create an instance/object of class for calling. Here Main() function can be called without instance/object of a class.
Main() is a function/methods which a starting/entry point of every program that are written in c#. Therefore Main() needs to be static in order to allow to be entry point of any c# program.
Main() is a also a first "Thread" of every program.
Note:-Suppose someone ask you how you can execute the program without a Main() function? what will be your answer?
- Lets do practical on the above code for compiling the program without main function.
Remove the Main() function from the code and save it then compile it by given below command:-
csc /t:library ApplicationName.cs (t-->target).
what it will do that it will convert the code into .dll form as image below:-
-
As you can see in below image the emp1.cs file is converted into .dll file without Main() function if try to run the program with csc emp1.cs it will give error because we have not define the entry point(Main() function).
-
Now the .dll file is created for emp1.cs, lets make another program with another class where we will define the Main() function and in that class we will create an object/instance of emp1.cs class.
Kindly note that in emp1.cs program the class must be public so that it can accessible by other class. Also its variables must be public because variables that we define is private by default.
Know lets create another program with Main() function and we will define the object of class that are in emp1.cs program as image below.
-
Now, we will take the reference to "emp1.cs" program of .dll file in Hr.cs file(in which we define the Main() function/method).
The command for taking reference to .dll is:-
csc /r:ApplicationName.dll ApplicationName.cs (r is for reference).
After running a above command we have gave reference of emp1.dll file to Hr.cs and also we have execute the Hr.cs file with reference as below image:-
-
Know for proof lets execute/compile the Hr.exe for output as image below:-
-
Now by above steps and output as shown in image, we can say that we run/compile the application without a Main() function.
Note:- Now we will discuss later parts of codes in part-2
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.