c sharp

How to Differentiate Between C# Properties and Fields

Regardless of what your thoughts and opinions are about OOP, if you are working with languages such as C#, you are bound to encounter them sooner or later.

Now, one of the most powerful concepts of OOP is data encapsulation which is essentially a mechanism of restricting a direct access to the components of an object. Think of it as a way of preventing unintended modifications to the data that is stored in an object.

The way encapsulation works is by the use of two main components namely: fields and properties.

In this tutorial, we will walk you through the workings and differences between a field and property in C#.

Fields and Properties

Let us start with a more basic outlook.

Both fields and properties provide mechanisms from reading, writing, or computing the values of an object.

However, fields act as direct variables that are entirely private to enforce the encapsulation of the data. Fields are accessed by the use of properties.

Hence, we can say that are the means of accessing the fields using the “getter” and “setter” methods. This allows an extensive control over the access and modification of the data that is stored in the object.

C# Fields

We begin with the fields and discuss their definition, usage, and functionality.

As we mentioned, fields are variables that we declare directly within a class, interface, struct, etc. They act as members of class and are often (more required) to be private. This helps to ensure that the field can only be accessed or changed in a specific method.

Take a look at the following example class with a field definition:

public class Car

{

  private string model;

}

In this example, “model” is a private field of the “Car” class. Once we set the field as private, you cannot access it directly outside of the class in which it is defined.

The only way to access or modify (get or set) this field is by either using a method or a property.

C# Properties

As you can probably guess, properties are members of a class and their role is to provide a flexible mechanism for reading, writing, or computing the value of a private field.

You can use a property like a public member but they use special methods which are known as “accessors”.

Consider the following example:

public class Car
{
    private string model;

    public string Model;
    {
        get { return model; }
        set { model = value; }
    }
}

In this example, we define a property called “Model”. Note that the property is public while the field itself is private.

The property provides external classes with the access to the private field.

You will notice the two methods: get and set. These are what we call as “accessors”. The role of the “get” accessor is to return the value of the field while the “set” accessor helps to set a new value for it.

This feature plays a crucial role in helping to hide the actual implementation of the class and fields from external classes.

Main Differences between Fields and Properties

With all that being said, the following is a summary of the difference between the two:

  1. Fields are private while properties are public that provide access to the fields.
  2. Fields are data storage units and offers no logic. Properties, on the other hand, can provide additional logic beyond the stored value.

Conclusion

In this tutorial, we walked you through a major components of OOP, namely fields and properties. We explained what they do, how they work, and the major differences between them.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list