我是否需要使用{搞定;组; }以获取和设置时没有特殊动作C#领域 [英] Do I need to use { get; set; } with c# fields that have no special actions when getting and setting

查看:98
本文介绍了我是否需要使用{搞定;组; }以获取和设置时没有特殊动作C#领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编码类是这样的:

I have been coding classes like this:

public class ReportViewModel
    {
        public string Status;
        public string DataSource;
        public String DataStore { get; set; }
        public PageMeta PageMeta { get; set; }
        public ICollection<Question> List { get; set; } 
    } 

请注意,大部分字段使用{搞定;组; }除了前两个我让Visual Studio中加我。

Note that most of the fields use { get; set; } except the first two which I let Visual Studio add for me.

我想知道是我真的需要使用{搞定;组; }。这在我看来,VS2010不会自动添加此所以做我需要它吗?

What I am wondering is do I really need to use { get; set; }. It seems to me that VS2010 does not automatically add this so do I need it?

推荐答案

{得到;组; } 表示autoimplemented属性。在.NET之间存在性能并的字段。一般情况下字段应该是私有的。它们被用于一些具体实施,并应在大多数情况下,是内部的类。另一方面属性用于封装暴露于消费者行为

{ get; set; } indicate autoimplemented properties. In .NET there is a difference between properties and fields. Normally fields should be private. They are used for some specific implementation and should in most cases be internal to the class. Properties on the other hand are used to encapsulate behavior that is exposed to the consumers.

这篇关于我是否需要使用{搞定;组; }以获取和设置时没有特殊动作C#领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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