Share via


C#: Basic Concepts

** **

Introduction:

**      **As C++ language, C# is an Object Oriented programming language. Generally many people spells C# as C#.Net (C Sharp dot Net), But here Microsoft developed .Net environment mainly for distributed applications( Sharing Processing between client and server) and in C#.Net Net signs that C# is used to develop only Distributed Applications but using C# we can develop any kind of Software applications including windows applications.

C# is a new language especially developed from scratch to work with .NET environment. Using C# we can write a webpage, XML based applications like web services, Components for distributed applications as well as desktop applications.

Writing First C# Program:

**     **Writing a program in C# language is similar as we write in traditional C++ language, if you are aware of C++ language its easy you to writing and understanding the C# Code. Anyway I will explain every line in our basic First C# program where we will cover the things using that we can write and understand a simple to complex programs. Let’s look at our first C# program.

Program 1-1:

** **

01.Using System;
02. 
03.Namespace sai.CS
04. 
05.{
06. 
07.Class FirstCSProgram
08. 
09.{
10. 
11.Static void  main()
12. 
13.{
14. 
15.Console.WriteLine(“This Is Our First CSharp Program.”);
16. 
17.Console.ReadLine();
18. 
19.Return;
20. 
21.}
22. 
23.}
24. 
25.}
Compiling and Executing the Program:

The above Program can be write on any text editor like notepad, editplus, vi editor(in Linux) etc..,  or we can use Visual Studio’s .NET IDE(Integrated Development Environment) designed by Microsoft especially to write, compile and Execute the .NET Compatible Languages, C# is one of the  .NET Compatible Languages the other compatible languages of .NET are VB.NET, J# etc..,

There are two ways of compiling the above C# Program.

1)   If you write this Program in Visual Studio’s .NET IDE, then there is no additional work do perform for Compiling and Executing the application. Just Use functional Key F5 or go to <Debug> Menu and select <Start Debugging >. The IDE internally complies and executes the application without any user interaction.

2)    In this method we manually compile and execute the above application using command Prompt. Now open you command prompt (Start->Run->cmd or command).

You can compile the program by simply using the C# compiler Tool (csc.exe) as shown below

Csc  FirstCSProgram.cs

** **

As you press <Enter> Key this csc.exe tool will compile our application named FirstCSProgram.cs and creates an exe file with same file name such as FirstCSProgram.exe.

Now using type FirstCSProgram.exe in your command prompt to execute our sample first C# program.

Note: cs is the file extension for C# applications.

Important point: Before using the tools such as csc.exe in your command prompt you have to set some environmental variables, to set these environmental variables you have two choices first, you can run a batch file named vcvars32.bat which is located at <Microsoft visual studios folder>/common7/Tools Folder (here <Microsoft visual studios folder> location is where your visual studios installed). Second, you will find a <visual studios 2005 or 2008> command prompt in start Menu ->programs -> Microsoft visual studios 2005 or 2008 -> visual studios Tools-> visual studio 2005 or 2008 command prompt which automatically set up these environmental variable for you. So you can directly use the .NET tools here.

A Close Look at the Code:

Line 1:  The first line of our sample program is

1.Using System;

Here we are importing the namespaces using <using> keyword in the above statement system is the namespace and we are importing it in our program. I will explain what is this namespace and how to use it and its importance in our next session for now just remember that namespace is a group of similar type of items and every class should belong to a specific namespace.

Line 2: Our next line in our program defining a namespace to our class as shown below,

1.Namespace sai.CS

Here we can define a namespace to our class by simply writing a user defined namespace name preceding with <namespace> keyword.

Line 3: Opening flower braces ({) this indicates to the compiler that the block is opened or started, it is similar as we use in C++ programming Language. When the compiler occurs this opening braces it will crease a new space on the stack memory where it will declares the variables which is scope to that block only (in the next chapter I will explain about variables, declaring a variables and their scopes) and allocating some memory from that newly created space for that block. Like that the compiler maintains n number of variables on the memory.

Line 4: In line 4 of our program we are declaring the class and its name as shown below,

1.Class FirstCSProgram

In C# programming what ever the business logic you want to write should be in class block I will explain what is class and its uses in our coming chapters so don’t worry for now just remember that what ever you want to write should write under class block and every program should contain at least one class. A class contains an optionally variables and optionally methods or functions   here we can define a class by simply writing a user defined class name preceding with <class> keyword.

Line 5: Opening flower braces ({) as I explained above in line 3, but this braces opening indicates class block has opened to the compiler.

Line 6: As I said in the above line that a class contains an optionally variables and optionally methods or functions, here  now in line 6 we defined a function or method as shown below,

1.Static void  main()

One important point should note by you that a program can contain multiple classes under one namespace but in that classes at least one class should contain this main() method because the compiler starts its job from this main() function, if you not mention this function in your program then the compiler cannot compile you program because it doesn’t know from where should it start compiling?, And raises an error. So if you can define a main() method as we defined in our program.

Note: A deep discussion on this main() method and their uses will be in coming chapters

Line 7: Opening flower braces ({) as I explained above in line 3 & 5, but this braces opening indicates main() method block has opened to the compiler.

Line 8:  This is the first statement in our main() method and note that every statement should end with semi colon(;). Here in the following statement console is the class in System namespace as  explained above and writeLine() is a static method I will explain you later what difference between static method and normal method for now just remember that static methods will call directly by its class name as in this case, In writeline() method we are sending a some text “This Is Our First CSharp Program.”  When the compiler read this line it just prints the text “This Is Our First CSharp Program.” On the command prompt at runtime.

1.Console.WriteLine(“This Is Our First CSharp Program.”);

Note: A console class is developed to read and print or write a text on the console i.e., on command prompt, this class contains a functions by using these methods we can read and print a

Text on the console.

In console class we have two methods to print a text on the command prompt there are WriteLine() and Write() method. Both prints the text on the screen but writeLine() method prints the text followed by a new line character (\n) with this every WriteLine() method writes text on separate new line. For example,

1.Console.WriteLine(“This Is First Line.”);
2.Console.WriteLine(“This Is Second Line.”);

The output will be

[

](http://saimaterial.files.wordpress.com/2012/05/1.png)http://saimaterial.files.wordpress.com/2012/05/1.png?w=300

In the same example replace the WriteLine() Method with Write() method  as shown below

1.Console.Write (“This Is First Line.”);
2.Console.Write (“This Is Second Line.”);

The output will be

[

](http://saimaterial.files.wordpress.com/2012/05/1.png)

http://saimaterial.files.wordpress.com/2012/05/2.png?w=300

Line 9:  As we know by above discussion about the console class we said that we can read and print the text on the console, above we came to know how we can print a text on the console screen but how we will read a text written by user at runtime ?. The answer is as I said that console class has a functionality using that we can read and print a text, as we seen to print a text on the screen we used writeLine() and Write() method in console class to read we have another method ReadLine() the name itself resembles the functionality of this method i.e.., it reads the text which is written by user at runtime.

1.Console.ReadLine();

Let’s take one example program to know how ReadLine() method works and their important. Here I this example we accept two numbers from the user ar runtime and we print the sum of that two numbers. Here the Program.

01.using System;
02.namespace FirstCSprogram
03.{
04. 
05.class Program
06.{
07.static voidMain()
08.{
09.int a, b;
10. 
11.Console.Write("Enter First Number : ");
12.a=int.Parse(Console.ReadLine());
13.Console.Write("Enter First Number : ");
14.b = int.Parse(Console.ReadLine());
15.Console.WriteLine("The Sum Of {0} and {1} Is : {2}",a,b,(a +  b).ToString());
16.Console.ReadLine();
17.}
18.}
19.}

When you compile and run this program on console it will ask you to enter first number after entering the fist number and press enter again it prompts for second number after giving the send number it will calculates the sum of these two numbers and displays the result on the console. Here how the output screen will look like.

http://saimaterial.files.wordpress.com/2012/05/3.png?w=300

**

Note**: As we have ReadLine() to read a line of text similarly we have another method Read() which reads the next character on the input stream.

Line 10: The next statement is

1.Return;

This statement indicates to the compiler that the end of the scope of its belonging Block. It return’s the control to its calling method which is waiting for this control on the stack memory.

Line 11, 12, and 13: These three lines indicating end of their block. As I explained that opening of the block is represented by Opening flower braces ({) similarly the closing of the block is represented by closing flower braces (}).

Happy Coding :)