C# Tutorials

                            Introduction

 
Welcome to this C# Tutorial. This tutorial is primarily for new users of this great technology, and we recommend you to go through all the chapters, to get the most out of it as possible. While each chapter can be used without reading the previous chapters, some of them may reference things done in earlier chapters.

Have a look at the Table of contents to the right, where all the chapters are listed. This tutorial is never done - we will keep adding new stuff to it, so check back regularly. We hope this tutorial helps you to get started with C#.

Everything here is free, and we hope you like our work. If you do, then all we ask is that you link to this tutorial on your website, personal blog or anything else where a link is possible. Also, if you see anything interesting in one of our ad blocks, please click it to help us maintain this tutorial. Thank you!


                      Visual C# Express



C# can be written with any text editor, like Windows Notepad, and then compiled with the C# Command line compiler, csc.exe, which comes with the .NET framework. However, most people prefer to use an IDE (Integrated Development Environment), and Microsoft offers several options for this. Their flagship is Visual Studio, which can be used to work on every possible aspect of the .NET framework. This product is very advanced, and comes in several editions. Visual Studio is not exactly cheap, and might even be too advanced for hobby programmers.

With .NET framework 2.0, Microsoft introduced the so-called Express versions, targeted at hobby programmers and people wanting to try .NET, and they continued this tradition with the later release of .NET 3.0 and 3.5. The Express versions only work for one language, like C# or VB.NET, and miss some of the really advanced features of Visual Studio. However, they are free and will work just fine for learning the languages, which is why we will use it for this tutorial.

For C# programming, you should download the Visual C# Express from http://www.microsoft.com/express/download/. Install it, and you're ready to write your first C# application!


                            Hello, world!



If you have ever learned a programming language, you know that they all start with the "Hello, world!" example, and who are we to break such a fine tradition? Start Visual C# Express (introduced in the last chapter), and select File -> New project… From the project dialog, select the Console application. This is the most basic application type on a Windows system, but don't worry, we won't stay here for long. Once you click Ok, Visual C# Express creates a new project for you, including a file called Program.cs. This is where all the fun is, and it should look something like this:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
Actually, all these lines doesn't really accomplish anything, or at least it may seem so. Try running the application by pushing F5 on your keyboard. This will make Visual C# Express compile and execute your code, but as you will see, it doesn't do much. You will likely just see a black window launch and close again. That is because our application doesn't do anything yet. In the next chapter we will go through these lines to see what they are all about, but for now, we really would like to see some results, so let's pretend that we know all about C# and add a couple of lines to get some output. Within the last set of { }, add these lines:


Console.WriteLine("Hello, world!");
Console.ReadLine();

The code of your first application should now look like this:
 
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, world!");
            Console.ReadLine();
        }
    }
} 
 
Once again, hit F5 to run it, and you will see the black window actually staying, and even displaying our greeting to the world. Okay, so we added two lines of code, but what to they do? One of the nice things about C# and the .NET framework is the fact that a lot of the code makes sense even to the untrained eye, which this example shows. The first line uses the Console class to output a line of text, and the second one reads a line of text from the console. Read? Why? Actually this is a bit of a trick, since without it, the application would just end and close the window with the output before anyone could see it. The ReadLine command tells the application to wait for input from the user, and as you will notice, the console window now allows you to enter text. Press Enter to close it. Congratulations, you have just created your first C# application! Read on in the next chapter for even more information about what's actually going on.

Hey there, I'm Splash!

Share This Post

Comments

    Blogger Comment

0 comments:

Post a Comment