Difference Between “Field” and “Property” in C# OOP

Cem Tuğanlı
5 min readMar 21, 2023

--

What is a “Field”?

A field is a member variable that is declared within a class or struct and is used to hold data for an instance of a class/objects of a class.

All non-static fields are called “instance fields”. As a naming rule, all private fields must have “_” before their name and must start with a not capital letter.

All Fields are typically declared at the class level, and are used to represent the state of an instance of a class and can be accessed by methods. (And properties of a class.)

public class PersonTitleName
{
//Fields
private string Name;
private string Title;

//...

}

There are a few situations where fields can be defined elsewhere:

Structs: While structs and classes share many similarities, they also have some significant distinctions. Although structs can have fields, they can not be initialized in the struct declaration, unlike classes. Instead, when a new struct instance is created, each field must be initialized individually. Also, struct instances are often passed by value rather than by reference, which has an impact on performance.

Nested classes: A nested class has been defined inside of another class. Although they are still linked to the outer class, fields defined inside nested classes are not technically defined at that class’s level. Only code contained within a nested class or its containing class can access fields defined in that class.

Partial classes: The “partial” keyword allows you to define a class across multiple files. Each component of the partial class may define its fields, but all components are combined to create a single class at compile time.

What is the use of a “Field” inside a class?

Fields let you separate data from the other code parts outside of the class by having necessary access modifiers like protected, private, and public and encapsulating it within a class. This can improve the organization, maintainability, and error prevention of your code.

public class PersonTitleName
{
//Fields
private string Name;
private string Title;

//initializing fields with a constructor
public PersonTitleName(string Name, string Title)
{
this.Name = Name;
this.Title = Title;
}

public string GetName()
{
return Name;
}

public string SetName(string Name)
{
this.Name = Name;
}

public string GetTitle()
{
return Title;
}

public string SetTitle(string Title)
{
this.Title = Title;
}
public void EnhanceTitle(string title2)
{
Title += title2;
}

public string GetFullName()
{
return Title + Name;
}
}

As you can see in the example above, fields, especially private fields can have methods that set or change their value and get their value to print and show on the screen. These methods are called “accessors”, they generally contain get or set prefixes before their name. Get accessor is also called “Getter” or “Accessor” while set accessor is called “Setter” or “Mutator”. These methods allow programmers to put validation checks and do changes to the fields when necessary. If the modifiers of fields are all private or protected, then public methods like these provide a means to access them from outside of the class by using these inside methods.

These methods can provide a layer of abstraction to ensure that any changes made to the fields are valid and meet certain criteria before being set. This can help ensure that the class is used correctly and the data within it is properly validated and protected.

So, What is a “Property”, then?

Properties are actually special methods. They also have accessors(getter) and mutators(setter) which in normal equal conditions do not vary in performance and speed whether in run-time or compile-time with respect to fields and their traditional getter and setter methods. It is important to note that in Microsoft documentation both getter and setter methods are collectively referred to as “accessor” methods.

Properties are an easier-to-write and more flexible way to access fields in a class, carrying the get and set capabilities for fields within themselves.

It removes the hustle to access a private or protected field by removing creating long codes of getter and setter methods to access a private field from outside of a class and thus, allows us to put validation checks and additional logic much more easily, which means that properties allow us to easily encapsulate the access to fields, allowing easily controlled access to the state of the class(its fields).

Additionally, it has to be noted that when you use a property to access a field, you are calling a special method (the property’s getter or setter) that is generated by the compiler behind the scenes.

public class Person
{
//Field
private string name;

//Property
public string Name
{
get { return name; }
set { name = value; }
}
}

As you can see in the code above, a private field can be accessed by a properties get — set structure. However, if you will not write validation and logic, it is not necessary to create a private field anymore when the properties are used, just use auto-implemented property:

public class Person
{
//Auto-Implemented Property
public string Name { get; set; }
}

This way, a private backing field is created in the background, and it can not even be seen with a debugger, though we know that the get & set structure accesses it and works with it.

So, now with properties; writing fields and their logic/validation checks are now easier.

Now let’s write the first PersonTitleName class code of Fields and regular methods, by using properties, and see how much it gets easier to write:

public class PersonTitleName
{
//Fields
private string name;
private string title;

// Initializing fields with a constructor
public PersonTitleName(string name, string title)
{
this.name = name;
this.title = title;
}

public string Name
{
get { return name; }
set { name = value; }
}

public string Title
{
get { return title; }
set { title = value; }
}

public void EnhanceTitle(string title2)
{
title += title2;
}

public string GetFullName()
{
return title + name;
}
}

See the difference?

Warning:

This code below is a common mistake which could result because of a typo mistake or not declaring the necessary Fields. The variable inside the Getters and Setters must not be the same as the name of Property’s, it must be same as the Field’s name. Or, it will give a Stack Overflow Error because of an infinite loop will fill all the relevant memory space.

public class PersonTitleName
{
//Stack Overflow Error
public string Title
{
get { return Title; }
set { Title = value; }
}
}

static void Main(string[] args)
{
//Create an instance of PersonTitleName class
PersonTitleName ptn = new PersonTitleName();

ptn.Title="Michael";
}

Normally, the order should go like this: In the main function the property is called by invoking it’s name inside of the main function. The property’s get or set method is activated for the relevant action, these methods access the Field and get or set it.

Resources :

Fields — C# Programming Guide | Microsoft Learn

Properties — C# Programming Guide | Microsoft Learn

A Clap or a Comment is much appreciated! Thank you for your time!

--

--