TOC

The community is working on translating this tutorial into Greek, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Classes:

Fields

One of the most basic building blocks of a class is a field. It's just like a variable, which we talked about previously, but defined on the class level instead of the method level. The difference is quite important and it has everything to do with the concept of scopes, which decides from where a variable can be accessed from: A local variable (defined inside a method) can only be accessed from this specific method, while a class field can be accessed from all methods of a class and even from methods in other classes if the visibility allows it.

In other words, the difference between a variable and a field is pretty much where it's declared. A class-level variable is referred to as a field, while a method-level variable is usually just referred to as a variable.

Fields are often declared near the top of the class and their visibility are often set to private (we'll discuss visibility later in this chapter). It could look like this:

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!