HOME

Wednesday, February 29, 2012

Execute c# program in notepad and cmd? with out visual studio

Introduction
In this article you will learn to write your first C# program and compile it using the command-line C# compiler. I strongly recommend you read this article first to correctly install the .NET v1.1 SDK on your computer. The goal of this article is to get you comfortable with writing a C# program and compiling it. The code you type will be explained in later articles. So dont worry if you cant understand any code in this article.

As tradition goes the first program though is the Hello World program, which does nothing more but print a simple "Hello World" message on the console screen. Youll need a text editor (like Notepad) and the .NET v1.1 SDK installed on your computer to work with this example.
Let's begin!
Step 1: Start notepad from Start -> Program Files -> Accessories -> Notepad so that you can write the Hello World program. The program you write in C# is also called as source code.
Step 2: Write the Hello World program, you can either type the program shown below into notepad or just copy-paste it from this article.
/*
 HelloWorld.cs - First C# Program
 Written by - Saurabh Nandu
 Compilation:
 csc HelloWorld.cs
*/

public class HelloWorld
{
  public static void Main()
  {
    //Print Hello World
    System.Console.WriteLine("Hello World !");
  }
}
Note 1: C# is a case-sensitive language hence in C# class HelloWorld, class helloWorld and class helloworld are treated differently, so be very careful while typing and type exactly as shown !!
Note 2: Indenting (leaving spaces before each line), is not mandatory, but its a good practice since it makes it easier to read indented code.
Step 3: Once you have finished typing your program you should Save the source code file. In fact after making any changes to your source code, you should always save the file. To save the file for the first time in notepad click on
File menu -> Save As. In the Save As dialog select the directory from the Save In dropdown where you want to save your files, I generally save my files to C:\csharp, and then in the File name textbox, enter the file name as HelloWorld.cs (although you can provide any name you want but it should have an extension .cs). and click Save. Once you have saved the source code, and if you make any further modifications, in notepad use the Ctrl+S keyboard short-cut to save your source code file.
 

Figure 1: Notepad Save As dialog
Note 3: C# source code files are stored with the extension .cs.
Note 4: In notepad's Save As dialog, File name textbox, always enter the file name in quotes (" ") so that the file is saved with the correct *.cs extension, else on some operating systems if you miss the quotes the file is saved with the extension *.txt (default for text files) due to which your files may not compile.
Step 4: Since you have finished writing the source code its time to compile it. Since we are using a command-line compiler that ships with the .NET SDK, start the command prompt fromStart -> Program Files -> Accessories -> Command Prompt. Or go to Start -> Run, type cmd and press enter.
Note 5: If you have installed Visual Studio.NET then start the command prompt from Start -> Program Files -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools -> Visual Studio .NET Command Prompt. We use this prompt since it has the correct path settings for the C# compiler. Refer to the Installing the .NET v1.1 SDK article for more information on setting paths.
Now from the command prompt navigate to the directory where you have stored the source code file by issuing the following DOS commands.

cd\ - To navigate to the root of the drive
cd csharp - To navigate to the csharp directory.

Once you are in the csharp directory where you saved the source code file earlier, its time to run the C# Compiler csc.exe. Issue the following command to compile our HelloWorld.csprogram:

csc HelloWorld.cs 

You should see the output similar to that shown in figure below indicating the file was successfully compiled.
 
Figure 2: Compilation of C# source code
Step 5: If the compilation of the source code was successful then a new executable (Exe) file by the name HelloWorld.exe will be created in the directory you compiled the source code.
To execute the program simply type the name of the executable file at the command prompt and you will see the output of the program (Hello World !) as shown below.

Figure 3: Executing Hello World application
Congratulations you have successfully compiled your first C# program! Its time for you to proceed further and learn the C# language.
Points to Remember
1) C# code can be written in any text editor like notepad.
2) C# Source Code files are saved with the extension .cs.
3) C# is a case-sensitive language so you have to be careful while typing.
4) C# runs on the .NET Platform, hence you need to install the .NET SDK in order to compile C# programs.
5) The C# compiler is contained within the file csc.exe, which generally resides in the C:\windows\Microsoft.NET\Framework\v1.0.4322 directory.

13 comments:

  1. I can't compile this because I don't know anything about Visual Studio.net

    ReplyDelete
  2. The blog gave me idea to execute C# program my sincere thanks for sharing this post
    Dot Net Training in Chennai

    ReplyDelete
  3. really you have posted an informative blog. really you have explained each and every concept very well. so keep on sharing such kind of an interesting blogs.
    dot net training in chennai

    ReplyDelete
  4. thank you so much for this nice information Article, Digital marketing is tha good skill in grouth tha career For website creation, promotion and development contact here. Android Training in Chennai | Big data Analytics Training in Chennai

    ReplyDelete
  5. C# Programming notepad and cmd code to go to desktop control panel, on the to select that programs and features page, and that select the product edition you want to be repair and final to select an option for uninstall/change button to click.If want to become learn from Java Training to will have Reach us Java Training in Chennai | Java Training Institute in Chennai

    ReplyDelete
  6. Nice Post! It is really interesting to read from the beginning & I would like to share your blog to my circles, keep your blog as updated.
    Regards,
    Android Training in Chennai|Android Course in Chennai|iOS Training in Chennai

    ReplyDelete
  7. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    amazon-web-services-training-in-bangalore

    ReplyDelete
  8. I and my friends were going through the nice, helpful tips from the blog then the sudden came up with an awful suspicion I never expressed respect to the website owner for those secrets.
    nebosh course in chennai

    ReplyDelete
  9. Thanks a lot for sharing such a good source with all, i appreciate your efforts taken for the same. I found this worth sharing and must share this with all.




    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery


    ReplyDelete
  10. It was a very wonderful post!!! Thank you for your great work sharing with us. I am very glad to read your blog and I want more updates about this topic...thanks lot!!

    android training in chennai

    android online training in chennai

    android training in bangalore

    android training in hyderabad

    android Training in coimbatore

    android training

    android online training

    ReplyDelete