TOC

This article is currently in the process of being translated into Dutch (~31% done).

Klassen:

Velden (Fields)

Een van de meest basis bouwstenen van een klasse is een veld. Het is net als een variabele, waar we het eerder over hadden, maar gedefinieerd op klasse niveau in plaats van het methode niveau. Het verschil is best belangrijk en heeft alles van doen met de reikwijdte, welke beslist van waaruit een variabele kan worden benaderd: een lokale variabele ( gedefinieerd binnen een methode) kan alleen benaderd worden vanuit deze specifieke methode, terwijl een klasse veld vanuit alle methoden van een klasse benaderd kan worden en zelfs vanuit methodes in ander klassen wanneer de zichtbaarheid het toestaat.

Met andere woorden, het verschil tussen een variabele en een veld is eigenlijk de plek waar deze gedeclareerd wordt. Een klasse-niveau variabele wordt een veld genoemd, terwijl een methode- niveau variabele normaal gesproken een variabele wordt genoemd.

Velden worden vaak gedeclareerd bovenin de klasse en hun zichtbaarheid wordt vaak op private gezet ( we bespreken zichtbaarheid verderop in dit hoofdstuk). Het zou er als volgt uit kunnen zien:

public class FieldsSample
{
	private string name;
	....

We now have a class-level variable called "name", which can be accessed from all methods of this class. It can't be accessed from outside the class, but only because we have marked it as private. You are free to declare your fields as protected if you want to access them from derived classes or even public if you want to access them from anywhere, but keep in mind that the recommended way of accessing fields from outside a class is through properties, which we'll discuss in the next article.

In our example above, or "name" variable doesn't have an initial value, meaning that you will have to assign something to it before you can use it. If you already know which value your field should start with, you can easily assign it at the same time as declaring it:

public class FieldsSample
{
	private string name = "John Doe";
	....

As an alternative, you can assign a value to a field in the constructor of the class. This will overwrite any value assigned to the field at the declaration point and will allow you to assign dynamic values.

As we talked about previously, members of a class can be accessed with the dot-notation, like "class.member". However, when we are accessing a member of the current class, we use the "this" keyword, like "this.member". Here's a more complete example where we declare a couple of fields and use them inside a method:

public class Fields1
{
	private string name = "John Doe";
	private int age = 42;

	public void Describe()
	{
		string description = this.name + " is " + this.age + " years old...";
		Console.WriteLine(description);
	}
}

In the Describe() method (and don't worry, we'll get to methods in one of the next articles) we declare a local variable called "description", with a value based on our two declared fields. The "description" variable is a great example of a variable which should always be a variable and never a field: It's temporary and only relevant to the method which uses it, where the fields could easily be relevant in other methods of the class.

Summary

Fields act a bit like global variables because they can be used to store data which can then be accessed from anywhere inside the class. They can be accessed from outside the declaring class as well, but normally, properties are used for this purpose. We'll talk about properties in the next article.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!