C# Classes what the heck are they
A class is basically a technique of using one or a group of variables to be used as a foundation for a more detailed variable. I know that sounds a little confusing so let me show you how to create a class. To create a class, you start with the class keyword followed by a name and its body delimited by curly brackets. Below is an example of a class called Hand:
class Hand
{
}
A class is created in a code file. As such, you can include it in the first file of your project. Here is an example:
using System;
class Hand
{
}
class Program
{
static void Main()
{
int fingers = 10;
}
}
You can also create a class in its own file. To assist you with this, Microsoft Visual C# provides a wizard. To use it, on the main menu, you can click Project -> Add Class… or Project -> Add New Item… In the Templates list, click Class. In the Name a text box accept or change the default name and click Add. A new file named after the class with the .cs extension would be added to your project.
Like any normal variable, to use a class in your program, you can first declare a variable for it. To declare a variable of a class, you can use the var keyword. Alternatively, you can type its name followed by a name for the variable. For example, to declare a variable of the above Hand class, you could type the following:
using System;
class Hand
{
}
class Program
{
static void Main()
{
var property . . .
}
}
Or the following:
using System;
class Hand
{
}
class Program
{
static void Main()
{
Hand property;
}
}
The variables we have declared so far are called value variables. This is because such variables of primitive types hold their value. The C# language supports another type of variable. This time, when you declare the variable, its name does not hold the value of the variable; it holds a reference to the address where the actual variable is stored in memory. This reference type is the kind used to declare a variable for a class.
To use a variable as reference, you must initialize it using an operator called new. Here is an example:
using System;
class Hand
{
}
class Program
{
static void Main()
{
var Property = new Hand();
}
}
If you are using the name of the class instead of var to declare the variable, you can first declare it. Then, on another line, you can allocate memory for it using the new operator. Here is an example:
using System;
class Hand
{
}
class Program
{
static void Main()
{
Hand Property;
// You can do something here
Property = new Hand();
}
}
In C#, as well as Visual Basic, if you create a class in any of the files that belong to the same project, the class is made available to all other files of the same project.
The section between the curly brackets, { and }, of a class is referred to as its body. In the body of a class, you can create a list of the parts that make up the class. Each of these parts must be a complete variable with a name and a data type. For example, here are the characteristics that make up the hand, declared as the parts of the class and each declared as a variable:
public class Hand
{
string Skin Color;
uint fingers;
decimal Value;
}
The variables declared in the body of a class are referred to as its member variables and each member variable is referred to as a field. When creating a class, it is your duty to decide what your object is made of.
I hope you have grasped the concept of what a class is.