Difference between Properties with get; set; and w

2020-03-07 17:23发布

问题:

I do not understand the difference between static properties:

public static int intId;

and get;set; properties:

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

What is the difference between these two? They work like same or different?

回答1:

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.



回答2:

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.



回答3:

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;
        }
    }


回答4:

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.



回答5:

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;
    }
}