Export (0) Print
Expand All

C# for Sharp Kids - Part 1 - Getting Started

February 2007

Are We Allowed To Have Fun Here?

I suspect I know what you’re thinking … “I want to write a program and I want to write it now! Coding is the cool part; I don’t want to read half the book before I can write my first program. In fact, this sentence is getting too long already - I want to get going!"

Bb330922.c065f194-bfb7-4e04-8794-59b48edf55e2(en-US,VS.80).png

Truthfully, I’ve yet to meet a programmer who can resist diving in – after all, if you bought a new bike, would you sit down and read the book first? Quite likely not – you’d leap aboard, take it for a spin, fall on your face and only later discover the fact that the brakes on this model are opposite to the usual conventions.

So let’s make a deal. We’ll dive right in within this first chapter. Once you’ve installed the software, you can run some of the sample programs and, if you're feeling brave, perhaps even try modifying them.

But the code will look strange and won’t make much sense – you’ll be riding blind – and when you make a small mistake you’ll be clueless as to what’s wrong and how to fix it. It just won’t work and you won’t know why. That’s when you know it’s time to start studying the theory like a pro. Don't become discouraged when something doesn't work, and give up. When that time comes, read the rest of the book; okay?

The Really Important Part

In programming, as in all other professions, the people who achieve the most are those who love what they do. If you enjoy your programming, you’ll find it hard to stop yourself learning new things. Your need to know will drive you forward and studying further will just be something that happens along the way.

So, above all, enjoy yourself as you master the world of C# programming!

Bb330922.5f510d01-62d2-4f6a-a934-78147901fa87(en-US,VS.80).png

To develop C# programs, you’ll need:

  1. The Microsoft .NET Framework 2.0, which is freely available from http://msdn2.microsoft.com/en-us/netframework/aa569263.aspx.

  2. A .NET Framework development environment. We recommend Microsoft Visual C# 2005 Express Edition, which is available free from Microsoft at http://msdn.microsoft.com/vstudio/express/visualcsharp/ . The Visual C# 2005 Express installation package includes the .NET Framework so you don’t need to worry about installing item #1 above separately.

  3. Lastly, you need to copy the book's example files to a drive on your own computer. Extract the file example.zip to any folder of your choosing. Just remember which location you choose since you’ll be asked to open the files from there later.

More About Microsoft Visual C# 2005 Express Edition

It is possible to create C# programs using nothing but a simple text editor (like Notepad) and something called the "C# compiler" included with the .NET Framework. To make things a little easier though, and to make sure you can carry on further once you've read this book, we suggest you use a development environment such as Visual C# Express. It is a lightweight environment for developing Windows applications and console (command-line) applications, using C#.

It's important that you understand that this book is not about teaching you about the Visual C# Express Edition software product - rather it teaches you about the C# language. Visual C# Express also has lots of snazzy features like writing C# code for you when you drag-and-drop a button or other control onto the page. That's cool and you're welcome to use it - but this book aims to teach you C# "from the ground up". We'd like you to learn to write C# code by hand first - so that you really understand what's happening.

If you want to learn more about the Visual C# Express development environment, go to the website at http://msdn.microsoft.com/vstudio/express/visualcsharp/.

Writing a New Program Using Visual C# Express

  • To start Visual C# Express, click START -> All Programs -> Microsoft Visual C# 2005 Express Edition.

  • To start a new project, simply select File -> New Project and then choose a project type. We will start with some Console applications and will later move on to Windows applications.

    Bb330922.adc44044-6d91-4066-8150-9368d21b7e23(en-US,VS.80).png
  • For example, try creating a new Console Application and clicking OK. When the coding window for Program.cs appears, delete all the code that is automatically inserted and type a simple program such as this. You can copy and paste it if you're feeling lazy. Or is that smart?!

    using System;
    
    class PleaseSayYo
    {
          static void Main()
          {
                Console.WriteLine("Yo!");
                Console.ReadLine();
          }
    }  
    

    Make sure you type it EXACTLY as above (except that formatting like italics can be ignored).  

  • Then click the Run or Play button (or press F5)

    Bb330922.0ec89b7a-9f0f-47a2-ab2b-2509dfdec4b1(en-US,VS.80).png
  • If the program runs successfully, it will appear after a few moments, in a window above the C# Express window.

    Bb330922.6890351d-4dfa-4a77-84d9-b996632cbd74(en-US,VS.80).png

When you're done, you must stop the program. Usually, you will do this by clicking the X in the top right-hand corner to close the window.

  • If there is some problem in the code of the program, you'll be alerted to this fact. For example, you notice that in the C# computer language, we must end every sentence or statement with a semicolon. If you try deleting the last semicolon ";" and then clicking the start button, you'll see the error message below.

    Bb330922.b96402e1-808b-4d2f-ad1b-b83f36c43eef(en-US,VS.80).png

In most cases, you will click "No", and will be shown some details in the "Error List" window, including an indication of which line number the error was found on. If you then double-click that error description, you will be taken to the line where the problem exists, so that you can try to correct it.

Bb330922.c59196bd-c575-4eae-bac9-ee77ba603967(en-US,VS.80).png

Running the Book's Sample Programs

Several of this book's example programs need accompanying files. Those programs won't run correctly if you simply paste the code in as above.

So once you've copied the example files to some folder on your computer, you should run them by:

  • In Visual C# Express, select File -> Open Project

  • Browse to the folder where you saved your example files, and select the project you want, which will always have a .csproj (c# project) extension.

  • Click the green Run button.

In order to make it easier to follow the code samples in the book, we have generally stuck to certain habits:

Consider this block of code as an example:

// Main method

static void Main()
{
      Application.Run( new MyButtonClass() );
}

// Event handler method

void MyButtonClickEventHandler( object sender, EventArgs e )
{
      mrButton.Text = "You clicked me!";
}

  • Code samples are written using a slightly different font to the rest of the text (Courier New)

  • Any words or symbols that we “choose ourselves” are written in italics. Examples above include “MyButtonClass” and “mrButton”.

  • Any words or symbols that are part of the C# language or built into the .NET Framework, such as “static void Main()” or “.Text”, we write with normal style.

  • Bold text simply indicates something we want to draw your attention to.

  • Two forward slashes // indicate comments to explain the code (actually this is a feature of the C# language). Anything on the line beyond them is ignored by the computer. An example above is “// Main method”.

Part II of the book contains most of the general concepts and we often summarize the key concepts there. These summaries are called “Building Blocks”. Here s a partial example showing how a Building Block appears.

Building Block : Classes

All C# programs are built inside classes. The basic structure of a class is written down for the computer as follows :

class Animal
{

}

You will probably want to play with Visual C# Express for a while - you may wish to explore its help files and see what it's all about. If you're new to C#, though, you'll probably soon find that having a programming tool is not enough - you need more than just a tool in order to communicate with the computer.

Bb330922.f2e63741-0ea8-4057-898c-800b50f0273b(en-US,VS.80).png

Then it's time to tackle the next part of the book - learning how to communicate with the computer using the C# programming language.

Show:
© 2014 Microsoft