Introduction
This part of the tutorial explains C# and how to write and compile your first C# program from the command line.
What is C#?
C# is a new programming language developed by Microsoft. C# has the power of C++ since it's derived from C and C++. It is simpler than VB. Besides that, C# is a Java-like language for web programming, and it has some good features of Delphi too. Microsoft says that C# is the best language to develop its .NET Framework applications.
Installing .NET SDK
Installing .NET SDK is the first step to running C# on your machine. You can install .NET SDK on Windows ME, Windows NT, or Windows 2000. But Windows 2000 is recommended. After selecting your OS, you need to follow these steps.
- Install IE 5.5
- Install Microsoft .NET Framework SDK. It's free, and you can download it here. NET Framework SDK.
- After installing these, you can write your code in any text editor and save it as a .cs extension. Type this in a notepad and save it as my. cs.
C# Compiler and Editors
.NET SDK Beta 1 release of Microsoft's new platform, .NET, incorporated with C# command line compiler. You must have to install .NET SDK to run a C# program. Once you install .NET SDK, you can write your C# program in any text editor including Notepad, Wordpad, or Visual Studio. There are some third-party editors available in the market too. Some of them are free. Check out the tools section of C# Corner.
How to write your first C# program?
Writing your first C# program is similar to writing C++ applications. You open any text editor I described in the above paragraph and type this code.
using System;
class MyClass
{
static void Main()
{
Console.WriteLine("Hello World!");
}
}
How to Compile your first C# program?
Now use the command line to compile your CS file. C# compiler takes at least one argument i.e., file name. Say your C# file name is myclass.cs, then here is the command line syntax.
csc myclass.cs
C# compiler creates an exe file in the bin dir of your project. Just run this exe and see the output.
Now, let's take a look at your code line by line. The first line of your program is using System.
Why use a System?
The system is a namespace that stores system classes. The Console class I used in the program to display the output on the console is defined in the System namespace. That's why this like is there.
The next line is class MyClass. The class keyword in C# is used to create a new class.
class MyClass
{
// Add class members here when needed
}
Each class has one static void Main() function. This function is the entry point of a C# program.
static void Main()
{
Console.WriteLine("Hello, C# World!");
}
The WriteLine method of the Console class writes text to the console.