Auto Property In C# 6.O

Discussion in 'C#' started by Sagar Jaybhay, Nov 12, 2019.

  1. Sagar Jaybhay

    Sagar Jaybhay New Member

    Joined:
    Jan 28, 2019
    Messages:
    29
    Likes Received:
    17
    Trophy Points:
    3
    Gender:
    Male
    Occupation:
    Sr. Software Developer
    Location:
    Pune
    Home Page:
    https://sagarjaybhay.net
    The auto property came into existence at C# version 3.0. It is used for code simplification and by using this your code becomes more readable


    One of the great features of c# is you can declare a property.

    Code:
    public class Student
        {
            public Guid Id { get; }=Guid.NewGuid();
        }
    
    In this code, you can initialize the property at the time of declaration. By using this feature you don’t have a need to write a constructor and initialize the property in that constructor.

    In the previous version of c# we not able to write a property only with get.

    upload_2019-11-12_21-21-32.png

    Above image shows it will give an error in c# version 5.0 and below but if you declared read-only property in c# it will not throw an error. Below is the image of version 6.0

    upload_2019-11-12_21-21-52.png

    Why auto-property initialization needed?
    Suppose I have a class like below

    Code:
    public class Student
        {
           public string Fname { get; private set; }
        }
    
    I am writing this code because I want my property read-only and I don’t want to set value from outside. But somehow by mistake, I implement one method and changed the value of that Fname field like below which is completely valid.

    Code:
    public class Student
        {
           public string Fname { get; private set; }
    
           public void ByMistake()
           {
               Fname = "sagar";
           }
        }
    
    To avoid this in C# 6.0 introduced auto property initialization for read-only field and our code become


    Code:
    public class Student
        {
            public string Fname { get; } = "Mr.";
    
        }
    
    By using this our field becomes Immutable.


    upload_2019-11-12_21-24-6.png


    And if try to change in the class itself it gives the compile-time error.



    Code:
      public class Student
        {
            public string Fname { get; } = "Mr.";
    
            public void tryToChange()
            {
                Fname = "sagar";
            }
        }
    
    Check below code by that you will able to change read-only property value but it is in the constructor.


    Code:
    public class Student
        {
            public string Fname { get; } = "Mr.";
    
            public Student()
            {
                Fname = "sagar";
            }
    
            public void tryToChange()
            {
                Fname = "sagar";
            }
        }
    
    upload_2019-11-12_21-25-16.png
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice