How do I build a C# application using Command Line Compiler?
Microsoft’s .NET SDK ships with a command line compiler called csc.exe. It can be executed from within the DOS prompt. In this article, I will show you how to build a simple C# program using this compiler with Notepad as the editor.
Open your editor and enter the code as given in listing 1
Listing 1
001: // HelloWorld.cs002: // -------------003: using System;004: class HelloWorld005: {006: public static void Main()007: {008: Console.WriteLine("Hello World");009: }010: }
The line numbers are given only for the sake of explanation and does not form part of the source code.
In the above listing, Line 3 defines the namespace System. Line 4 declares our class named HelloWorld. Line 6 defines the Main() method, which is considered as a entry point for all C# programs. Line 8 calls the WriteLine() method of the Console class and prints "Hello World" as output.
Save the file as HelloWorld.cs and compile the code using a C# compiler. I assume you are using the compiler which ship with .NET SDK. For this purpose, you have to give the following command at the DOS prompt as shown in the figure given below
csc HelloWorld.cs
Figure 1
If you have installed Visual Studio 2008, you can compile a C# program using Visual Studio Command Prompt. You can launch it from Start | All Programs | Microsoft Visual Studio 2008 | Visual Studio Tools
If you have installed Mono C# Compiler then you should compile the above program using the following command:
mcs HelloWorld.cs
If there are any errors and warnings, the compiler will display them during the above process. You have to go through all those messages and correct them preferably by going back to the source code. As explained earlier, C# is a case sensitive language and hence even if you miss a semicolon or a comma, the compiler will throw error messages. If there are no errors and warnings your screen would look like as shown in the figure given above.
To view the output of the above program, you have to supply the name of the assembly (HelloWorld.exe) at the DOS prompt.
For example, you have to give the following command for executing the above program.
HelloWorld
The output will be as shown in the figure given below
Figure 2
For mono compiler, the execution statement will be as shown below:
mono HelloWorld.exe
An Assembly is a file which is created by the compiler upon successful compilation of every C# application.
Commenting the Code
If you look at line numbers 1 and 2 in the above code, you would see two slash lines at the beginning. In programming terminology, these lines are called as comments. C# compiler won’t compile the statements inside these comments and they are given only for documentation and reference purposes. It is a best practice to give comments while coding as it will help you to study the code at a later stage or for others who look at your code.
There are three ways by which you can give comments in C#. The first two will be already familiar to you if you had worked with C++ and Java. They are single-line and multiline comments.
Single-line comments are given with the symbol //, while Multiline comments are applied with /*…..*/ symbols and can spread more than one line (See Listing 2)
Listing 2
// this is a single line comment/*This is aMultilineComment*/
The third type of comment which is given with “///” is a new one introduced by the .NET Framework. It is used to generate documentation for your C# program and they are denoted by using the relevant XML tags as shown in listing 3:
Listing 3
/// <summary>/// this is a new comment system/// <summary>
[tag]microsoft, c#, java [/tag]
Categories: C#, Channels, Latest Tags: briefly about c# and c# tools, c sharp compiler download, c# compilers, C# keywords classified, csharp, develop in c# in windows98, download C# compiler for windows xp, download different Types Of Compilers in C#, explain briefly about c# and c# tools, multiform application in C#.Net, visual studio, Visual Studio 2005, visual studio 2008, visual studio 2010
What are the different editors that are available for C# programming?
Once you have installed a C# compiler, the next process is to choose an editor to enter source codes. There are different types of editors such as Notepad and Visual C#. In addition to these two, many third party vendors have also developed editors for programming with C#. This FAQ briefly discusses each one of these editors.
Notepad is a commonly used editor among many programmers. It comes with Windows and it is very easy to use as well. But there are lots of disadvantages with this editor like it doesn’t supports syntax coloring and other features such as intellisense. Moreover, you cannot directly compile and execute a C# program from within the editor. Although it doesn’t support features such as line numbering, you can jump to a specific line of the code by using the CTRL+G shortcut.
Microsoft Visual Studio is a full featured development tool and it includes project templates for all .NET languages in an Integrated Development Environment (IDE). Other than C#, the IDE supports the development of major .NET languages such as Visual Basic , Visual J# and Visual C++. In other words, it provides a single development environment for all these languages. You need to master the various elements found on the IDE such as Toolbox, Properties window apart from learning the C# language.
Will I get Visual C# as a separate product?
Yes. Microsoft sells a standard version of the product named “Visual C# .NET Standard Edition” at a reduced price. It comes with four CD’s along with a printed manual. This product doesn’t supports advanced features such as Crystal Reports which is available with Visual Studio. This product is a big boon for developers with limited budget.
Visual C# .NET provides you with the required templates and wizards for building applications. It also supports features such as Intellisense and Dynamic Help. Dynamic Help is a new functionality which will provide you help according to the relevant situation while developing a project. It will be available only if you have installed MSDN Library collection. Moreover, it ships with powerful debugging capabilities and automatic syntax/clause completion features. For instance, if you enter the keyword “try”, remaining clauses like catch and finally will be automatically get listed on the code editor.
What do you mean by IntelliSense?
IntelliSense refers to popping up of methods and properties according to the context. For instance, if you enter Console followed by a dot, a menu automatically pops up, which gives you all the methods and properties of the Console class. You should already be aware of this useful feature, if you had used any of the languages in Visual Studio 6.0.
Where will I get MSDN Library?
MSDN Library ships with all editions of Visual Studio. You will be automatically prompted to install the same after the installation of Visual Studio. You will get periodic updates of the library if you have subscribed to MSDN. Alternatively, you can access the library online at any time. The biggest advantage of using Visual C# is that it significantly reduces the development time of projects by integrating all parts of a project under a single roof. Even though there are lots of benefits, Visual C# is not compulsorily required for programming with C#.
In addition to Visual C#, many third party vendors have also released editors for C#. They can be downloaded from the websites of the respective vendors. One of the most popular editors is SharpDevelop. It supports syntax coloring, compilation and execution from the IDE apart from other features. With the help of this editor, you can also build Visual Basic, User controls and Web Services applications. You can also create XML and XSL files. Moreover, the developers of SharpDevelop have provided the complete source code for this editor under an Open Source License Agreement. The product and its source code can be downloaded from www.isharpdevelop.com.
Another editor is Antechinus C# editor developed by c-point but it is not as much popular due to its limited capabilities. Some of the other popular editors are given below
Borland C# Builder
CodeWright
EditPlus
Even though there are many editors available on the market its up to you to decide which editor to use. I strongly recommend you to install one editor and learn the language completely.
Categories: C#, Channels, Latest Tags: briefly about c# and c# tools, c sharp compiler download, C#, c# compilers, C# keywords classified, develop in c# in windows98, download C# compiler for windows xp, download different Types Of Compilers in C#, explain briefly about c# and c# tools, intellisense, microsoft, msdn library, multiform application in C#.Net, visual studio, Visual Studio 2005, visual studio 2008, visual studio 2010
How do I begin programming with C#?
First of all, you need to install a C# compiler. You can either use a command line compiler available from Microsoft or third party compiler kit developed by Mono. Microsoft’s C# compiler is widely used by many developers as the Mono compiler kit is not yet fully developed. In addition to this you can also build C# applications with Visual C# 2008, which is available with Visual Studio 2008. It is a robust development tool used for developing all kinds of C# applications.
Is it necessary to install Visual Studio 2008 to build C# applications?
Not necessary. A command line compiler is sufficient to learn C#. Visual C# is only used to simplify the development process. Microsoft’s C# compiler is available in the form of a package called Software Development Kit (SDK). A latest version of this kit can be downloaded from http://www.microsoft.com/downloads. You will also find this kit along with many programming magazine CD’s or from popular C# books such as Sams Teach Yourself the C# language in 21 Days by Brad Jones.
After the kit has been downloaded, you have to install it. The installation process normally takes between 15-30 minutes depending upon the speed of your computer and is self explanatory. The setup will automatically register the required environment variables and hence you can straight away start programming with C# after the completion of its installation.
Is it necessary to install .NET SDK and Visual Studio 2008 separately?
If you have installed Visual Studio 2008 on your system, then there is no need to install .NET SDK separately as it will be automatically installed as a prerequisite during the installation of Visual Studio. To test whether .NET Framework has been successfully installed on your system, go to Visual Studio 2008 Command Prompt and enter csc. If the system returns a fatal error message with the code CS2008 then the installation is perfect. All C# error messages are documented with codes. These messages will become familiar to you as you gain more experience with C#.
In addition to Microsoft’s C# compiler you also have an option to install Mono C# compiler kit available from http://www.mono-project.com/Main_Page. Mono is a project started to create .NET compilers meant for platforms other than Windows, such as Linux. But the project is still under development and with each release of the kit more features and additional support for namespaces are added. The upcoming version of this kit promises to provide support for Windows Forms, ASP.NET and ADO.NET. Moreover, the new compiler will also provide database connectivity support for MySQL databases.
Can I install Mono C# compiler kit under Windows?
Yes. While Microsoft’s .NET SDK has been designed to work with Windows, Mono C# kit can be installed in both Windows and Linux.
Categories: C#, Channels, Latest Tags: briefly about c# and c# tools, c sharp compiler download, C#, c# compilers, C# keywords classified, develop in c# in windows98, download C# compiler for windows xp, download different Types Of Compilers in C#, explain briefly about c# and c# tools, microsoft, multiform application in C#.Net, visual studio, visual studio 2008
FAQ 4 – What is Common Language Runtime?
Common Language Runtime shortly called as CLR provides a universal execution engine for .NET applications and it is provided by the .NET Framework. Every target computer should require CLR for executing .NET applications. One of the most interesting points to note is that the CLR itself manages the life of objects. This is the effect of Automatic Garbage Collection system built into it. When the CLR finds an object that is not being used, it destroys them and thus freeing memory allocation.
The corresponding compilers (C#, Visual Basic .NET) generate meta code upon successful compilation. This meta code contains lot of information about code types, its members and references used. The CLR generates Simple Object Access Protocol (SOAP) when it makes remote procedure calls. SOAP is a protocol used to access .NET Web Services. In addition to the garbage collection, CLR also provides support for Managed Code, Effective Memory Management, Type Safety Verification, Conversion of IL to native code, Structured Exception Handling and Effective Interoperability between COM components and Unmanaged Code. You can build secured applications using the runtime since the .NET Framework provides support for advanced security features as well.
Categories: C#, Channels, Latest Tags: briefly about c# and c# tools, c sharp compiler download, C#, c# compilers, C# keywords classified, clr, develop in c# in windows98, download C# compiler for windows xp, download different Types Of Compilers in C#, explain briefly about c# and c# tools, microsoft, multiform application in C#.Net, soap, visual studio, Visual Studio 2005, visual studio 2008, visual studio 2010
FAQ 3 – What is C#?
C#, pronounced as C-Sharp, is one of the powerful programming languages provided by the .NET Framework. According to Microsoft “C# is a modern, object-oriented language that enables programmers to quickly build a wide range of applications for the new Microsoft .NET platform, which provides tools and services that fully exploit both computing and communications”. The main brains behind C# were Anders Hejlsberg and Scott Wiltamuth. However, many other personalities like Rob Howard, Scott Guthrie were also involved behind C# and the .NET Framework. The programming language has been standardized by European Computer Manufactures Association (ECMA).
Microsoft originally released beta versions of .NET Framework to get feedback about the product from developers and customers. Based on the massive response and suggestions received from them for the first beta, Microsoft released second beta in early 2001. Finally, they released final versions of C# under the name, .NET Framework 1.0 in 2002 and that of 1.1 in 2003. With each release, the product shipped with improved features for developing secured and scalable applications along with high quality documentation.
Categories: C#, Channels, Latest Tags: briefly about c# and c# tools, c sharp compiler download, C#, c# compilers, C# keywords classified, develop in c# in windows98, download C# compiler for windows xp, download different Types Of Compilers in C#, explain briefly about c# and c# tools, microsoft, multiform application in C#.Net, scada controls silverlight, visual studio, Visual Studio 2005, visual studio 2008, visual studio 2010
Fix: A project with an Output type of Class Library cannot be started directly
You will sometimes come across the following error in a message box if you execute an ASP.NET application using Visual Studio 2005/2008
A project with an Output type of Class Library cannot be started directly. In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project.
Resolution
In order to fix the above error, right click the Solution name in Visual Studio 2005/2008 and select Set as StartUp Project option from the popup menu.
Applicability
Visual Studio 2005
Visual Studio 2008
Categories: Latest, Programming Tags: ASP.NET, briefly about c# and c# tools, c sharp compiler download, c# compilers, C# keywords classified, develop in c# in windows98, download C# compiler for windows xp, download different Types Of Compilers in C#, explain briefly about c# and c# tools, multiform application in C#.Net, Visual Studio 2005, visual studio 2008