So, we are going to start with C#. C# is an Object Oriented Language developed by Microsoft Corporation for working with .NET platform. For working with C# you must have any version of Visual Studio installed on your Computer. For the latest case you will get Light Components like Microsoft C# Express Edition to Download from Microsoft which is absolutely free.
Now we can see a simple program in C#.
-------------------------------------------------
using System;
namespace smilu
{
class cHello
{
public static void Main(string[] args)
{
Console.Write("Hello World");
}
}
}
--------------------------------------------------------
I know that if you are a fresher to C#; you may be wondering seeing the Code. There is nothing in much in the above code....
Let's be now familiar with what is written in the above code.
In the above code go from the begining...
The first line is
using System;
The using statement is used to add a NAMESPACE into this program.
Namespaces are collection of Classes. These Namespaces are used to avoid Class Name Conflicts. ie, there may be situations where we must have two or more CLASSes with the same Name. So, inside a program, it will not allow to create two classes with the same name. So there exists a problem. So to avoid these types of conflicts we use Namespaces. So we create a namespace and add the classes inside the namespace and we create the other class with the same name in another namespace.
In the above program we have added System namespace into this program. The System namespace contains CLASSes for implementing base datatypes.
Now the second step
namespace smilu
This step is used to create a User Defined namespace which contain classes. Here we have created a Namespace in the name of "smilu".
Then you must have noticed curly brackets throughout this program. These are the Bodies of Namespace, Classes and Functions.
The Third step
class cHello
As I have said in the earlier topics that C# is an Object Oriented Language. So an Object Oriented Language(OOL) must have a class. Without class we cannot say a program is purely Object Oriented(refer). So, in this step we are creating a CLASS with name "cHello".
We will be writing rest of the Codes inside the CLASS
The Fourth Step
public static void Main(string[] args)
or
public static void Main()
This is the most important Function of a Program. Without the Main() function a program will not Execute. We write the Main() function in C# like this. All the things written there are not necessary. We will check each keyword in the Declaration.
public:
public written in the function refers it Access to others.It is known as "Access Specifier" ie we have Given a public access to the Main function,so anywhere within the program we can access this function.
Static:
C# is an OOL. So member variables and member functions can only be accessed through objects. Here in the case of Main it is the heart of a program. When we execute a program the first thing which gets executed is the Main() function. This is the first class so Object for this class can only be created inside the Main() function. So we must have to get the program started by executing the Main() function. That is why we declare a Static keyword to Main() function. So it is not necessary to create object for the class.
void:
void means NOTHING.... void represents the return type of the Main() function. ie, what the main should return to the program. Here in this case we doesn't need anything to be returned.. so, we have given the return type as void.
Main(string[] args) :
string[] args is used to store Command Line Arguments(if any). Command Line Arguments are arguments which we pass to a program while its execution with its Name.
The Fifth Step
Console.Write("Hello World");
Here we are creating Console application ie, we create a DOS based program. System.Console is a class which contains definitions for Write(), Read() etc... We mainly use
Write(); For writing in the same line(output)
WriteLine() This is used to Write in a string and set a break to the Line after that.
Read() Read a single line
ReadLine() Reading the strings.
Here in the above code inside the write function I have written "Hello World". The text inside the double quotes("") represenets the text to be printed as the output. What all we are write inside the "" of the Write() or WriteLine() functions will be printed as that itself exept some Escape charactors like...
Commonly used escape charactors are...
\n - For new Line
\t - For a Tab Space
\b - For a BackSpace etc...
NOTE:- C# is a case sensitive Language. So it is necessary that you write codes in appropriate cases itself as written above.
After you type the program save the file with a Filename with extension ".cs"(eg:- Hello.cs).
For executing the program take the Microsoft Visual Studio 2005 Command Prompt from
START->PROGRAMS->MICROSOFT VISUAL STUDIO 2005->MICROSOFT VISUAL STUDIO TOOLS->MICROSOFT VISUAL STUDIO 2005 COMMAND PROMPT.
Now navigate to the Folder you have saved the File.
Compile the Code.
For Comiling you have to do like this .. if the file name is Hello.cs
In the COmmand prompt write " csc Hello.cs " and Press enter
If the compilation returns Successfully write the File name and press Enter
like
" Hello " and Enter.
You will get an output as
Hello World
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment