A property in C# is a member of a class that provides a flexible way to read, write or compute the value of a private field. It acts like a combination of a variable and a method.
- Declared inside a class using { get; set; }.
- get: returns the value of the field.
- set: assigns a value to the field.
- Can be read only (get only) or write only (set only).
- Improves encapsulation by controlling access to fields.
Example: Property with backing field
C# private int age; public int Age { get { return age; } set { if (value >= 0) age = value; else Console.WriteLine("Age cannot be negative!"); } } There are two main reasons to use properties:
- To access private data members of the class from another class through accessors.
- To protect members of the class so another class may not misuse those members.
Accessors
The block of “set” and “get” is known as Accessors. It is very essential to restrict the accessibility of the property. There are two types of accessors: get accessors and set accessors.
Example: Problem Without Properties
C# using System; public class C1{ // Public data members (No restrictions) public int rn; public string name; // Private field (Cannot be accessed directly) private int marks = 35; } public class Geeks { public static void Main(string[] args) { // Creating an object of class C1 C1 obj = new C1(); // Setting values to public data members obj.rn = 10000; obj.name = null; // Directly modifying private field is not possible obj.marks = 0; // Error Console.WriteLine("Name: {0} \nRoll No: {1}", obj.name, obj.rn); } } Output:
./Solution.cs(25,14): error CS0122: `C1.marks' is inaccessible due to its protection level
Compilation failed: 1 error(s), 0 warnings
Explanation:
- The public fields can be modified directly, without restrictions.
- The private fields cannot be accessed directly, making it difficult to modify values when needed.
- We use properties for controlled access.
Syntax of Properties
This is the syntax for defining Properties:
<access_modifier> <return_type> <property_name>{
get { // body }
set { // body }
}
These are the different terms used to define the properties
- Access-modifiers: Used to define the accessibility levels. It can be public, private, protected or internal.
- Return-type: Used to define which value returns with the end of the method.
- Get accessor: Used to get the values.
- Set accessor: Used to set or modify the values.
Get Accessor (Read-only Property)
It specifies that the value of a field can be accessed publicly. It returns a single value and it specifies the read-only property.
Example:
C# using System; class Geeks { // Private field private int r = 357; // Read-only property public int Roll_no { get { return r; } } public static void Main(String[] args) { Geeks o = new Geeks(); Console.WriteLine("Roll no: " + o.Roll_no); } } Set Accessor (Read-Write Property)
It will specify the assignment of a value to a private field in a property. It returns a single value and it specifies the write-only property.
Example:
C# using System; public class Geeks { private int r; // Read-Write Property public int RollNo { get { return r; } set { r = value; } } public Geeks(int rollNo) { this.RollNo = r; } } class MainClass { public static void Main(string[] args) { Geeks o = new Geeks(147); Console.WriteLine($ "Current Roll No: {o.RollNo}"); // Modify RollNo using setter o.RollNo = 357; Console.WriteLine($ "Changed Roll No: {o.RollNo}"); } } OutputCurrent Roll No: 0 Changed Roll No: 357
Types of Properties
- Read and Write Properties: When the property contains both get and set methods.
- Read-Only Properties: When the property contains only the get method.
- Write Only Properties: When the property contains only a set method.
- Auto-Implemented Properties: When there is no additional logic in the property accessors it is introduced in C# 3.0.
Accessor Accessibility
- We can not use accessor modifiers on an interface or an explicit interface member implementation.
- We can use accessor modifiers only if the property has both set and get accessors.
- If the property is an override modifier, the accessor modifier must match the accessor of the overridden accessor.
- The accessibility level on the accessor must be more restrictive than the accessibility level on the property.
Example 1: Read-Write property example
C# using System; public class Student { private int rollNo; // Read-Write Property public int RollNo { get { return rollNo; } set { rollNo = value; } } public Student(int rollNo) { this.RollNo = rollNo; } } public class Geeks { public static void Main(string[] args) { Student obj = new Student(147); Console.WriteLine($ "Roll No: {obj.RollNo}"); obj.RollNo = 123; Console.WriteLine($ "Changed Roll No: {obj.RollNo}"); } } OutputRoll No: 147 Changed Roll No: 123
Example 2: Auto-implemented property example
C# using System; public class Student{ // Auto-implemented property public string Name { get; set; } = "GFG"; } class Geeks{ public static void Main(string[] args){ Student s = new Student(); Console.WriteLine("Name: " + s.Name); // Changing property value s.Name = "Geeky"; Console.WriteLine("Changed Name: " + s.Name); } } OutputName: GFG Changed Name: Geeky
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers