locked
Difference between Auto - Implemented Properties and normal public member variables RRS feed

  • Question

  • Hi,

    I am new to C#.

    I am confused about how "Auto - Implemented Properties" is used. What is the difference between AIP and a normal public member variables. It seems to be that there are not much difference.

    If I write :

    class MyClass
        {
            public int MyProperty;
        }

    and

        class MyClass
        {
            public int MyProperty{get; set;}
        }

    It seems to me that I can assign value to both MyProperty using:

         MyClass mc = new MyClass();
         mc.MyProperty = 10;

    But I am sure there must be reasons why "Auto - Implemented Properties" is invented and there must be difference between them. Can anyone let me know?

    Regards,
    Kevin

    • Edited by WhereIsMyHome Wednesday, February 11, 2009 1:30 AM typo
    Wednesday, February 11, 2009 1:28 AM

Answers

  • At first they do seem similar. However, there are several important differences:
    1) There is a difference between a field and a property in the type definition. This is particularly important for components using reflection, which is fairly common. e.g., XML serialization only writes out properties, not fields.
    2) For publicly exposed types, properties allow different implementation in the future. Changing a field into a property would require all users of the library to recompile.
    3) Properties work more cleanly with the type system. One example is that interfaces may not have fields, but may have properties. Similarly, properties may be virtual and overridden by derived classes.
    4) Properties support asymmetric accessibility, which provides more flexibility than a readonly field.

            -Steve
    • Marked as answer by Bin-ze Zhao Friday, February 13, 2009 7:20 AM
    Wednesday, February 11, 2009 5:29 AM
  • As Nishant says, the first is a field and the second a property. There is impoant difference about what is going on under the covers in the IL

    for a field, the compiler emits a stfld instruction which means the state gets stamped directly into that location

    for a property the compiler emits a pair of methods (get_MyProperty and set_MyProperty) and when you assign a value to the property the set_ method gets called. This means that you retain control over the processing setting the state of the field. As has been said, an automatic property is a way to get the compiler to write the boiler plate property implementation

    private int _myProperty;
    public int MyProperty
    {
           get{ return _myProperty;}
           set{ _myProperty = value; }
    }

    which means anyone compiled against your code with already be bound to the generated methodds rather than the public field explicitly so should you choose to fully expand the property syntax and, say, do validation you will not break any clients
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    • Marked as answer by Bin-ze Zhao Friday, February 13, 2009 7:20 AM
    Wednesday, February 11, 2009 8:04 AM

All replies

  • WhereIsMyHome said:

    Hi,

    I am new to C#.

    I am confused about how "Auto - Implemented Properties" is used. What is the difference between AIP and a normal public member variables. It seems to be that there are not much difference.

    If I write :

    class MyClass
        {
            public int MyProperty;
        }

    and

        class MyClass
        {
            public int MyProperty{get; set;}
        }

    It seems to me that I can assign value to both MyProperty using:

         MyClass mc = new MyClass();
         mc.MyProperty = 10;

    But I am sure there must be reasons why "Auto - Implemented Properties" is invented and there must be difference between them. Can anyone let me know?

    Regards,
    Kevin


    Your first example is not a property at all. It's a public field.

    The 2nd example is an auto-implemented property (the backing storage is generated by the compiler).

    http://blog.voidnish.com
    Wednesday, February 11, 2009 2:31 AM
    Moderator
  • At first they do seem similar. However, there are several important differences:
    1) There is a difference between a field and a property in the type definition. This is particularly important for components using reflection, which is fairly common. e.g., XML serialization only writes out properties, not fields.
    2) For publicly exposed types, properties allow different implementation in the future. Changing a field into a property would require all users of the library to recompile.
    3) Properties work more cleanly with the type system. One example is that interfaces may not have fields, but may have properties. Similarly, properties may be virtual and overridden by derived classes.
    4) Properties support asymmetric accessibility, which provides more flexibility than a readonly field.

            -Steve
    • Marked as answer by Bin-ze Zhao Friday, February 13, 2009 7:20 AM
    Wednesday, February 11, 2009 5:29 AM
  • As Nishant says, the first is a field and the second a property. There is impoant difference about what is going on under the covers in the IL

    for a field, the compiler emits a stfld instruction which means the state gets stamped directly into that location

    for a property the compiler emits a pair of methods (get_MyProperty and set_MyProperty) and when you assign a value to the property the set_ method gets called. This means that you retain control over the processing setting the state of the field. As has been said, an automatic property is a way to get the compiler to write the boiler plate property implementation

    private int _myProperty;
    public int MyProperty
    {
           get{ return _myProperty;}
           set{ _myProperty = value; }
    }

    which means anyone compiled against your code with already be bound to the generated methodds rather than the public field explicitly so should you choose to fully expand the property syntax and, say, do validation you will not break any clients
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    • Marked as answer by Bin-ze Zhao Friday, February 13, 2009 7:20 AM
    Wednesday, February 11, 2009 8:04 AM
  • Hi all,

    Thanks for your explanation. I now know the difference between them.

    Regards,
    Kevin
    Wednesday, February 11, 2009 10:50 AM