简体   繁体   English

为什么在C#getter / setter中使用私有变量?

[英]Why do you use private variables with C# getter/setters?

I see this all the time: 我一直都看到这个:

    private int _myint;

    public int MyInt
    {
        get
        {
            return _myint;
        }
        set
        {
            _myint = value;
        }
    }

To me this seems identical to: 对我而言,这似乎与:

    public int MyInt{ get; set; }

So why does everyone do the former... WHY THE PRIVATE VAR AT ALL?! 那么为什么每个人都做前者...为什么私人VAR?

First of all, this is new to C# 3.0, it wasn't around before that. 首先,这是C#3.0的新功能,它在此之前并不存在。 Second, if you want to add any custom logic to your getter and setters, you have no choice. 其次,如果你想为你的getter和setter添加任何自定义逻辑,你别无选择。 So yes, in your example where there's no custom logic, it's the same thing (in fact the compiler generates that for you behind the scenes) but if you want to raise an event for example, or anything like that, you have to be explicit about it. 所以,是的,在你没有自定义逻辑的例子中,它是同样的事情(实际上编译器在幕后为你生成)但是如果你想举例,或者类似的东西,你必须明确关于它。

I'd like to see 我想看

public int MyInt{ get; private set; }

more, ;) 更多, ;)

but @BFree nailed it 但@BFree钉了它

First of all, the syntax you used is new. 首先,您使用的语法是新的。 It didn't exist in earlier versions of C#. 它在早期版本的C#中不存在。

Second of all, you need the private variable if you're going to have a default value, or lazy-load the result. 其次,如果您要使用默认值,或者延迟加载结果,则需要私有变量。

An example to expand on what @BFree is saying: 扩展@BFree所说内容的一个例子:

private int _Whatever; 
public int Whatever
{
    get {return _Whatever;}
    set 
    {
        if(value != _Whatever)
        {  
          // It changed do something here...
          // maybe fire an event... whatever

        } 

        _Whatever = value;

    }
}

Do you like someone you don't know messing with your private parts? 你喜欢一个你不知道弄乱你私处的人吗? I hope not. 我希望不是。 This is a way to provide what is essentially a proxy for something you own but don't want to cede control over. 这是一种提供基本上代理你自己的东西但不想放弃控制权的方法。 If you decide you want to validate that int's are positive, you can start doing that if you code as shown. 如果您决定要验证int是否为正,那么如果您按照所示进行编码,则可以开始执行此操作。

C# makes this transparent now with automatic properties. C#现在使用自动属性使其透明。

Before C# 3, that was the only way to do it. 在C#3之前,这是唯一的方法。 Implicitly typed properties were not yet available. 隐式类型的属性尚不可用。 People still wanted to abstract away the private member. 人们仍然希望抽象出私人成员。 If developers are still doing this in C# 3, they either aren't aware of the new changes or need to provide custom get/set logic. 如果开发人员仍然在C#3中这样做,他们要么不知道新的更改,要么需要提供自定义的get / set逻辑。

That's the old way to do it. 这是旧方法。 The way you prefer ("automatic properties") it a relatively new construct in the language. 你喜欢的方式(“自动属性”)它是一种相对较新的语言结构。 A few years ago, we always had to use private variables. 几年前,我们总是不得不使用私有变量。

There may be other reasons to use private variables as well, though not in the simple example you provide. 使用私有变量可能还有其他原因,但不是在您提供的简单示例中。 If, for example, you needed to intialize the property with a default value, you can't really do that cleanly with automatic properties; 例如,如果您需要使用默认值初始化该属性,则无法使用自动属性进行干净利落; instead you need to initialize in the constructors. 相反,你需要在构造函数中初始化。

public int MyInt{ get; set; }

was a feature added in C# 3.0 called Automatic Properties. 是C#3.0中添加的一项名为“自动属性”的功能。 C# 2.0 does not support this and requires the private variable with the explicit getters and setters. C#2.0不支持这一点,并且要求私有变量具有显式的getter和setter。 Therefore, a lot of older code or backwards compatible code will use the explicit getters and setters. 因此,许多旧代码或向后兼容代码将使用显式getter和setter。

如果您使用反射器等工具查看编译器生成的输出,则会添加私有字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么C#编译器不允许接口中的私有属性设置器? - Why C# compiler does not allows private property setters in interfaces? 为什么使用私有属性setter,因为成员变量可以直接访问? - Why use private property setters since member variables can be accessed directly? 如何在数组中使用Getter setter - How to use Getter setters in array C#私有变量和Java私有变量getter和setter-区别? - C# private variable & java private variable getter & setter - Difference? 您经常看到滥用C#速记吸气剂/孵化器? - How often do you see abuse of C# shorthand getters/setters? protobuf-net没有使用私有setter序列化C#属性 - protobuf-net not serializing C# properties with private setters C#-属性如何使用getter / setter快捷方式访问私有字段 - C# - How do properties access a private field using getter/setter short hand C#如何为集合创建公共getter和setter以及私有方法? - C# How to make public getters and setters and private methods for a collection? 私有静态和实例变量是否在C#中继承?为什么? - Are private static and instance variables inherited in C# and why? 在C#中,首先触发什么是私有成员变量或构造函数,为什么? - In C#, what fires first, private member variables or constructors, and why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM