C# Tutorial For Beginners (Part II)

Congratulation you've done the most difficult, let increase the difficulty. and create an object instance. in the DOS shell create a new directory:

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


and then type, in the notepad

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();
}
}


Wouah, 25 lines! That's a program! Save it, compile it, run it...
What happened? csc look for a Main() function in your program, it should find one (and only one) and it will be the entry point of your program.
In this tutorial we create 2 classes: Echo & 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 MS convention, do as it pleased you.
public is a visibility access, method wich are not public could not be seen from outside, there is also other visibility keywords, to learn more, clic on 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.

Comments

Leave a Reply

Awards

100 Blog Indonesia Terbaik


IP
Powered by Blogger.