自动属性与简单变量 [英] Automatic properties vs simple varible

查看:93
本文介绍了自动属性与简单变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

自动属性和变量之间有什么区别?

What is differences between automatic properties and variable?

我读了这个过时的话:

在C#代码库中看不到自动生成的私人支持字段的名称.看到它的唯一方法是使用诸如ildasm.exe之类的工具.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _05_static
{
    class Program
    {

        public int number { get; set; }
        public int num;

        static void Main(string[] args)
        {
           
        
    }
}
}

推荐答案

属性公开了一个后备字段.出于封装原因,您应该使用属性来公开它(而不是公开该字段(变量)本身).请参考以下链接以获取更多信息: http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c

A property exposes a backing field. Instead of exposing the field (variable) itself, you should expose it using a property for encapsulation reasons. Please refer to the following link for more information: http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c

自动或自动实现的属性只是定义属性的一种更短,更简洁的方法.实际上,编译器会在构建应用程序时自动为您创建一个支持字段.请参阅以下页面以获取更多信息: https://msdn.microsoft.com/en-us/library/bb384054.aspx

An automatic or auto-implemented property is just a shorter and more concise way of defining a property. The compiler will actually create a backing field for you automatically when you build the application. Please refer to the following page for more information: https://msdn.microsoft.com/en-us/library/bb384054.aspx

>> 自动生成的私有支持字段的名称在您的C#代码库中不可见.看到它的唯一方法是使用诸如ildasm.exe之类的工具.

由于后勤字段是在编译时自动生成的,因此您无法在代码中看到它.

Since the backing field gets generated automatically at compile-time, you cannot see it in your code.

希望有帮助.

请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


这篇关于自动属性与简单变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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