Friday, January 30, 2015

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 workinghello.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 theStart menu-> Programs -> Microsoft .NET Framework SDK -> Documentation there is a search window, an index window, etc… try to learn more about public, private, protectedaccess 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 namespaceHelloUtil, alternatively, you could have typed (at he start of the file) using HelloUtil and avoid HelloUtil., that’s the way namespaces work.

No comments:

Post a Comment