Before going on properties lets have a look at why the concept of properties came into C#? The is because of two reasons:
- If the members of a class are private then how another class in C# will be able to read, write, or compute the value of that field.
- If the members of the class are public then another class may misuse that member.
Example:
C#
using System;
public class C1
{
public int rn;
public string name;
}
public class C2
{
public static void Main( string [] args)
{
C1 obj = new C1();
obj.rn = 10000;
obj.name = null ;
Console.WriteLine( "Name: {0} \nRoll No: {1}" , obj.name, obj.rn);
}
}
|
Output:
Name:
Roll No: 10000
Explanation: In above you can see that public members of class C1 can be accessed by class C2 and using the object “obj” of C1 it can provide the values to the members like Name is given value null but we don’t want this to be null. C2 cannot provide the value to the member “marks” because it is private in C1. To test the private member access remove the comments and try to run and you can see the compiler will give an error. The Programming languages which do not have properties, use getter and setter methods to provide such access mechanism.
Using Properties
Properties are the special type of class members that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and helps to promote the flexibility and safety of methods. Encapsulation and hiding of information can also be achieved using properties. It uses pre-defined methods which are “get” and “set” methods which help to access and modify the properties.
Accessors: The block of “set” and “get” is known as “Accessors”. It is very essential to restrict the accessibility of property. There are two type of accessors i.e. get accessors and set accessors. There are different types of properties based on the “get” and “set” accessors:
- Read and Write Properties: When property contains both get and set methods.
- Read-Only Properties: When property contains only get method.
- Write Only Properties: When property contains only set method.
- Auto Implemented Properties: When there is no additional logic in the property accessors and it introduce in C# 3.0.
The syntax for Defining Properties:
<access_modifier> <return_type> <property_name>
{
get { // body }
set { // body }
}
Where, <access_modifier> can be public, private, protected or internal. <return_type> can be any valid C# type. <property_name> can be user-defined. Properties can be different access modifiers like public, private, protected, internal. Access modifiers define how users of the class can access the property. The get and set accessors for the same property may have different access modifiers. A property may be declared as a static property by using the static keyword or may be marked as a virtual property by using the virtual keyword.
- Get Accessor: It specifies that the value of a field can access publicly. It returns a single value and it specifies the read-only property.
Example:
class Geeks {
// Declare roll_no field
private int roll_no;
// Declare roll_no property
public int Roll_no
{
get
{
return roll_no;
}
}
}
- Set Accessor: 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:
class Geeks {
// Declare roll_no field
private int roll_no;
// Declare roll_no property
public int Roll_no
{
get
{
return roll_no;
}
set
{
roll_no = value;
}
}
}
Accessor Accessibility
- we can’t 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.
Below are the programs to demonstrate different types of properties:
Program 1: To demonstrate the Read-Only property using “get” accessor.
C#
using System;
public class Student {
private static int cnt;
public Student()
{
cnt++;
}
public static int Counter
{
get
{
return cnt;
}
}
}
class StudentTest {
public static void Main( string [] args)
{
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Console.WriteLine( "Total No of Student: " + Student.Counter);
}
}
|
Output:
Total No of Student: 3
Program 2: To demonstrate the both read & write property using “get” and “set” accessors.
C#
using System;
public class Student {
private string name = "GeeksforGeeks" ;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
class TestStudent {
public static void Main( string [] args)
{
Student s = new Student();
s.Name = "GFG" ;
Console.WriteLine( "Name: " + s.Name);
}
}
|
Output:
Name: GFG
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!