Thursday, July 14, 2016

Run CSharp “Hello World!” program on Developer Command prompt

To start writing your first program without using the Visual Studio or other tools, you just need to open “Command Prompt”. Just type command on the search in Start menu either you are using Windows 7 or any higher.
image_thumb[10]_thumb[3][4]
Visual Studio installs few command line tools to do some operation using command tools and these can be invoked using the “Developer Command Prompt for VS2015”(For Visual Studio 2015). If someone has not installed Visual Studio then just install .NET Framework on the computer.
After this we are ready to create and compile our “Hello World!” program using the .NET Framework compilers.
Now create a file “hello.cs” using the “notepad”. We are going to write our code to print “Hello World!” on the screen for this example. “.cs” is the common extension for the C# file and “.vb” for Visual Basic. So now we going to create a C# program now so this file will contain C# code.
image_thumb[16]_thumb[1][4]
After running this command a prompt will appear with message “Do you want to create a new file?”. Click on “Yes” to create a new file.
In this we are going to create a class with a static method “Main”. In a Console application that would run on command prompt, CLR look for class named “Program” with a “Main” method to run the program.
image_thumb[8][3]
Now save this file and come back to the command prompt.
Now one of the tool “csc.exe” exists in the “Windows” directory in the .NET framework installation paths. It can be located at the path “C:\Windows\Microsoft.NET\Framework\{.NET Framework}”. Under the Framework folder you will find installation of .NET Framework multiple version. Below is the path of the latest one on my system. It is the “Visual C# Command Line Compiler” which compiles the C# code.
image_thumb[24][4]
Now we are going to compile the C# code then execute the program. After compiling the program C# compiler produce a executable file(.exe).
To compile the created C# code file, use the below command line on command prompt. Just pass the created “hello.cs” as parameter.
  1. csc hello.cs 
image_thumb[29][4]
On windows we can run exe. Now run the hello.exe to view the result of our example program.
image_thumb[32][4]
That’s all done. We have created an example program run on the command prompt using the C# command line compiler.
Actually it transforms C# code into Microsoft Intermediate Language. This can either an exe or dynamic link library. We specify the assembly format to the csc.exe using the /targe:library option. See below image for various target options:
image_thumb[37][4]
  Happy Coding!



















No comments :

Post a Comment