Read only Automatic Properties in C#

Automatic Properties were a nice addition to C# 3 and I use them all the time. A lot of people may already know this but somehow I only found out this week; you can create a read only automatic property simply by adding the ‘private’ keyword before ‘set’:


public class UserEventArgs : EventArgs
{
    public UserEventArgs(int userID)
    {
        UserID = userID;
    }

    public int UserID { get; private set; }
}

Posted on by Joe in C#

One Response to Read only Automatic Properties in C#

  1. Kirstin

    Great tip.. NERD!

Add a Comment