C# - init vs private set vs get only property

Shervan360 1,661 Reputation points
2022-05-30T12:10:43.527+00:00

Hello,

What are the differences between init vs private set vs get only property in C#?

Thank you

    class Book
    {
        public string PrivateTitle { get; private set; }
        public string getOnlyTitle { get; }
        public string initTitle { get; init; }
    }
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-05-30T12:15:12.96+00:00

    Best to read Microsoft Learn on init then read the following for private vs init.

    For all examples

    public enum Gender  
    {  
        Female,  
        Male,  
        Other  
    }  
    

    Here is a record example which is immutable except for the primary key.

    public record PersonRecord   
    {  
        public int Id { get; set; }  
        public string FirstName { get; init; }  
        public string MiddleName { get; init; }  
        public string LastName { get; init; }  
        public Gender Gender { get; init; }  
        public DateTime? Modified { get; init; }  
    }  
    

    Usage

    PersonRecord personRecord = new ()  
    {  
        Id = 1,   
        FirstName = "Karen",   
        MiddleName = "",   
        LastName = "Payne",   
        Gender = Gender.Female,   
        Modified = DateTime.Now  
    };  
    

    Or make one property immutable

    public class PersonClass  
    {  
        public int Id { get; set; }  
        public string FirstName { get; set; }  
        public string MiddleName { get; set; }  
        public string LastName { get; set; }  
        public Gender Gender { get; init; }  
        public DateTime? Modified { get; set; }  
    }  
    

    Usage

    PersonClass personClass = new()  
    {  
        Id = 1,  
        FirstName = "Karen",  
        MiddleName = "",  
        LastName = "Payne",  
        Gender = Gender.Female,  
        Modified = DateTime.Now  
    };  
    

    Tweak Gender to private set.

    public class PersonClass  
    {  
        public int Id { get; set; }  
        public string FirstName { get; set; }  
        public string MiddleName { get; set; }  
        public string LastName { get; set; }  
        public Gender Gender { get; private set; }  
        public DateTime? Modified { get; set; }  
    }  
    

    206772-privateerror.png

    Classic get only

    public class PersonClass  
    {  
        public int Id { get; set; }  
        public string FirstName { get; set; }  
        public string MiddleName { get; set; }  
        public string LastName { get; set; }  
        public Gender Gender { get; init; }  
        public DateTime? Modified { get; set; }  
      
        public string FullName  
        {  
            get  
            {  
                return $"{FirstName} {MiddleName} {LastName}";  
            }  
        }  
      
    }  
    

    Expression body member get

    public class PersonClass  
    {  
        public int Id { get; set; }  
        public string FirstName { get; set; }  
        public string MiddleName { get; set; }  
        public string LastName { get; set; }  
        public Gender Gender { get; init; }  
        public DateTime? Modified { get; set; }  
        public string FullName => $"{FirstName} {MiddleName} {LastName}";  
    }  
    

    And for completness

    public record Person1(string FirstName, string MiddleName, string LastName);  
    

    Used (read-only)

    Person1 person1 = new("Karen", "","Payne");  
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.