C#属性和字段 [英] C# Property And Field

查看:75
本文介绍了C#属性和字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我读了很多文章,但是我发现属性和字段之间有什么区别

在此示例1

Hi,
I read lots of article, But I coldnt find what is the differences between Property and field

In this sample 1

class X
{
  public int Number;
}

X Instance = new X();


X.Number(我可以更改其值)

如果我想保护Number字段以防止错误访问,也许我不想设置Instance Only的值,可以更改X的主类,因此我定义了该字段和属性

样本2


X.Number (I can change its value)

If I want to protect Number field to wrong access, maybe I do not want to set value from Instance Only can change main class Of X so I defined that field and property

Sample 2

class X
{
   private int Number;
   public int INumber
   {
       get {return Number;}
   }
}


但我可以按照以下步骤进行操作
样本3


But I can do this as follows
Sample 3

class X
{
   public int Number
   {
       get {return Number;}
   }
}


我无法理解示例2和示例3之间的区别
如果要使用属性,是否必须使用该字段,如果我不必使用它,为什么在单击封装字段时Visual Studio会自动生成此属性.

示例3中的Visual Studio是做什么的?
是否声明了领域背景?是否:)

对于我的英语不好,我深表歉意.

非常感谢.


I couldn''t understand differences between Sample 2 And Sample 3
If I want to use property, Do I must to use field, if I have not to use it why the visual studio auto generate this when I click the encapsulate the field.

What is the visual studio do in sample 3?
Is it declare a field background? Or not :)

I apologize in advance for my bad English.

Thanks a lot.

推荐答案

使用方式相同.现在,想象一下您写了其他东西而不是get { return Number; }:

No difference if you use it the way you do. Now, imagine instead of get { return Number; } you write something else:

internal int MyNumber {
    get {
        int readValue = ComplexCalculation();
        MyHistory.Add(readValue); //remembers all the values ever read during run-time
        return readValue;
    }
}

int ComplexCalculation() { /* too complex to show here... :-) */}
System.Collection.Generic<int> MyHistory = new System.Collection.Generic<int>();
//...



那就是使用属性 getter . 设置器的更典型用法:



That was the use of property getter. More typical use of the setter:

internal int MyNumber {
    get { /*...*/ return Number; }
    set {
        Number = value;
        MyForm.MyLabel.Text = Number; //it invalidates the form and shows new text       
    }
}



因此,这是关于属性的核心思想:类似字段的界面,类似方法的功能,并且可能会有副作用.这些方法实际上仅是getter或setter或两者都使用,但是主体是任意的.同样,与方法一样,该属性可以是 static instance 的一种,即没有或可以访问"this"参数(引用声明类型的实例).我还用internal替换了您的public.到目前为止,我认为public没有任何道理.您确实希望提供尽可能少的访问权限.请阅读以下内容: http://msdn.microsoft.com/zh-我们/library/ba0a1yw2%28v=VS.100%29.aspx [

—SA



So, this is the core idea about properties: interface like that of fields, functionality like that of methods, with possible side effect. The methods are actually only getter or setter or both, but the body is arbitrary. Also, like methods the property can be static or instance one, that is without or with access to "this" parameter (reference to the instance of a declaring type). I also replaced your public with internal. So far, I see no justification for public. You really want to give the minimal possible access. Read about it: http://msdn.microsoft.com/en-us/library/ba0a1yw2%28v=VS.100%29.aspx[^].

Note, I replaced you INumber with MyNumber. Don''t violate good Microsoft naming conventions. First style of naming is reserved for interfaces.

Now, this is a time for you to say: "Aha!". Come on!

—SA


它可能对您有帮助,

字段 [属性 [
It might help you,

Fields[^]

Properties[^]

:)


属性是一个成员,它提供了一种灵活的机制来读取,写入或计算私有字段的值. (MSDN)
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. (MSDN)
public int MyProperty { get; set; }


我可以像字段一样使用它.
我了解属性可以像警卫队或功能上为田野提供
但是它是如何工作的,只有属性没有域,实际上它是域?或字段重载
如果我告诉我错了,我不会写更多.因为也许您有很多工作:)
真的,我很奇怪,我读了土耳其语书籍,但没有找到像我的搜索


I can use it like a field.
I understand that properties provides like a guard or functionally for field
but how it is work, only propery with out field, actually it is a field ? or overloaded field
If I wrong tell me, I wont write a more. Because Maybe you have got lots of work :)
Really I wonder this and I read turkish books but I didnt find like my search


这篇关于C#属性和字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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