Starting C# with C# 2008 Express Edition
Brief history and overview:
Around 1997 Microsoft starts the work on .NET. By that time the .NET framework is called "codename Project 42" and "project Lightning". Microsoft faced some serious issues these days in the 90's. They were criticized about Visual Basic, when it lost ground against Java and C++ and at the same time Sun Microsystems plus the US government open cases against Microsoft in charges of monopoly and copyrights.
The answer of Microsoft comes later with the .NET platform. Soon, after the beginning of project 42, Microsoft hires Anders Hejlsberg. Hejlsberg is a luminary in the software world then and nowadays as well. By the time of his appointment to Microsoft, he was already famous with his programming language Delphi and his work in Borland. Microsoft offered him one million dollars salary and a package of over 70,000 shares. Anders then gathers a team of pro. coders in 1999 and they start the work on a new computer language, which is then called "Cool". Soon, the "Cool" language acquires the name: C#. It is announced in July 2000 on a conference where Bill Gates speaks as well. Later in 2001 C# achieves its real fame when it's included in the first release of Microsoft Visual studio .NET. Within just a few years, if not months C# becomes extremely popular and successful. Today it is used in very sophisticated applications, such as the "Voice recognition software" in Windows Vista, video editing software of Sony, and even prototypes of Artificial Intelligence. The name "C#"(C sharp) came from the musical term "#". In musical language the sign "#" means: A semi-tone over the note. The intention behind that original name was to be shown that C# is a little bit better than C++, just like C++ used the "++" sign to express its power over C. However, apart of its metaphysical "power" C# really differs from C++ and other languages. And it's without a doubt one of the best languages nowadays. Here is in brief what makes C# so better and so special:
- C# has four paradigms, it is: functional, imperative, generic, object-oriented (class-based). For comparison Java has only 3 paradigms and PHP and C++ only 2 paradigms.
- C# is perhaps better than C++ in many ways: The Visual Studio contains the so called "garbage collector", which gathers the released memory so the software developer doesn't have to bother about the excess of memory like in most other languages. Also, in C# the pointers are removed, there are no functions or global variables, only classes, which simplifies the process or programming a lot. Some other keywords and types are included: like the "bool" type and the "foreach" statement.
- C# could be easily included in any webpage containing ASP.NET code, thus making the WebPages as powerful as Windows software: With the use of the .NET platform the software developer could simply connect any web-code of ASP.NET with C# just like for example PHP is connected with HTML. Hence, the programmer could for example create very sophisticated online website applications like a voice recognition software, just connecting ASP.NET to the windows C# code.
2. Setup your computer to work with C#:
Actually, working on C# environment is very easy. All you need is a single program called:
Microsoft Visual studio. Not only that but this application has FREE edition. You can of course pay for the commercial edition. But for the purpose of this tutorial we will use the free version.
2.1: Download the free version of Microsoft Visual studio here:
Downloads - Choose below the "Visual C# 2008 Express edition" usually presented as a green box... Then either save the download installer or chose to run it directly...
2.2. The installation of the compiler/IDE: Here is the main screen of the C# express edition installer. Choose "
Next >":
Accept the "Terms and Conditions" and click "Next >":
Choose whether you would like or not to read the HELP offline or online and choose "
Next >":
In my case I chose to skip the HELP installation and...here we go, ready to install. Again, click "
Next >":
The C# express edition is now being installed:
The end of the installation, being successful, as you can see the product is totally free. But you need to register and activate it. Now click "Exit" .
This box appears telling you to restart. Chose for example "
restart now". After you restart you will be able to start the software.
3. First steps with C#:
After you have restarted your computer, go here: Start/All Programs/Microsoft Visual C# Express Edition. Click on the quick launch shortcut and...you should see the main look of the program.
3.1 Starting your first C# program:
Now, after you have started the Visual studio, open new project. On the main menu click File/New project. Like here:
Now on the window that appears, choose on the left side: "
Windows forms application", then of course choose "OK" :
Now, you must have a screen similar to this:
Take a look on the screen above on the right side where is marked in blue color the word
"program.cs". Click on this word. Now, you must see a screen filled with some code, about 20 lines of code...this is a C# code. But...we won't need it at the moment. Simply, select those code and delete it(select it to become marked in blue and delete it). Now, write the following code on the place of the deleted code:
Code:
using System;
using System.Windows.Forms;
class MainClass
{
public static void Main()
{
MessageBox.Show("Hi there!");
}
}
Here is the look of the code when you write it in the Visual studio...
And...once again the same code, seen on another view:
Now, let’s execute our first program. You can test your result typing "
Ctrl + F5" on the keyboard. Or for example on the menu go here: Debug/Start without debugging. Like it's shown below:
And here it is - your first C# program:
Now obviously you can click just "OK". But voila! You created a completed C# which actually though simple, shows many aspects of the C# program.
4. Brief overview on your first program: The "Hi there" program:
Actually, first off, I must explain why I chose a graphical program. If you have some experience as software developer, I am sure you have seen only tutorials and books that start with the famous "Hello world" program in console only/DOS variant. However, as you know nowadays the console applications are almost forgotten and showing the power of C# through a DOS interface could simply kill the interest of any newbie programmer. Thus, I chose to start with a simple Windows dialog showing the greeting "Hi there"

.
Now, lets explain every line of this program. As you remember your program started with the following line:
The very first word "
using" speaks for itself. It is a directive at the compiler to USE something. In the case it USES "System".
Now...what is "
System"?:
"
System" is in brief a large library containing pieces of code, which you don't have to type anytime when you start your coding. The "System" is called "namespace". In C#, a "namespace" means generally two things: A library of code, like in that case. And as well "namespace" is a word used to avoid name conflicts. For example if you have two classes called with one and the same name "Johnny", before each class you can type different namespace. Like before the first john class type "namespace1" and before the second john class type "namespace2". And, if the word "class" sounds like a rocket science to you at the moment...don't worry. We will explain later here what the heck a class is. Now let’s continue with your program. After "using System", we have another namespace: "System.Windows.Forms".
Code:
using System.Windows.Forms;
The System.Windows.Forms is a namespace which allows us to use the graphical objects in some applications. In the following case it contains the GUI + the OK button and even the closing X on the right corner...
Here we continue with the "class". So, what is a class? A class is in brief a block of code. It is similar to the "function" for example in PHP. I compare it to the "function" word because in languages like C++ of PHP the word "function" speaks for itself, showing the exact purpose. But some programmers might be confused when they see the word "class" in C# or Java. However, like we explained this is just the beginning of a block of code. Though the class is indeed more complex. It not only initiates the beginning of a program, but as well defines its "appearance". For example the class "cars" might create another child class called "sport car". Also, a class inherits another class, a class can be derived from other class and so on. It is the form of your program. But for now simply think of it, like a beginning of your program and a "holder" of programming function. Note that you can call your class as you wish in most cases. For example class thisIsCool, or class "Coconut". You just use the word "class" and then some name...
After the class definition you must put a curly brace
{. Every class starts and ends with a curly brace:
{...}.
Here comes the next sentence:
Code:
public static void Main()
This is the so called
Main method, which is the heart of every C program. Not only C# but C++ and so on... The main method is in our case defined as follows:
public - meaning that we can access the main method from everywhere in the current program.
static- meaning that it doesn't manipulate any data.
void- meaning that the Main method is not expected to return a value.
Now, after the Main method we have yet another scope of curly braces.
{}
WIthin these braces however is the main object of our program:
Code:
{
MessageBox.Show("Hi there");
}
As you can see here you can modify the words within the
(""). You can write for example "Hello John" or "Hi Kate" or "C# is great!".
5. Showing the real power of C#:
Well, obviously we cannot state in a single tutorial the power of such a complex language like C#. But more generally, what is of interest to be mentioned is that the programming language are actually very easy if you understand their main concepts. They are not like the human languages (English, German etc.)when you need new and new words to express some object or subject. In the programming languages and c# particularly are just set of classes...if you learn how to connect these classes and to use a predefined libraries then you are an excellent programmer. Hence, below I will show a little more complex C# program, which is actually a webaddress resolver. In other words a program that shows you the IP address of any website. Here, is the code of the program. I use comments, to describe every part of it. The comments are in green...
You can copy and paste this code on the place of the previous "hi there" application and execute it using the same way(Debug/Start without debugging"):
Code:
/* First we define the namespaces, the so called "Namespaces”, are in simple words,
libraries of required data.
For example almost every C# program starts with the namespace "System", in the
following case we will use these namespaces: System, System.Drawing, System.Net, System.Text,
System.Windows.Forms. Each one of them speaks for its purpose clearly.
*/
using System;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
/* Now we start the program with the "class" which we name "AsyncResolve". Note that after the
* class we have the ":" identificator and then the word "Form". This means: "the object AsyncResolve
* will create a Windows dialog/form/ */
public class AsyncResolve : Form
{
/* Here we define the boxes which are in the case the very main objects in the application
thus we better define them early */
TextBox address;
ListBox results;
/* Here we use the "AsyncCallback" delegate, whose function is to start the Asynchronous
* web services in our program. Actually this object is very important for the program, but
* doesn't appear often, because we just define it and later use it in our main class, as shown
* below */
private AsyncCallback OnResolved;
/* Below is The "main" class of program. Take a look at the word "AsyncResolve". This once again
* should remind you that we use a predefined words (called "DELEGATES") in our program which
* actually initiate the C# functionality by default. In simple words, when we use such predefined delegates
* we simply instruct the C# environment to accomplish the entire job. In the case our
* desired job is to resolve a web address such as "ebay.com" onto its actual IP address (
* say...64.127.34.98.*/
public AsyncResolve()
{
/* Here we define the Text box and later on the string "
OnResolved = new AsyncCallback(Resolved);" we invoke the object/delegate defined earlier: "AsyncCallback".
* In simple words we earlier invoked the needed object/function that performs
* the hard task: Getting the IP. Now we just include this object(AsyncCallback in our
* textbox to connect it with the text box */
Text = "Website to IP";
Size = new Size(400, 380);
OnResolved = new AsyncCallback(Resolved);
/* Here we define and connect the graphical parts of our program. Such as the listbox,
the button, the text. It is all accomplished here in several lines */
Label label1 = new Label();
label1.Parent = this;
label1.Text = "Enter website to find its IP:";
label1.AutoSize = true;
label1.Location = new Point(10, 10);
address = new TextBox();
address.Parent = this;
address.Size = new Size(200, 2 * Font.Height);
address.Location = new Point(10, 35);
results = new ListBox();
results.Parent = this;
results.Location = new Point(10, 65);
results.Size = new Size(350, 20 * Font.Height);
Button checkit = new Button();
checkit.Parent = this;
checkit.Text = "Find IP";
checkit.Location = new Point(235, 32);
checkit.Size = new Size(7 * Font.Height, 2 * Font.Height);
checkit.Click += new EventHandler(ButtonResolveOnClick);
}
/* Here we simply tell the program what must happen when the button is clicked.
* Obviously in the following case, what happens is that when the button is clicked
* we invoke a method called "Dns.BeginResolve" which actually finished the real task started
* by the AsyncCallback delegate, which is to
* find the IP address of the website */
void ButtonResolveOnClick(object obj, EventArgs ea)
{
results.Items.Clear();
string addr = address.Text;
Object state = new Object();
Dns.BeginResolve(addr, OnResolved, state);
}
/* Now we must show the found data. We use the keyword "string" to group the data in lines.
along with the objects of the DNS resolving */
private void Resolved(IAsyncResult ar)
{
string buffer;
IPHostEntry iphe = Dns.EndResolve(ar);
buffer = "Host name: " + iphe.HostName;
results.Items.Add(buffer);
foreach (string alias in iphe.Aliases)
{
buffer = "Alias: " + alias;
results.Items.Add(buffer);
}
foreach (IPAddress addrs in iphe.AddressList)
{
buffer = "IP: " + addrs.ToString();
results.Items.Add(buffer);
}
}
/* Here in the last class we simply use the required Main() method, which is required
* in every C# application. Hence, we use the Main() method and instruct it to start the
* application along with the object/delegate "AsyncResolve" */
public static void Main()
{
Application.Run(new AsyncResolve());
}
}
That is all. If you have any questions or comments please post them here!