Monday, 28 November 2011

3. C# Editors and IDEs


Before starting your first C# application, you should take a look at the C# editors available for creating applications. Visual Studio .NET (VS.NET) Integrated Development Environment (IDE) is currently the best tool for developing C# applications. Installing VS .NET also installs the C# command-line compiler that comes with the .NET Software Development Kit (SDK).

If you don’t have VS.NET, you can install the C# command-line compiler by installing the .NET SDK. After installing the .NET SDK, you can use any C# editor.

Visual Studio 2005 Express is a lighter version of Visual Studio that is free to download. You can also download Visual C# 2005 Express version for free. To download these Express versions, go to MSDN website, select Downloads tab, and then select Visual Studio related link.

Tip: There are many C# editors available- some are even free. Many of the editors that use the C# command-line compiler are provided with the .NET SDK. Visit the C# Corner’s tools section (http://www.c-sharpcorner.com/tools.asp) for a list of available C# editor


If you can’t get one of these editors, you can use a text editor, such as Notepad or Word pad. In the next sections, you’ll learn how to write a windows forms application in notepad, and then you’ll look at the VS .NET IDE.


4. “Hello, C# Word!”


Let’s write our first simple “Hello, World!” program. The program will write output on your console saying, “Hello, C# word!”

Before starting with the C# programming, however you must install the C# compiler. The C# command-line compiler, csc.exe, comes with Microsoft’s .NET SDK. The .NET SDK supports the Windows 98, Windows ME, Windows NT 4.0 and Windows 2000 and later platforms.

After installing the compiler, type the code for the “HELLO, C# World!” program in any C# editor, which is shown in Listing 1. Then save the file as first.cs.

Listing 1. “Hello, C# world!” code

using System;
class Hello
{
  static void Main()
  {
    Console.WriteLine("Hello, C# world!");
  }
}

You can compile C# code from the command line using this syntax:

csc C:\\temp\first.cs

 

Make sure the path of your .cs file is correct and that the csc executable is included in your path. Also make sure that path of C# Compiler (csc.exe) is correct. After compiling your code, the C# compiler creates an .exe file called first.exe under the current directory. Now you can execute the .exe from window explorer or from the command line. Figure 1 shows the output.




Did you see ”Hello, C# world!” on your console?

Yes? Congratulations!!! You’re now officially a C# programmer.

No? You may want to check the path of your file first.cs and the path of the compiler csc.exe.

You have now written your first few lines of C# code. But what does each line of your program means? I’ll describe the various components of your “Hello, C# world!” program.

The first line of your program is this:

using System;

The .NET framework class library is referenced in namespaces. The System namespace contains the Console class, which reads from or writes to the console.

The class keyword defines a new class that is followed by a class name, as seen in the second line of the “Hello, C# World!” code listing:

class Hello
{
      ...
}

The next line of code is the static void Main() function:

static void Main() {
Console.WriteLine ("Hello, C# World!");
}
}

In C#, every application must have a static Main() or int Main() entry point. The concept is similar to that of the Main() function of C++. This means, this is what a compiler will be looking for to start the application and whatever code is written in this method will be executed before any thing else.

The Console class is defined in the System namespace. You can access its class members by referencing them directly. Writeline(), A method of the Console class, writes a string and a line terminator to the console.


No comments:

Post a Comment