与获取属性之间的差异;组;并获得无;组; [英] Difference between Properties with get; set; and without get; set;

查看:146
本文介绍了与获取属性之间的差异;组;并获得无;组;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白,静态属性的区别:

I do not understand the difference between static properties:

public static int intId;

和获取;设置;属性:

public int intId
{
  get
  {
    return intId;
  }
  set
  {
    intId = value;
  }
}



什么是这两者之间的区别?他们的工作就像相同或不同?

What is the difference between these two? They work like same or different?

推荐答案

您第一个样本是一个领域,而不是财产。

Your first sample is a field, not a property.

这是一个很好的做法,总是让私人领域,并在性能包装他们,而不是。

It's a good practice to always make fields private, and wrap them in properties instead.

这样,你可以在设置中添加验证或后代覆盖属性(如果它被标记为虚拟)。您也可以在获取和设置将从那些谁使用您的类被隐藏添加一些棘手的代码。

That way you can add validation on set or override property in descendants(if it is marked as virtual). Also you can add some tricky code in get and set that will be hidden from those who use your class.

这篇关于与获取属性之间的差异;组;并获得无;组;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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