none
Difference of field and property RRS feed

  • Question

  • Hi teams,

    I am new to programming and am learning reflection now.

    Usually, we will write a class like this:

        class MyClass
        {
            private int id;
            private string name;
            public int ID
            {
                get { return this.id; }
                set { this.id = value; }
            }
            public string Name
            {
                get { return this.name; }
                set { this.name = value; }
            }
        }

    Then ,when i try to get field, it goes like this:

    Int32 id
    System.String name
    


    But if i only write property,

        class Class2
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }

    I can still get two fields, although looks strange:

    Int32 <ID>k__BackingField
    System.String <Name>k__BackingField

    So, my question is:

    Since I write fields or not, there will be fields generated, so why should I write the fields myself? What is the significance of the field? 

    Thanks in advance.

    Robert


    Monday, January 13, 2020 2:32 PM

Answers

  • Hello,

    Here is a good page to explain differences rather than me writing up something which has already been covered.

    https://www.jeremymorgan.com/blog/programming/properties-vs-fields/

    Note, on auto properties or expression body members (readonly), there is always a backing field as you have learned. You write fields yourself when you need to do some work in a fully implemented property, otherwise no need.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by CoolDadTxModerator Monday, January 13, 2020 6:05 PM
    • Marked as answer by robert-y Tuesday, January 14, 2020 11:40 AM
    Monday, January 13, 2020 3:19 PM
    Moderator
  • In the first versions of C# you always had to write manually the backing field in order to save the internal value of the property. This became so common when writing code that in a later version of C# (I think it was version 3.0) they added an option to the compiler so that you could type an abbreviated version of the property and the compiler would generate the backing field automatically.

    So there is no real difference in the binaries that are generated: both if you write an automatic property or if you write manually the full code for the property, you will have a backing field. The difference is that in an automatic property the backing field has a weird name chosen by the compiler, but when you write the field yourself you can give it a readable name.

    • Marked as answer by robert-y Tuesday, January 14, 2020 11:40 AM
    Monday, January 13, 2020 3:22 PM
    Moderator
  • Think about you writing a data container class that has 100 public properties.

    Are you going to sit there and write 100 private backing variables for 100 public properties? Or will you write 100 auto properties for the class and let the compiler make the private backing variables? 

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    There are plenty of articles on your subject using Bing or Google.

    https://www.jeremymorgan.com/blog/programming/properties-vs-fields/

    • Marked as answer by robert-y Tuesday, January 14, 2020 11:40 AM
    Monday, January 13, 2020 6:04 PM

All replies

  • Hello,

    Here is a good page to explain differences rather than me writing up something which has already been covered.

    https://www.jeremymorgan.com/blog/programming/properties-vs-fields/

    Note, on auto properties or expression body members (readonly), there is always a backing field as you have learned. You write fields yourself when you need to do some work in a fully implemented property, otherwise no need.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by CoolDadTxModerator Monday, January 13, 2020 6:05 PM
    • Marked as answer by robert-y Tuesday, January 14, 2020 11:40 AM
    Monday, January 13, 2020 3:19 PM
    Moderator
  • In the first versions of C# you always had to write manually the backing field in order to save the internal value of the property. This became so common when writing code that in a later version of C# (I think it was version 3.0) they added an option to the compiler so that you could type an abbreviated version of the property and the compiler would generate the backing field automatically.

    So there is no real difference in the binaries that are generated: both if you write an automatic property or if you write manually the full code for the property, you will have a backing field. The difference is that in an automatic property the backing field has a weird name chosen by the compiler, but when you write the field yourself you can give it a readable name.

    • Marked as answer by robert-y Tuesday, January 14, 2020 11:40 AM
    Monday, January 13, 2020 3:22 PM
    Moderator
  • Think about you writing a data container class that has 100 public properties.

    Are you going to sit there and write 100 private backing variables for 100 public properties? Or will you write 100 auto properties for the class and let the compiler make the private backing variables? 

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    There are plenty of articles on your subject using Bing or Google.

    https://www.jeremymorgan.com/blog/programming/properties-vs-fields/

    • Marked as answer by robert-y Tuesday, January 14, 2020 11:40 AM
    Monday, January 13, 2020 6:04 PM
  • Thank you all very much. :-)

    Regards.

    Tuesday, January 14, 2020 11:41 AM