Please enable Javascript to correctly display the contents on Dot Net Tricks!

A Deep Dive into C# Property

Posted By : Shailendra Chauhan, 26 Nov 2013
Updated On : 27 Nov 2013
Total Views : 94,042   
 

A Property acts as a wrapper around a field. It is used to assign and read the value from a field by using set and get accessors. The code block for the get accessor is executed when the property is read and the code block for the set accessor is executed when the property is assigned a new value. A property can be created for a public, private, protected and internal field.

Unlike fields, properties do not denote storage locations and you cannot pass a property as a ref or out parameter.

using System;
class Example
{
 string name;
 public string Name
 {
 get { return name; }
 set { name = value; }
 }
}
class Program
{
 static void Main()
 {
 Example obj= new Example();
 obj.Name = "Dot Net Tricks"; // called set { }
 Console.WriteLine(obj.Name); // called get { }
 }
}

When to use

  1. Need to validate data before assigning it to a field.

  2. Need to do intermediate computation on data before assigning or retrieving it to a field.

  3. Need to log all access for a field.

  4. Need to protect a field by reading and writing .

Different types of properties

Properties can be divided into three categories read-only, write-only, and read-write properties.

  1. Read-Only Property

    A read-only property allows you to only retrieve the value of a field. To create a read-only property, you should define the get accessor.

    class Example
    {
     string name;
     public string Name
     {
     get { return name; }
     }
    }
    
  2. Write-Only Property

    A write-only property allows you to only change the value of a field. To create a write-only property, you should define the set accessor.

    class Example
    {
     string name;
     public string Name
     {
     set{ name = value; }
     }
    }
    
  3. Read-Write Property

    A read-write property allows you to assign and read the value of a field. To create a read-write property, you should define the set and get accessors.

    class Example
    {
     string name;
     public string Name
     {
     get { return name; }
     set { name = value; }
     }
    }
    

Auto-implemented property

Auto-implemented properties was introduced with C# 3.0, which make property declaration more concise. Unlike standard property, in auto-implemented property, wrapped or backing field is automatically created by the compiler but it is not available for use by the class's members.

public int Name { get; set; } 

To make an auto-implemented property read-only or write-only, you need to specify both get and set accessors.

public int ReadOnly { get; private set; }
public int WriteOnly { private get; set; }

When to use Auto-implemented property

Simply, you need to store a value. No additional functionality can be added to either of the accessors.

Static property

You can also declare a property static. To make a static property you must ensure that the backing store field is also static. Typically, a static property is used to make a singleton class.

public class Singleton
{
 private static Singleton instance = new Singleton();
 private Singleton() { }
 public static Singleton GetInstance
 {
 get { return instance; }
 }
}

Abstract property

An abstract property declaration does not provide an implementation of the property accessors. It leaves the accessors implementation to derived classes. Abstract properties are declared with in a abstract class or interface.

public abstract class Person
{
 public abstract string Name{ get; set;}
}
class Student : Person
{
 private string name;
 // Override Name property 
 public override string Name
 {
 get { return name; }
 set { name = value; }
 }
}
public interface IPerson
{
 string Name { get; set; }
}
class Student : IPerson
{
 private string name;
 // implement Name property 
 public string Name
 {
 get { return name; }
 set { name = value; }
 }
} 
What do you think?

I hope you will enjoy the property in C# while programming. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

 
 
About the Author
Hey! I'm Shailendra Chauhan full-time author, consultant & trainer. I have more than 6 years of hand over Microsoft .NET technologies and other web technologies like JavaScript, AngularJS, NodeJS etc. I am an entrepreneur, the founder & chief editor of www.dotnet-tricks.com and www.dotnettricks.com. I am author of most popular e-books for technical Interview on ASP.NET MVC Interview Questions and Answers & AngularJS Interview Questions and Answers & LINQ Interview Questions and Answers.
I have delivered 100+ training sessions to professional world-wide over Microsoft .NET technologies such C#, ASP.NET MVC, WCF, Entity Framework and other mobile technologies such Ionic, PhoneGap, Corodva. Read more...
 
Free Interview Books
 
23 APR
NodeJS Development (offline)

Sat, Sun (10:00 AM-12:00 PM IST)

More Details
16 APR
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun (8:00 AM-10:00 AM IST)

More Details
11 APR
ASP.NET MVC with AngularJS Development (online)

Mon-Fri (7:00 AM-9:00 AM IST)

More Details
2 APR
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun (4:00 PM-6:00 PM IST)

More Details
19 MAR
ASP.NET MVC with AngularJS Development (online)

Mon - Fri     (8:30 PM-10:30 PM IST)

21 FEB
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     (3:00 PM-5:00 PM IST)

6 FEB
NodeJS Development (offline)

Sat, Sun     (10:00 AM-12:00 PM IST)

31 JAN
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     (8:00 AM-10:00 AM IST)

31 JAN
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     (4:00 PM-6:00 PM IST)

4 JAN
.NET Development (offline)

Mon-Fri     (9:00 AM-11:00 AM IST)

BROWSE BY CATEGORY
 
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 
+