codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

23 / 60 topics
17Classes and Objects18Constructors in C#19Inheritance in C#20Polymorphism in C#21Encapsulation in C#22Access Modifiers in C#23Properties in C#24Indexers in C#25Events in C#26Delegates in C#
Tutorials/C# Programming/Properties in C#
🔷C# Programming

Properties in C#

Updated 2026-05-15
10 min read

Properties in C#

Introduction

In object-oriented programming, encapsulation is a fundamental concept that involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit or class. Encapsulation also restricts direct access to some of an object's components, which can prevent the accidental modification of data. In C#, properties provide a way to encapsulate fields by allowing you to define methods that are called when a field is accessed or modified.

Concept

A property in C# is a member that provides a flexible mechanism for reading, writing, or computing the value of a private field. Properties can include both a getter and a setter method, which control how the data is accessed and modified. This allows you to add logic to enforce validation rules, perform additional actions when a value changes, or compute values dynamically.

Key Features of Properties

  1. Encapsulation: Properties allow you to hide the implementation details of a class by exposing only the necessary parts.
  2. Validation: You can include validation logic in the setter method to ensure that only valid data is assigned to a field.
  3. Computed Values: Properties can return computed values based on other fields or methods.
  4. Read-Only and Write-Only Properties: You can define properties that are read-only (no setter) or write-only (no getter).

Examples

Let's explore some practical examples to understand how properties work in C#.

Basic Property Example

Here is a simple example of a class with a property:

csharp
1public class Person
2{
3 private string name;
4
5 public string Name
6 {
7 get { return name; }
8 set { name = value; }
9 }
10
11 public void DisplayName()
12 {
13 Console.WriteLine("Name: " + name);
14 }
15}
16
17class Program
18{
19 static void Main(string[] args)
20 {
21 Person person = new Person();
22 person.Name = "Alice";
23 person.DisplayName(); // Output: Name: Alice
24
25 Console.WriteLine("Accessing property directly: " + person.Name); // Output: Accessing property directly: Alice
26 }
27}

In this example, the Name property encapsulates the private field name. The getter and setter methods allow you to access and modify the value of name.

Property with Validation

You can add validation logic in the setter method to ensure that only valid data is assigned:

csharp
1public class Person
2{
3 private string name;
4
5 public string Name
6 {
7 get { return name; }
8 set
9 {
10 if (string.IsNullOrEmpty(value))
11 {
12 throw new ArgumentException("Name cannot be null or empty.");
13 }
14 name = value;
15 }
16 }
17
18 public void DisplayName()
19 {
20 Console.WriteLine("Name: " + name);
21 }
22}
23
24class Program
25{
26 static void Main(string[] args)
27 {
28 Person person = new Person();
29 try
30 {
31 person.Name = ""; // This will throw an exception
32 }
33 catch (ArgumentException ex)
34 {
35 Console.WriteLine(ex.Message); // Output: Name cannot be null or empty.
36 }
37 }
38}

In this example, the setter method checks if the value is null or empty and throws an exception if it is. This ensures that the name field always contains a valid string.

Read-Only Property

A read-only property has only a getter method:

csharp
1public class Circle
2{
3 private double radius;
4
5 public Circle(double radius)
6 {
7 this.radius = radius;
8 }
9
10 public double Radius
11 {
12 get { return radius; }
13 }
14
15 public double Area
16 {
17 get { return Math.PI * radius * radius; }
18 }
19}
20
21class Program
22{
23 static void Main(string[] args)
24 {
25 Circle circle = new Circle(5);
26 Console.WriteLine("Radius: " + circle.Radius); // Output: Radius: 5
27 Console.WriteLine("Area: " + circle.Area); // Output: Area: 78.53981633974483
28 }
29}

In this example, the Radius property is read-only, meaning it can only be set during object initialization. The Area property is a computed property that returns the area of the circle based on its radius.

Write-Only Property

A write-only property has only a setter method:

csharp
1public class Logger
2{
3 private string logFile;
4
5 public string LogFile
6 {
7 set { logFile = value; }
8 }
9
10 public void WriteLog(string message)
11 {
12 File.AppendAllText(logFile, message + Environment.NewLine);
13 }
14}
15
16class Program
17{
18 static void Main(string[] args)
19 {
20 Logger logger = new Logger();
21 logger.LogFile = "log.txt";
22 logger.WriteLog("This is a log message.");
23 }
24}

In this example, the LogFile property is write-only, meaning it can only be set after object initialization. The WriteLog method writes messages to the specified log file.

What's Next?

Now that you have learned about properties in C#, you should explore how they contribute to encapsulation and data validation. In the next section, we will dive into indexers in C#, which provide a way to access elements of a collection using an indexer syntax similar to arrays.

Stay tuned for more tutorials on object-oriented programming in C#!


PreviousAccess Modifiers in C#Next Indexers in C#

Recommended Gear

Access Modifiers in C#Indexers in C#