Difference between Properties with get; set; and without get; set; [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-01 02:20:12

Your first sample is a field, not a property.

It's a good practice to always make fields private, and wrap them in properties instead.

That way you can add validation on set or override property in descendants(if it is marked as virtual). Also you can add some tricky code in get and set that will be hidden from those who use your class.

The first is not a property. It is a field. It is also a static one.

Even if it were not static, but an instance variable, the two are different and compile to different IL.

In regards to why use simple properties over a public field - properties allow you to encapsulate the implementation behind. They let you change the type internals without changing the interface.

They are the same int variable for the class however the first one since its a static int, it would be accessed from the Class and can be altered and any instance of it would have the same value.

public static int intId;

The second one will just be accessed by instances and its an unique value per instance since its not static, but it can be accessed by anyone since its public.

 public int intId
    {
        get
        {
            return intId;
        }
        set
        {
            intId = value;
        }
    }
Martin Prikryl

The first is static field; it's not even a property.

Static fields have only one value for the application.

While the second is an instance property, which is different for every instance of the class.

It does not make a big difference in this example, if it is a property or field. But in the long term, if you use a property in your interface, you might change it later to have an actual getter and setter that do for example, validations or make the object react somehow on the new value. A field is just a field, you cannot control when and how it is set and react to it.

what is the difference between these two?

Your first code example is a field and your second one is a property.

A field is a class member that its value is assigned on a class instantiating (if it's set on class definition), before the constructor is been called and you don't have any control when setting or getting it:

public static int intId;

A property is a class member, I can describe it as a special "field" which you can control on how the data will be set and get on it,in other words - Encapsulation, it's kind of a function but behaves like a field:

public int intId
    {
        get
        {
            return intId;
        }
        set
        {
            intId = value;
        }
    }

In your example, the int property is using the static int field but you're doing a wrong use of the both:

  1. Your field should be with a private modifier and not static, otherwise it's not make sense of using it because it might be change from external sources.

  2. They both have the same name, change it.

Like that:

private int _intId;

public int IntId
{
    get
    {
        return _intId;
    }
    set
    {
        _intId = value;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!