What are C# Properties and How to Implement Them in Your C# Program?
A property is a data member that offers you a way to access and change the private fields of a class, interface and structs. Properties in C# help to work around the “data encapsulation” or “data hiding” concept of object oriented language. In encapsulation, current state of an object is hidden from the outside world. C# is a very powerful object oriented language. Learning how to manipulate hidden data while promoting safety, and flexibility of methods is an important part of coding in C#.
Getting started with C#? Learn the basic concepts
Properties retrieve value of the private fields and allow you to change the value of the fields. You can write, read or compute the value of an internal data. The properties can be thought of as public data members. In Java programming, the states or current values of the objects can be changed through the get and set methods. C# properties work in a slightly different way. The property member has special methods called “accessors”. The assessors include the executable statements associated with reading and writing actions. The get accessor is used for retrieving a value, and the set accessor is used for changing a value. A property with both get and set accessors is called a read-write property. One with just the set accessor is called a write-only property — you cannot reference a property with only a set accessor. A read-only property has only the get accessor — you cannot assign a value to a read-only property.
What does the C# properties block include?
A basic property block includes:
- The access level of the field
- The type of the property
- Name of the property
- A block of executable statements that declares a get-accessor and/or a set-accessor.
Accessor declaration: [attributes][modifiers] type identifier
Or
[attributes][modifiers] type interface-type.identifier
Getting started with C# syntax? Learn the language at Udemy
Consider the following examples to see how a property fits in C#:
The get accessor:
The get accessor reads the value of the internal field and returns the value. It works as a method; it returns a value of the property type.
private string address; // the internal address field public string Address // the Address property (property declaration) { // property block starts get { return address; } } // property block ends
The above code returns the value of a private field “address”. You would have noticed the type of the returned value is the same as the type of the property.
The get accessor must not be used for changing the state of the field.
private int amount;
public int Amount { get { return amount++; // this changes the state of the object and must be avoided } }
You can also use the get accessor to compute a value and return it.
For example:
Class AddressBook { private string address; public string Address { get { return address != null ? address : “NA”; } } }
In the above code example, the get accessor will return the value NA if you do not assign a value to the Address property.
The set accessor:
The set accessor behaves like a method with return type void. It utilizes a parameter called value; it has the same type as the type of the property. In the following code example, a set accessor is added to the Address property.
public string Address { get { return address; } set { address = value; } }
In the above lines of code, you can invoke the set accessor with an argument, when you assign a value to the property.
For example:
If u have an object a1 of a class —let’s say AddressBook — containing the field: address and the property: Address, you can assign a value to the property.
AddressBook a1 = new AddressBook(); a1.Address = “Newark”; // this invokes the set accessor System.Console.Write(a1.Address);
It is important to remember that the value parameter cannot be used implicitly for a local variable declaration in a set accessor.
C# Interface Properties
Interfaces can have property declarations. An interface is a group of empty methods. It is implemented in a class, where all the empty methods get defined. A class that implements an interface must provide method bodies for all methods declared in the interface.
public interface myInterface { // property declaration string employeeID { get; set; } }
In an interface, the accessors do not have bodies. The sole purpose of the accessors here is to indicate whether the property is read-only, write-only or read-write.
Implement Interface with C# properties accessors
An access modifier is not used in an accessor that implements an interface. However, you can use an access modifier if you implement the interface using just one accessor such as the get accessor.
public interface Myinterface { int age { get; // accessor has no access modifier because it is an interface } } public class Myclass : MyInterface { get {return 24;} protected set { } // interface property has no set accessor, so access modifier is allowed } }
Keywords that can be used with C# properties
Properties can be marked as private, internal, public or protected internal. These access modifiers determine how you can access the property. A single property can have different access modifiers for the get and set accessors. For instance, the set accessor may be protected or private while the get accessor may be public to allow read-only access.
For Example:
private string address = “ park street”; public string Address { get { return address; } protected set { Address = value; } }
In the above example, the Address property includes a set and get accessor. The get accessor attains the accessibility level of the property, which in this case is public. The set accessor has protected as the access modifier; it is explicitly restricted.
The virtual keyword can be used to mark a property. This will allow you to override the property behavior in a derived class. The overridden property can be sealed to indicate it is no longer virtual in the derived class. Learn how to work with C# at Udemy today
Properties declared as abstract have no implementation in the base class; derived classes write their own property implementation.
The static keyword can also be used for declaring a property. You can access a static property even if no instance of a class exists.
Recommended Articles
Top courses in C# (programming language)
C# (programming language) students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.