Now it is time to learn how to declare and use a class. In the first steps of this tutorial, you saw a "Hello World!" sample. You could observe that you had to declare a class, event you didn't have to use it. This situation is because C# is a 100% OO language, and it won't let you declare any function or even variable outside a class. Even the Main() method, the entry point of an application, must be contained in a class.
The best for learning is writing code, so, let's see this first approach to a class declaration. Note that when you declare a class, you are only defining the prototype of the class, and if you want to use an instance - create a variable of that type -, you have to declare it. So, let's declare a useless class:
class MyClass { }
As you can see, there's no difficult in declaring a class. Observe the reserved word class, which lets us declare a new class type. Next we have the word MyClass, which is the name of the new user defined type. Finally, pay attention on the brackets; inside them we will be able to put any method/field we could need.
Next, if you want to use it, you have to use declare an instance of it:
MyClass class = new MyClass();
With that line, you are creating an instance. Note the reserved word new; it is neccesary for the creation of more instances. Did you see the () ? you can make use of them if you want to pass a parameter when declaring a new instance. Don't worry if you feel you need to know it better; we will be learning this topic in the next sections, and is called constructor.
All right. But this class does nothing, event if we want to create an instance of it. So, let's add it a pair of elements: a field and a method.
int myField = 12; public int GetResult ( int n ) { return myField*n; }
By the way: if you don't know what a function/method is, don't worry and try to pay attention to this example. Continuing with our travel, you will see we have a method that returns a int, the multiplication of a parameter, n, and our field, MyField, with value of 12. You will appreciate that the declaraton of our method has a public word. With it, we are saying "we want to let anyone to access this method" ( we will be studying this topic in the next sections ).
A entire example of this could be:
// // class-basics.cs // A first approach to a class // using System; // // Our test class // class MyClass { int myField = 12; public int GetResult ( int n ) { return myField*12; } } // // Our class containing the Main() method // class Tester { static void Main () { MyClass class = new MyClass(); int n = 56; // // Call the public accessible method // Console.WriteLine(" The value of GetResult({0}) is {1}", n, class.GetResult(n)); } }
Save it as class-basics.cs and compile it:
mcs class-basics.cs
And run it with
mono class-basics.exe
If everything went perfect, you should see:
The value of GetResult(56) is 672