Should I use public properties and private fields

2019-01-01 13:37发布

In much of the code I have seen (on SO, thecodeproject.com and I tend to do this in my own code), I have seen public properties being created for every single private field that a class contains, even if they are the most basic type of get; set; like:

private int myInt;
public int MyInt 
{
     get { return myInt; }
     set { myInt = value }
}

My question is: how does this differ from:

public int MyInt;

and if we should use properties instead of public fields why should we use them in this specific case? (I am not talking about more complex examples where the getters and setters actually do something special or there is only one get or set (read/write only) rather than just returning/setting a value of a private field). It does not seem to add any extra encapsulation, only give a nice icon in IntelliSense and be placed in a special section in class diagrams!

13条回答
闭嘴吧你
2楼-- · 2019-01-01 14:03

Setting a value into a private field only changes that field,but making them in property you can handle another arguments for example,you can call a method after setting a value

private string _email;
public string Email
{
    get
    {
        return this._email;
    }
    set
    {
        this._email = value;
        ReplaceList(); //**
    }
}




查看更多
何处买醉
3楼-- · 2019-01-01 14:04

There are many reasons why.

Mainly:

  • You can do some other functions when the variable is set
  • You can prevent setting and provide only get
  • Some 'things' only work on properties (DataBinding, for example)
  • You can hide the implementation of the property [perhaps it is a ViewState variable, in ASP.NET).
查看更多
梦该遗忘
4楼-- · 2019-01-01 14:06

When you create private field name and a simple public property Name that actually gets and sets the name field value

public string Name
{
   get { return name; }
}

and you use this property everywhere outside your class and some day you decide that the Name property of this class will actually refer to the lastName field (or that you want to return a string "My name: "+name), you simply change the code inside the property:

public string Name
{
   get { return lastName; //return "My name: "+name; }
}

If you were using public field name everywhere in the outside code then you would have to change name to lastName everywhere you used it.

查看更多
看淡一切
5楼-- · 2019-01-01 14:08

It... depends?

I always use getters & setters, since they created this shortcut:

public int Foo { get; set; }

At compile time it is translated. Now you can't get fancy with it, but it is there, and if you need to get fancy you just spell it out later.

However public, private, protected... it's all a matter of who you want to be able to tweak the data. We use inheritance a lot and this is a very common method for us, so that only chidren can edit certain properties.

protected _foo;  
public Foo  
{  
    get { return _foo; }
} //lack of set intentional.
查看更多
十年一品温如言
6楼-- · 2019-01-01 14:09

See this article http://blog.codinghorror.com/properties-vs-public-variables/

Specifically

  • Reflection works differently on variables vs. properties, so if you rely on reflection, it's easier to use all properties.
  • You can't databind against a variable.
  • Changing a variable to a property is a breaking change.
查看更多
查无此人
7楼-- · 2019-01-01 14:10

The point is - what if further down the line you want to make sure that every time myInt is referenced something special happens (a log file is written to, it's changed to 42 etc)? You can't do that without getters and setters. Sometimes it's wise to program for what you might need, not what you need right now.

查看更多
登录 后发表回答