C# 了解获取和设置访问器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15463416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:55:31  来源:igfitidea点击:

Understanding Get and Set Accessors

c#asp.net

提问by iJade

I'm a newbie and I'm trying to learn the basics of C#. This might sound quite trivial and may be stupid but its a doubt. While going through one of the source codes of an application, I saw a piece of code inside a class

我是新手,我正在尝试学习 C# 的基础知识。这可能听起来很微不足道,可能很愚蠢,但这是一个疑问。在浏览一个应用程序的源代码时,我看到了一个类中的一段代码

private string fname;
public string FirstName
{
    get
    {
       return fname
    }
    set
    {
       fname = value;
    }
}

Can anyone tell me what it means. I understand that when we declare a class we access fnameusing an alias FirstName. If it's for some security purpose then what?

谁能告诉我是什么意思。我知道当我们声明一个类时,我们fname使用别名访问FirstName。如果是出于某种安全目的,那又是什么?

采纳答案by Darin Dimitrov

This code is also equivalent to:

此代码也等效于:

public string FirstName { get; set; }

What this do is define a property. In C# properties provide encapsulation for private fields.

这样做是定义一个property. 在 C# 中,属性为 private 提供封装fields

回答by J0HN

It's called Properties (MSDN article). The reason for using them is to encapsulate accessing some class field to be able to easily change class behavior in future if needed.

它称为属性(MSDN 文章)。使用它们的原因是为了封装访问某些类字段,以便将来可以在需要时轻松更改类行为。

This is also equivalent to so called auto-property, since the property at this moment oftimedoes not add any logic:

这也相当于所谓的auto-property,因为此时时刻的属性并没有添加任何逻辑:

public string FirstName { get; set; }

回答by syned

fname is a field and has private visibility but FirstName is a public property therefore it will be visible outside of the class and can contain logic inside get and set methods

fname 是一个字段并且具有私有可见性但 FirstName 是一个公共属性因此它将在类之外可见并且可以在 get 和 set 方法中包含逻辑

回答by Farhad Jabiyev

You can write your custom logic on your property. F.e, some validation:

您可以在您的财产上编写自定义逻辑。Fe,一些验证:

public string FirstName
{
    get
    {
       return fname;
    }
    set
    {
       if (value.Count(s => Char.IsDigit(s)) > 0)
       {
           throw new Exception("Only letters allowed");
       }
       fname = value;
    }
}

回答by direndd

get and set methods are called accessors(getters) and mutators(setters) these methods are used to access and mutate the attributes of an object without allowing the access from outside the class. See that access modifier of the variable fname is private which means it can only be accessed by any method inside the class.

get 和 set 方法称为访问器(getter)和修改器(setter),这些方法用于访问和改变对象的属性,而不允许从类外部进行访问。看到变量 fname 的访问修饰符是私有的,这意味着它只能被类内的任何方法访问。

and note that the get and set methods should normally be given the public access modifier which enables the method to be accessed from any outside class.

并注意 get 和 set 方法通常应该被赋予 public 访问修饰符,以便可以从任何外部类访问该方法。