C# Tutorial For Beginners

C# can appear a challenge at first, but follow our simple four part C# tutorial and you will be up and running in no time. Note that can  download this complete tutorial here. There is a directory for each tutorial with a build.bat to build the tutorial and a run.bat to run the program. And every example is already compiled, just in case…

C# Tutorial Part 1

You should first open a DOS command shell. (click on the Start menu then run (at the bottom) and type, in the text field: “cmd”. ) .

For this C# tutorial we will begin working in an empty directory, let call it “C:\learncs”. Type in the shell:

> md C:\learncs
> cd C:\learncs
> C:

Now let’s create your first C# program, type “notepad hello.cs” and then  type (in the notepad)

using System;

public class Hello
{
public static void Main()
{
Console.WriteLine("Hello C# World :-)");
}
}

the using keyword just lets you write Console at line 7, instead of System.ConsoleUsing is a very usefull shortcut when you use a lot of “class” define in System. Save the file.

Now you can compile you first C# program. Back in the DOS Shell again, type:
csc /nologo /out:hello.exe hello.cs
You may have some errors, correct them, compile again, and now you have a working hello.exe program… type hello, see…

C# Tutorial Part 2

Congratulation you’ve done the most difficult part, now lets dive a little deeper and create an object instance, in the DOS shell create a new directory:

> md ..\learncs2
> cd ..\learncs2
> notepad hello.cs

and then in the notepad type:

using System;

public class Echo
{
string myString;

public Echo(string aString)
{
myString = aString;
}

public void Tell()
{
Console.WriteLine(myString);
}
}

public class Hello
{
public static void Main()
{
Echo h = new Echo("Hello my 1st C# object !");
h.Tell();
}
}

Just 25 lines! That’s a program! Save it, compile it, run it…
What happened? Always look for a Main() function in your program, there should be one (and only one) and it will be the entry point for  your program.
In this tutorial we create 2 classes: Echo and Hello. In the Main() method you create an Echo object (an instance of the Echo class) with the keyword new. Then we called the instance method “Tell()”. The upper case letter on class or Method is just a Microsoft convention.
Public is the visibility access, methoda which are not public can not be used from outside the current class (or ‘scope’), there is are other visibility keywords, to learn more, click on the Start menu-> Programs -> Microsoft .NET Framework SDK -> Documentation there is a search window, an index window, etc… try to learn more about public, private, protected access controls.

C# Tutorial Part 3

Now you become to be pretty confident,  so we could start using multiple fileq, and even a dll. Navigate to an other directory (or stay in this one) and create 2 files:

hello.cs

using System;

public class Hello
{
public static void Main()
{
HelloUtil.Echo h = new HelloUtil.Echo("Hello my 1st C# object !");
h.Tell();
}
}

echo.cs

using System;

namespace HelloUtil
{
public class Echo
{
string myString;

public Echo(string aString)
{
myString = aString;
}

public void Tell()
{
Console.WriteLine(myString);
}
}
}

Note in hello.cs I have used the syntax “HelloUtil.Echo” because Echo is in the namespace HelloUtil, alternatively, you could have typed (at he start of the file) using HelloUtil and avoid HelloUtil., that’s the way namespaces work.

Continues…

Pages: 1 2

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

6 Responses to “C# Tutorial For Beginners”

  1. i love it..
    its great.. and really simple.
    thanx ..

  2. nice…easy for understanding especially for the begineers

  3. Oh wow!!!…Your tutorial is awesome…Thanks a lot!!!

  4. Thank you,
    I loved this tutorial.
    Its simple, and answered my questions.

  5. for 64 bit users use these commands to built.

    csc /nologo /t:library /out:”echo.dll” “echo.cs” /platform:x86

    csc /out:”hello.exe” /r:”echo.dll” hello.cs /platform:x86

    it took me 30 min before i figured out XD

  6. Thanks!!!

    Its very simple way to understand C#.
    very Helpful for beginners.