hi!
Are you a .NET programmer?
If so, try to share your .NET programming skills.
Thank you
-Viswanath
Do you know...

Visual Studio.NET is richest among all the other developer IDEs

.NET Chunks

Showing posts with label Basic C# Programming.... Show all posts
Showing posts with label Basic C# Programming.... Show all posts
        Its quiet simple creating and executing a C# app using VS.NET. I reckon one of the major reasons behind this is its user friendly GUI and the other reasons may be like intellisense, pre-written lines of code, code snippets and so on......

        To start writing a C# app we need to do the following:
Open VS.NET-->File menu-->new project-->choosing a template (as if now console app); choosing a framework version we want to work with-->click ok. As soon as we click ok, the created file will be opened and within the file we will find the following:

1.The set of namespaces that are frequently used and that are imported using 'using' statement.
2.Current project's namespace which is created automatically.
3.Current Class under the current project's namespace and a Main() under the current class, which are also created automatically.

        So now we can directly write our code for C# app within the Main() of the current class as following:   Console.WriteLine("first C# app");

        Now we can run the current C# app by using Debug option which will build and then run the C# app. To debug we need to press F5 or ctrl+F5. This will output the result in a console window.


        I think its quiet evident how VS.NET makes the development of a .NET app so simple. Instead of writing 8 lines of code we just wrote a single line of code and it (single line of code) can be executed by pressing a single key (F5).

        Compilation and execution of a C# app written in a notepad can be done using a command prompt but not VS.NET.

        It is recommended not to use windows command prompt (cmd). Instead of that we can use ” Visual Studio command prompt” which is a free tool available along with .NET framework.

        To know the reason, type CSC (C# compiler) within your windows cmd prompt. You will get the assistance as “csc is not recognized……….”, since no path is set to C# compiler. So we need to explicitly set path to C# compiler (like how we set path to java compiler) to compile and run C# app in windows cmd prompt, but why to do so when Microsoft provides us with a tool (VS cmd prompt) which has an ability to recognize the C# compiler or any .NET compatible language compilers.

        So, directly go to VS cmd prompt which is present within VS tools from Start-->MS VS 2008/2005. Now go to your current working directory by using cd command and type csc Basic.cs shown as follows:

       
        As soon as you press ‘enter’, Basic .exe file (called as assembly) will be generated - which includes compiled code (IL code) plus some other information (metadata + resources if any) regarding your file. This is shown as follows: (note: Type dir within the command line to view .exe file).


        Now type your C# filename within the VS cmd prompt to run or execute your app - shown as follows: (this will display the program output)

        To program the C# code it is not mandatory to use Visual Studio.NET. We can program it using some free tools such as notepad or any text editor. But it mandatory to install the .NET framework of required version.


        The following program is just a snap of simple C# console app which can be written in a notepad:

class Basic
{
static void Main()
{
System.Console.WriteLine("first C# app"); //like printf in C or cout in C++
}
}

        As OOP says "whatever you write should be enclosed within a container called as class to provide code access security - [ENCAPSULATION]", all the code above is written in a class named "Basic".
        As every program needs an entry point to get executed , the Main() is defined - which is the entry point to our program. OOP says "you cannot access the members of a class untill and unless you create an instance (called as object to that class) to that class". To avoid this we declare Main() as static. As static members doesn't require an obiect of a class to access them, the Main() which we defined above will get executed.
        The statement which we wrote within the Main() is helpful in displaying some content on the console (output window) . In this statement System is a namespace, Console is class within the System namespace and WriteLine is a static method within the Console class. All these are predefined and are present in BCLs. As WriteLine is a static method it also doesn't require the object of the 'Basic' class to access it.

End .NET Chunks