Thursday, September 1, 2011

Getting Started With Csharp




To compile your first C# application, you will need a copy of a .NET
Framework SDK installed on your PC.T

There are two .NET frameworks available: Microsoft's and Mono's.

Microsoft

For Windows, the .Net Framework SDK can be downloaded from Microsoft's
.NET Framework Developer Center. If the default Windows directory (the
directory where Windows or WinNT is installed) is C:\WINDOWS, the .Net
Framework SDK installation places the Visual C# .NET Compiler (csc) in
the C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705 directory for
version 1.0, the C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322
directory for version 1.1, or the
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 directory for version
2.0.

Mono

For Windows, Linux, or other Operating Systems, an installer can be
downloaded from the Mono website.

For Linux, a good compiler is cscc which can be downloaded for free from
the DotGNU Portable.Net project page. The compiled programs can then be
run with ilrun.

If you are working on Windows it is a good idea to add the path to the folders
that contain cs.exe or mcs.exe to the Path environment variable so that you do
not need to type the full path each time you want to compile.

For writing C#.NET code, there are plenty of editors that are available. It's
entirely possible to write C#.NET programs with a simple text editor, but it
should be noted that this requires you to compile the code yourself. Microsoft
offers a wide range of code editing programs under the Visual Studio line that
offer syntax highlighting as well as compiling and debugging capabilities.
Currently C#.NET can be compiled in Visual Studio 2002 and 2003 (only
supports the .NET Framework version 1.0 and 1.1) and Visual Studio 2005
(supports the .NET Framework 2.0 and earlier versions with some tweaking).
Microsoft offers five Visual Studio editions, four of which cost money. The Visual
Studio C# Express Edition can be downloaded and used for free from Microsoft's
website.

The code below will demonstrate a C# program written in a simple text
editor. Start by saving the following code to a text file called hello.cs:

using System;

namespace MyConsoleApplication

{

 class MyFirstClass


 {

 static void Main(string[] args)

 {

 System.Console.WriteLine("Hello,");

 Console.WriteLine("World!");

 Console.ReadLine();

 }

 }

}

To compile hello.cs, run the following from the command line:

•For standard Microsoft installations of .Net 2.0, run
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe hello.cs




•For Mono run mcs hello.cs.


•For users of cscc, compile with "cscc -o <name>.exe <name>.cs".


Doing so will produce hello.exe. The following command will run
hello.exe:

•On Windows, use hello.exe.




•On Linux, use mono hello.exe or "ilrun <name>.exe".


Alternatively, in Visual C# express, you could just hit F5 or the green play
button to run the code, even though that is for debugging.

Running hello.exe will produce the following output:

Hello,

World!

The program will then wait for you to strike 'enter' before returning to the
command prompt.

Note that the example above includes the System namespace via the using
keyword. That inclusion allows direct references to any member of the System
namespace without specifying its fully qualified name.

The first call to the WriteLine method of the Console class uses a fully
qualified reference.

System.Console.WriteLine("Hello,");

The second call to that method shortens the reference to the Console class by
taking advantage of the fact that the System namespace is included (with using
System).

Console.WriteLine("World!");

C# is a fully object-oriented language. The following sections explain the
syntax of the C# language as a beginner's course for programming in the
language. Note that much of the power of the language comes from the classes
provided with the .Net framework, which are not part of the C# language syntax


per se.

live version · discussion · edit chapter · comment · report an error


0 comments: