Properties

The default access modifier is private
A property is similar to a field except that you do not have to use additional methods to manage your private fields.
What can a property do that a field can't
A property that has both a set and get component is considered read-write
they can validate data before it is actually changed
they can take action when data is changed
they can change the values of other variables
they can raise exceptions


Default


Auto Implemented

An auto implemented property is equivalent to a property value that is stored in a private field
This is also sometimes referred to as Automatic Properties

public string Property_Name { get; set; } 

replaces

private string m_sName; 

public string Property_Name
{
   get { return m_sName; }
   set { m_sName = value; }
}

You can also create a read-only auto-implemented property by inserting the private keyword.

public string Property_Name { get; private set; } 

You cannot use auto implemented in the following cases:
add extra code to the Get and Set procedure
you don't want public accessors for get and set
properties that are writeonly or readonly
you want the property to accept parameters


VB allows a property to accept parameters, C# does not.



Auto Initializing

Added in .NET 4.6
This allows assignment of properties directly within their declaration
Before you could only initialise the value of a property in the classes constructor.


class MyClass 
{
   public string Property_One { get; } = System.Environment.Username;
}

Instead of

class MyClass 
{
   public string Property_One()
   {
      Property_One - System.Environment.Username;
   }
   public string Property_One { get; }
}

Write Only Properties

When the value of a property can only be changed outside the class it is called a write-only property.
This is rarely seen or used.
There may also be times when you want the client to change a value but not to retrieve it.
This rarely makes sense though


Removing Get

To create a read-only field you can exclude the get altogether.

public class Animal 
{
   private int _Age;

   public int Age
   {
      set { this._Age = value; }
   }
}

Private Get

Alternatively if you still needed to set the value inside the class then you can change it to private.

public class Animal 
{
   private int _Age;

   public int Age
   {
      private get { return this._Age; }
      set { this._Age = value; }
   }
}

Indexed Properties

An Indexed Property or Indexer Property creates an illusion that the class itself works like an array.
Indexers provide a way to access elements that encapsulate a list or dictionary of values.
They are similar to properties but they are accessed with an index, not a property name
The string class has a built-in indexer that lets you access each of its 'char' values using an 'int' index

string myText = "Better"; 
Console.WriteLine(myText[0]); // 'B'


An indexer allows instances of a class to be indexed like an array.
C# has always had the notion of an indexer that you can add to a class to overload the [ ] operator.
This sense of indexer is called the default indexer since it is not given a name and calling it requires no name.
Indexers can be virtual
Indexers cannot be static
This lets you use array syntax to access the class
An indexer is accessed using the [] operator
They should only be used when an array-like abstraction makes sense


An indexer is a member that enables an object to be indexed in the same way as an array


class Something 
{
   public string this[int number]
   {
      get { return _values[number]; }
      set { _values[number] = value; }
   }
}

This allows you to use the values

class Something 
{
   private string[] _values = new string[100];

   public string this[string name]
   {
      get
      {
         foreach (string str in _values)
         {
             if (str == name) { return (str); }
         }
      }
      set
      {
         for (int n=0; n < _values.Length; n++)
         {
            if (_values[n] == name) { _values[n] = value; return; }
          }
      }
   }
}

C# does not allow you to create 'named' indexed properties.
There are no plans to include the ability to declare your own properties that take parameters.
Instead of using an indexed property you should use a type with an indexer which is returned by a property.
The property is there to get access to an object and belongs to the parent object.
The indexer is used to enumerate through an object and belongs to the returned object.


Anonymous Types

Added in .NET 3.5
This provides a quick way to create a single read-only object.
Provides a way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first
This is achieved by using implicitly typed local variables.
The name/value pairs declare properties with initial values.
Although anonymous types are classes they do not implement the IDisposable interface

var myType = new { Property_Name="Simon", Property_Age=30 }; 

instead of

public class ReadOnly_Object { 
   public string Property_Name { get; }
   public int Property_Age { get; }
}
var myType = new ReadOnly_Object();
myType.Property_Name = "Simon";
myType.Property_Age = 30;

If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them.
It is derived from System.Object class and it is also a sealed class.



© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext