Compare the Difference Between Similar Terms

Difference Between Field and Property in C#

Key Difference – Field vs Property in C#
 

The key difference between field and property in C# is that a field is a variable of any type that is declared directly in the class while property is a member that provides a flexible mechanism to read, write or compute the value of a private field.

C# is a modern programming language developed by Microsoft. It is general purpose programming language. The Common Language Interface (CLI) consists of runtime environment and the executable files. C# is a language built on .NET framework. It provides automatic garbage collection, delegates, Language Integrated Query (LINQ), etc. to write programs easily and faster. One main advantage of C# is that it supports Object Oriented Programming (OOP). It helps to construct a program or a software using objects. In a system, there is a lot of objects, and their objects pass messages using methods. Field and Property are two terms associated with OOP. This article discusses the difference between field and property in C#.

CONTENTS

1. Overview and Key Difference
2. What is Field in C#
3. What is Property in C#
4. Relationship Between Field and Property in C#
5. Side by Side Comparison – Field vs Property in C# in Tabular Form
6. Summary

What is Field in C#?

Every object has characteristics and behaviours. The characteristics are described by the fields, and the behaviours are described by the methods. An Employee object can have fields such as employee no, name and department.

Figure 01: Program with public fields

According to the above, the Triangle is a class. It contains three public fields, which are base1, height1 and area. The constructor can assign the values for base1 and height. In the main method, an object of Triangle is created. It is called t1, and two values are passed to the base and height. The constructor in the Triangle class assigns those values to the fields. Then, in the main method, the calArea method is called. It will calculate the area of the triangle and assigns the answer to the area field. Finally, the display method will call, and it will output the answer on the screen.

One main pillar of OOP is Encapsulation. It allows compacting the fields and methods into a single unit. Encapsulation is used to protect the data. Access specifiers can be used to change the visibility of the fields and methods. The public members can be accessed outside the class. The private members are accessible only within the class. To limit the accessibility only to the class, the fields can be made private. The setting and getting values can be done with public methods.

Figure 02: Program with private fields

According to the above program, the Triangle is a class. There are fields called base1 and height1. They are private fields. In the main method, an object of Triangle is created. The details method is called on the Triangle instance. The values for the base1 and height1 are accepted by the details method. Those gained values are assigned to base1 and height1 fields. In the main method, the calArea method is called on the t1 object. It calculates the area. Finally, the display method prints the area of the triangle. The fields are private, but they are accessible by the public methods.

What is Property in C#?

The properties do not have storage locations. The properties have accessors that contain the executable statements to read the values and to set the values. The accessor declarations can contain a get accessor and a set accessor. Assume that there is a class name Employee and it contains the private fields such as employee no, name and department. These fields cannot be accessed from outside the class directly. Therefore, the programmer can use properties to set and get values. Therefore, the properties can be used to access the private fields.

Declaring the name property of type String is as follows. The ‘value’ keyword refers to the assigned value.

public string Name {

get {return name;}

set {name = value;}

}

Refer the below program,

Figure 03: C# program with properties

The Employee class has two private fields that are id and name. The ID and Name are properties. The id value is set and get using the property ID. The name value is set and get using the property Name. In the main method, an object of Employee is created. The private id and private name fields of Employee class are accessed using the properties. Finally, the values are displayed on the screen.

What is the Relationship Between Field and Property in C#?

What is the Difference Between Field and Property in C#?

Field vs Property in C#

A field is a variable of any type that is declared directly in a class. A property is a member that provides a flexible mechanism to read, write or compute the value of a private field.
 Usage
A field can be used to explain the characteristics of an object or a class. A property can be used to set and receive values of a field.

Summary – Field vs Property in C#

In OOP, the program or the software can be modelled using objects. The objects are created using classes. A class is a blueprint to create objects. Fields and properties are two terms used in C# OOP. This article discussed the difference between field and property in C#. The difference between field and property in C# is that a field is a variable of any type that is declared directly in the class while property is a member that provides a flexible mechanism to read, write or compute the value of a private field.

Reference:

1.“Fields (C# Programming Guide).” Microsoft Docs. Available here
2.“Properties (C# Programming Guide).” Microsoft Docs. Available here
3.“C# Properties – Javatpoint.”, JavaTPointAvailable here
4.“C# Classes.”, Tutorials Point, 19 Mar. 2018. Available here
5.“C# Properties.”, Tutorials Point, 19 Mar. 2018. Available here