Read only Automatic Properties in C#

1

Posted by Joe | Posted in C# | Posted on 17-11-2009

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; }
}

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Comments (1)

Great tip.. NERD!

Write a comment