编程术语 - 字段,成员,属性(C#)

Pet*_*etr 21 c# syntax terminology

我试图找到这个术语的含义,但特别是由于语言障碍,我无法理解它们的用途.我假设"field"在类中是变量(对象也是?),而"property"只是一个返回特定值且不能包含方法等的对象.通过"member"我理解在类级别声明的任何对象.但这些只是我基于注释代码示例的假设,其中一些细心的程序员使用"属性区域"等.如果有人能够向我解释,我真的很感激.

Thi*_*ise 42

在C#中:

fields:这些是在类级别声明的变量.

public class SomeClass
{
    private int someInteger; // This is a field
    public double someDouble; // This is another field
    protected StringBuidler stringBuidler; // Still another field
}
Run Code Online (Sandbox Code Playgroud)

properties:通常用作类的私有字段的访问器,它们可以提供get和set方法,围绕字段操作包装一些逻辑.

public class SomeClass
{
    private StringBuilder stringBuilder;

    // Property declaration
    public StringBuilder StringBuilder
    {
        get 
        { 
            if(this.stringBuilder == null)
                this.stringBuilder = new StringBuidler();

            return this.stringBuilder;
        }
        set
        {
            if(this.stringBuilder == null)
                this.stringbuilder = value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

成员:包括类的字段,属性,方法和事件.