C# read or write only property

Description

We can create a read-only property by simply omitting the set accessor from the property definition.

Syntax

To make Name a read-only property, you would do the following:


private string name; 
              //from  w  w  w . ja v a  2  s. c  o  m
public string Name  
{ 
   get 
   { 
      return Name; 
   }  
} 

Read-only calculated properties

A property typically has a dedicated backing field to store the underlying data. However, a property can be computed from other data.

For example:


decimal currentPrice, sharesOwned; //  w  w  w.  j ava  2  s.c o  m

public decimal Worth 
{ 
  get { return currentPrice * sharesOwned; } 
} 




















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor