c#中get和set的简单版本 [英] the simple version of get and set in c#

查看:121
本文介绍了c#中get和set的简单版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解以c#来获取和设置。

但是我不明白这个简单的:



I understand somehow the get and set in c#.
But I did not understand the simple one like:

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
}





与没有get和set的那个相比,它有什么优势,即:





what is the advantage of it compared to the one without get and set, namely:

public class Customer
{
    public int ID;
    public string Name;
}





至少,尽管后面的机制不同,但从呼叫中没有任何区别。任何人都可以通过使用简单的get和set来澄清具体的优势?或者给我一个具体的例子?





我知道上面的简单版本等同于以下内容。



At least, from the calling no any difference although the mechanism behind is different. Anyone can clarify me the concrete advantage by using simple get and set? or give me a concrete exmaple?


I know that above simple version is equivalent to the following.

public class Customer
{
    private int m_id = -1;

    public int ID
    {
        get
        {
            return m_id;
        }
        set
        {
            m_id = value;
        }
    }

    private string m_name = string.Empty;

    public string Name
    {
        get
        {
            return m_name;
        }
        set
        {
            m_name = value;
        }
    }
}

推荐答案

你所指的是Auto-Implemented properties [ ^ ]



基本上他们可以帮助你定义一个没有定义一个类变量在哪里保存数据的复杂性。



你问的另一件事是为什么要使用自动实现的属性而不仅仅是字段。这是一个设计问题,通常认为在课堂外定义要修改的字段是不好的做法。



另一件事是你可以定义自动实现的访问级别属性。例如,您可以定义以下内容

What you're referring to is Auto-Implemented properties[^]

Basically they help you to define a property without the complexity of defining a class variable where to hold the data.

The other thing you asked was why to use auto-implemented properties and not simply fields. This is a design question and commonly it's considered a bad practice to define fields public to be modified outside the class.

Another thing is that you can define access levels for autoimplemented properties. For example you can define the following
public class Customer
{
    public int ID { get; private set; }
    public string Name { get; set; }
}



这意味着ID只能在类中设置。


That would mean that the ID can only be set from inside the class.


...{
    public string Name {get;set;}
}



是编译器生成后端字段get / set(而不是定义自己的字段 - 更快地编写代码)。


Is a compiler generated back end field get/set (as opposed to defining your own fields - a short hand for writing code faster).

...{
    public string Name;
}



是字段访问定义。

您需要获取/设置.net数据绑定才能工作(默认情况下)它不会绑定到UI组件中的字段属性,如网格等。)



一般来说,最好有get / set,因为你可以进行检查和验证将来需要它,使用它的代码不会改变[字段需要重新编译]。


Is a field access definition.
You need get/set for .net data binding to work (as by default it does not bind to field properties in UI components like the grid etc.).

Generally it is better to have get/set since you can do checking and validation if it is needed in the future and the code using this will not change [fields need a recompile].


这篇关于c#中get和set的简单版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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