Cannot Assign to Read Only Property '0' of Object

Introduction

In this article, I am going to explain Init-only setter holding which has been introduced in C# 9.0.

Each C# release has its own features. In C# 9.0, many new wonderful features have been added and here nosotros are going to discuss important feature Init-only setter belongings.

Earlier we showtime, nosotros should have basic agreement of immutable and mutable in c#.

What'south meaning of Mutable and Immutable in English?

Answers are:

  • Mutable – Can change
  • Immutable – Cannot Change

In simple words we can say, immutable means object cannot be changed once it is created. If you want to alter, you have to create new object/ assign new memory. Example of immutable type is string in C#.

What is Init-only setter belongings?

Earlier nosotros starting time with Init-simply property, we have to understand the traditional way of working (properties).

Let's get-go with an case,

Suppose, nosotros take Fellow member class which has iii properties,

  1. ID - Fellow member unique ID
  2. Name - Member Proper noun
  3. Address - Member Address
                    public class Fellow member   {       public int Id { get; set; }       public string Proper noun { get; gear up; }       public string Address { go; ready; }   }              

Now create object of Fellow member class and assign some values,

            using Organisation;      namespace C_9._0   {       class Program       {           static void Main(string[] args)           {               Fellow member memberObj = new Member();               memberObj.Id= 1;               memberObj.Proper noun="Kirtesh Shah";               memberObj.Accost="Vadodara";                  Panel.WriteLine("****************START - Fellow member Details***********");                  Panel.WriteLine("ID : -" + memberObj.Id);               Console.WriteLine("Name :- " + memberObj.Name);               Console.WriteLine("Address:- " + memberObj.Address);               Console.ReadLine();                  Console.WriteLine("****************Stop - Fellow member Details***********");           }       }   }                      

All looks skilful right? But what happens if someone inverse Fellow member  ID? ID is unique and it should not be editable, which means information technology should be immutable, not mutable. That'southward the problem with the traditional way, properties are mutable.

If nosotros desire to make backdrop immutable in the traditional way, we have to laissez passer values in constructor like the below lawmaking,

            public class Member   {       public Member(int memberId)       {           Id = memberId;       }       public int Id { get; }       public string Name { get; set; }       public cord Address { get; gear up; }   }                      

ID belongings is immutable now.

Let'due south see the beneath lawmaking:

            using System; namespace C_9._0 {     class Program {         static void Main(cord[] args) {             Member memberObj = new Member(1);             memberObj.Name = "Kirtesh Shah";             memberObj.Accost = "Vadodara";             Console.WriteLine("****************Commencement - Fellow member Details***********");             Console.WriteLine("ID : " + memberObj.Id);             Console.WriteLine("Name : " + memberObj.Name);             Console.WriteLine("Address: " + memberObj.Address);             Console.ReadLine();             Console.WriteLine("****************Terminate - Member Details***********");         }     } }          

Output of to a higher place code would be,

C# 9.0 - Introduction To Init-Only Property

Now ID property is Immutable and cannot be inverse once object is created. To brand mutable property into immutable, nosotros have to create a constructor.

In C# 9.0, we tin achieve the same matter using Init-merely property.

            public class Fellow member {     public int Id {         go;         init;     } // set is replaced with init     public cord Name {         become;         set;     }     public string Address {         get;         prepare;     } }  using Arrangement; namespace C_9._0 {     class Program {         static void Main(string[] args) {             Fellow member memberObj = new Member {                 Id = 1             };             memberObj.Name = "Kirtesh Shah";             memberObj.Address = "Vadodara";             Console.WriteLine("****************Starting time - Member Details***********");             Console.WriteLine("ID : " + memberObj.Id);             Console.WriteLine("Name : " + memberObj.Name);             Console.WriteLine("Accost: " + memberObj.Address);             Console.ReadLine();             Console.WriteLine("****************END - Member Details***********");         }     } }          

We got the aforementioned output as in the previous code

C# 9.0 - Introduction To Init-Only Property

It is the power of Init-only property. What will we do, if nosotros want to do some validation like ID should not exist zippo or cord? Init-only holding has the adequacy to fix read only property to implement validations.

Suppose we have Member form like beneath,

                public class Member   {       public int Id { get; init; }       public cord Proper noun { get; init; }       public string Address { get; init; }   }              

Now nosotros will endeavor to create a Member class object similar below,

            using System;      namespace C_9._0   {       class Program       {           static void Chief(string[] args)           {               Member memberObj = new Member             {                    Id =i              };              Console.WriteLine("****************START - Member Details***********");                  Console.WriteLine("ID : " + memberObj.Id);               Panel.WriteLine("Proper noun : " + memberObj.Name);               Console.WriteLine("Address: " + memberObj.Address);               Console.ReadLine();                  Panel.WriteLine("****************Terminate - Member Details***********");           }       }   }          

Init-only properties can or cannot be set as per your requirement. As you discover in the higher up code, but ID property is set and name and address backdrop are not ready. Please note that if whatever property is not set up at the fourth dimension of object cosmos that belongings cannot be set up.

I promise you enjoyed this article.

archibaldasaints.blogspot.com

Source: https://www.c-sharpcorner.com/article/c-9-0-introductions-to-init-only-properties/

0 Response to "Cannot Assign to Read Only Property '0' of Object"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel