为什么要使用公共变量? [英] Why use public variables?

查看:126
本文介绍了为什么要使用公共变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

变量,方法和类可以接收各种安全级别。
从我的C#的经验,有:

Variables, methods and classes can receive various security levels. From my C# experience, there is:

公开结果
内部结果
保护的结果
受保护的内部结果
私人

public
internal
protected
protected internal
private

现在,我理解制作使用方法和类的私人或内部或受保护的,但对于变量?即使我做一个私有变量,我可以用一个属性来从不同的类中调用它。

Now, I understand the use of making methods and classes private, or internal or protected, but what about variables? Even if I make a variable private, I can use a Property to call it from a different class.

我一直以为属性是最好的做法。所以,如果我可以使用,我并不需要直接通过一个实例调用变量。

I've always been thought that Properties are best-practice. So if I can use that, I don't need to call variables directly via an Instance.

是否有任何理由不使一个变量私有?

Is there any reason not to make a variable private?

编辑:我看到有些人在谈论性能,好像他们只不过是的荣耀公共变量

I see some people talking about Properties as if they are nothing more than Glorified public variables

快速提醒:公共变量只返回自己的价值。具有属性,你可以做更多。例如:

Quick reminder: public variables return just their value. With properties, you can do more. For instance:

public int AmountOfBooks{
  get {
    //code to check certain conditions
    //maybe trigger an event while we're at it.
    //and a few conditionals.
    return this.amountOfBooks;
  }

  set {
    //a few conditionals
    //maybe trigger an event
    this.amountOfBooks = value;
    //and I can do even more... I think, never tried this.
  }
}



你们谁读过我的个人资料知道我M学生。使用属性为荣耀公共变量是我看到很多同学做的。告诉它们时,他们能够做到这一点,最常见的反应是:是允许的

Those of you who've read my profile know I'm a student. Using properties as "glorified public variables" is something I see a lot of fellow students do. The most common response when telling them they can do this is: "Is that allowed?"

推荐答案

当然,也有情况这是有意义的有一个公共领域。例如,如果您要创建一个单独结构的互操作性与现有的非托管的Win32 API。

Sure, there are situations in which it makes sense to have a public field. For example, if you are creating a struct solely for interoperability with an existing unmanaged win32 API.

但是,如果你正在建设一个常规的管理对象模型,最好的做法是你的对象的属性,性能建模,使用领域作为私下实现机制。公开曝光功能

But if you are building a regular managed object model, the best practice is to model the properties of your objects as properties, and use fields as the mechanism that privately implements the publically exposed functionality.

更新:我要借此机会指出,我们试图让它在C#3.0很容易写一个简单的访问后盾属性店:

UPDATE: I'll take this opportunity to note that we have tried to make it very easy in C# 3.0 to write a property which simply accesses a backing store:

public int Foo { get; set; }



是完全一样的。

is exactly the same as

private int _Foo;
public int Foo { get { return _Foo; } set { _Foo = value; } }



但很多更短,更易于阅读。当然,如果你需要在以后将它扩大到长形式后备存储,这样做是不是一个重大更改

but a lot shorter and easier to read. And of course if you need to expand it into the long form with a backing store later, doing so is not a breaking change.

您也可以做到这一点:

public int Foo { get; private set; }

如果你想是只读的类/结构之外的代码的属性。

if you want to make a property that is read-only to code outside the class/struct.

这篇关于为什么要使用公共变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆