
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# - Properties (Get and Set)
Properties are named members of classes, structures, and interfaces. Member variables or methods in a class or structures are called Fields. Properties are an extension of fields and are accessed using the same syntax. They use accessors through which the values of the private fields can be read, written or manipulated.
Properties do not name the storage locations. Instead, they have accessors that read, write, or compute their values.
For example, let us have a class named Student, with private fields for age, name, and code. We cannot directly access these fields from outside the class scope, but we can have properties for accessing these private fields.
Accessors Properties
The accessor of a property contains the executable statements that helps in getting (reading or computing) or setting (writing) the property. The accessor declarations can contain a get accessor, a set accessor, or both. For example −
// Declare a Code property of type string: public string Code { get { return code; } set { code = value; } } // Declare a Name property of type string: public string Name { get { return name; } set { name = value; } } // Declare a Age property of type int: public int Age { get { return age; } set { age = value; } }
Example
In the following example, we demonstrate the use of the properties in C-sharp −
using System; namespace tutorialspoint { class Student { private string code = "N.A"; private string name = "not known"; private int age = 0; // Declare a Code property of type string: public string Code { get { return code; } set { code = value; } } // Declare a Name property of type string: public string Name { get { return name; } set { name = value; } } // Declare a Age property of type int: public int Age { get { return age; } set { age = value; } } public override string ToString() { return "Code = " + Code +", Name = " + Name + ", Age = " + Age; } } class ExampleDemo { public static void Main() { // Create a new Student object: Student s = new Student(); // Setting code, name and the age of the student s.Code = "001"; s.Name = "Zara"; s.Age = 9; Console.WriteLine("Student Info: {0}", s); //let us increase age s.Age += 1; Console.WriteLine("Student Info: {0}", s); Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following result −
Student Info: Code = 001, Name = Zara, Age = 9 Student Info: Code = 001, Name = Zara, Age = 10
Abstract Properties
In C#, An abstract class uses the abstract property to define the property that must be implemented in the derived class. They act as a contract ensuring that the subclass provides definite implementations −
Example
Following is another example of the properties in C#. Here, we use the abstract property in the abstract class −
using System; namespace tutorialspoint { public abstract class Person { public abstract string Name { get; set; } public abstract int Age { get; set; } } class Student : Person { private string code = "N.A"; private string name = "N.A"; private int age = 0; // Declare a Code property of type string: public string Code { get { return code; } set { code = value; } } // Declare a Name property of type string: public override string Name { get { return name; } set { name = value; } } // Declare a Age property of type int: public override int Age { get { return age; } set { age = value; } } public override string ToString() { return "Code = " + Code +", Name = " + Name + ", Age = " + Age; } } class ExampleDemo { public static void Main() { // Create a new Student object: Student s = new Student(); // Setting code, name and the age of the student s.Code = "001"; s.Name = "Zara"; s.Age = 9; Console.WriteLine("Student Info:- {0}", s); //let us increase age s.Age += 1; Console.WriteLine("Student Info:- {0}", s); Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following result −
Student Info: Code = 001, Name = Zara, Age = 9 Student Info: Code = 001, Name = Zara, Age = 10
Auto-Implemented Property
In C#, an auto-implemented property also known as an automatic property. It is a short-hand method to define the property. It allows us to declare a property without explicitly defining a backing field. The complier automatically generates the field behind the scene.
Example
In the following example, we use the auto-implement property that is set and get to set and get the name and age respectively of the student −
using System; class Student { // Auto-implemented properties public string Name { get; set; } public int Age { get; set; } } class Program { static void Main() { Student s = new Student(); // Setting property values s.Name = "Aman"; s.Age = 20; Console.WriteLine($"Name: {s.Name}, Age: {s.Age}"); } }
Following is the output of the above code −
Name: Aman, Age: 20
Read-Only and Write-Only Properties
The read-only property has only a get accessor and it can only be read but not set from outside the class. Whereas, the write-only property has only a set accessor and it can be set but not read.
Example
In the following example, we define a read-only property to store the creator name and a write-only property to set a password securely −
using System; class User { private string password; // Read-only property public string CreatedBy { get; } = "Ravi Kumar"; // Write-only property public string Password { set { password = value; } } public void ShowInfo() { Console.WriteLine("Created By: " + CreatedBy); Console.WriteLine("Password is stored securely."); } } class Program { static void Main() { User user = new User(); user.Password = "My$ecureP@ss"; user.ShowInfo(); } }
Following is the output of the above code −
Created By: Ravi Kumar Password is stored securely.
Property Validation
You can use validation inside a property's set accessor to restrict invalid values.
Example
In the following example, the Age property checks if the entered age is less than 18 and throws an exception if it is −
using System; class Employee { private int age; public int Age { get { return age; } set { if (value < 18) throw new ArgumentException("Minimum age is 18."); age = value; } } } class Program { static void Main() { Employee emp = new Employee(); emp.Age = 25; Console.WriteLine("Age: " + emp.Age); } }
Following is the output of the above code −
Age: 25