.NET Properties - Use Private Set or ReadOnly Property?

  • In what situation should I use a Private Set on a property versus making it a ReadOnly property? Take into consideration the two very simplistic examples below.

    First example:

    Public Class Person
    
        Private _name As String
    
        Public Property Name As String
            Get
                Return _name
            End Get
            Private Set(ByVal value As String)
                _name = value
            End Set
        End Property
    
        Public Sub WorkOnName()
    
            Dim txtInfo As TextInfo = _
                Threading.Thread.CurrentThread.CurrentCulture.TextInfo
    
            Me.Name = txtInfo.ToTitleCase(Me.Name)
    
        End Sub
    
    End Class
    
    // ----------
    
    public class Person
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            private set { _name = value; }
        }
    
        public void WorkOnName()
        {
            TextInfo txtInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;
            this.Name = txtInfo.ToTitleCase(this.Name);
        }
    }
    

    Second example:

    Public Class AnotherPerson
    
        Private _name As String
    
        Public ReadOnly Property Name As String
            Get
                Return _name
            End Get
        End Property
    
        Public Sub WorkOnName()
    
            Dim txtInfo As TextInfo = _
                Threading.Thread.CurrentThread.CurrentCulture.TextInfo
    
            _name = txtInfo.ToTitleCase(_name)
    
        End Sub
    
    End Class
    
    // ---------------
    
    public class AnotherPerson
    {
        private string _name;
        public string Name
        {
            get { return _name; }
        }
    
        public void WorkOnName()
        {
            TextInfo txtInfo = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;
            _name = txtInfo.ToTitleCase(_name);
        }
    }
    

    They both yield the same results. Is this a situation where there's no right and wrong, and it's just a matter of preference?

    `public string Name { get; protected set; }` through inheritance.

  • Adam Lear

    Adam Lear Correct answer

    9 years ago

    There are a couple reasons to use private set.

    1) If you are not using a backing field at all and want a read-only automatic property:

    public string Name { get; private set; }   
    
    public void WorkOnName()
    {
        TextInfo txtInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
        Name = txtInfo.ToTitleCase(Name);
    }  
    

    2) If you want to do extra work when you modify the variable inside your class and want to capture that in a single location:

    private string _name = string.Empty;
    public string Name 
    { 
        get { return _name; }
        private set 
        {
            TextInfo txtInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
            _name = txtInfo.ToTitleCase(value);
        }
    }
    

    In general, though, it's a matter of personal preference. Far as I know, there are no performance reasons to use one over the other.

    Just adding this because the question also has a vb.net tag, but in vb.net you need to specify a backer if you use private on either either get or set. So in vb.net it's actually less work to make the property readonly I think.

    I never knew about that `private set`. :-)

    An update for those reading this answer in 2016. C# 6.0 has introduced readonly auto-properties, which allow you to have a readonly property without a backing field: `public string Name { get; }`. If you don't want a mutable property, that's the preferred syntax now.

    One very good reason not to use `private set` is that it's not as immutable as we like to pretend it is. If you want to implement a truly immutable class, read only is a must.

    May be a performance reason to NOT use readonly. Seems to cause unnecessary copying of structs when accessing methods of a readonly struct field. https://codeblog.jonskeet.uk/2014/07/16/micro-optimization-the-surprising-inefficiency-of-readonly-fields/

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM

Tags used