This article is currently in the process of being translated into Swedish (~34% done).
Introduction to C# classes
I massor av programmeringstutorials sparas information om klasser till mycket senare. Men eftersom C # handlar om objektorienterad programmering och därmed klasser, kommer vi att titta på en grundläggande introduktion till de viktigaste funktionerna redan nu.
Först och främst är en klass en grupp relaterade metoder och variabler. En klass beskriver dessa saker, och i de flesta fall skapar du en instans av denna klass, nu kallad ett objekt. Till det här objektet använder du de definierade metoderna och variablerna. Naturligtvis kan du skapa så många instanser av din klass som du vill. Klasser och objektorienterad programmering i allmänhet är ett enormt ämne. Vi kommer att täcka en del av det i detta kapitel såväl som i senare kapitel, men inte allt.
I Hello world-kapitlet såg vi en klass som användes för första gången, eftersom allt i C # bygger på klasser. Låt oss utvidga vårt Hello world-exempel med en klass vi bygger på vår egen:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Car car;
car = new Car("Red");
Console.WriteLine(car.Describe());
car = new Car("Green");
Console.WriteLine(car.Describe());
Console.ReadLine();
}
}
class Car
{
private string color;
public Car(string color)
{
this.color = color;
}
public string Describe()
{
return "This car is " + Color;
}
public string Color
{
get { return color; }
set { color = value; }
}
}
}
Okay, lots of new stuff here, but almost all of it is based on things we've already used earlier in this tutorial. As you can see, we have defined a new class, called Car. It's declared in the same file as our main application, for an easier overview, however, usually new classes are defined in their own files. It defines a single variable, called color, which of course is used to tell the color of our car. We declared it as private, which is good practice - accessing variables from the outside should be done using a property. The Color property is defined in the end of the class, giving access to the color variable.
Besides that, our Car class defines a constructor. It takes a parameter which allows us to initialize Car objects with a color. Since there is only one constructor, Car objects can only be instantiated with a color. The Describe() method allows us to get a nice message with the single piece of information that we record about our car. It simply returns a string with the information we provide.
Now, in our main application, we declare a variable of the type Car. After that, we create a new instance of it, with "Red" as a parameter. According to the code of our class, this means that the color red will be assigned as the color of the car. To verify this, we call the Describe() method, and to show how easily we can create several instances of the same class, we do it again, but with another color. We have just created our first functional class and used it.
In the following chapters, concepts like: properties, constructors, and visibility will be explained in more depth.