寻找短片C#中的getter / setter方法的简单示例 [英] Looking for a short & simple example of getters/setters in C#

查看:137
本文介绍了寻找短片C#中的getter / setter方法的简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解C#语言中的getter和setters概念时遇到了麻烦。在像Objective-C这样的语言中,它们似乎是系统不可或缺的一部分,但在C#中却不那么重要(据我所知 )。我已经阅读过书籍和文章,所以我的问题是,那些了解吸气剂和吸尘器的人。 C#中的二传手,如果您是向一个完整的初学者讲授该概念的,那么您将个人使用哪个示例(这将包括尽可能少的代码行)?

I am having trouble understanding the concept of getters and setters in the C# language. In languages like Objective-C, they seem an integral part of the system, but not so much in C# (as far as I can tell). I have read books and articles already, so my question is, to those of you who understand getters & setters in C#, what example would you personally use if you were teaching the concept to a complete beginner (this would include as few lines of code as possible)?

推荐答案

我认为一些代码将有助于说明设置方法和获取方法是什么:

I think a bit of code will help illustrate what setters and getters are:

public class Foo
{
   private string bar;

   public string GetBar()
   {
       return bar;
   }

   public void SetBar(string value)
   {
       bar = value;
   }
}

在此示例中,我们有一个班级的私有成员那就是所谓的酒吧。 GetBar和SetBar方法完全按照它们的名称命名-一个检索bar成员,另一个设置其值。

In this example we have a private member of the class that is called bar. The GetBar and SetBar methods do exactly what they are named - one retrieves the bar member, and the other sets its value.

在c#1.1 +中,您具有属性。基本功能也相同:

In c# 1.1 + you have properties. The basic functionality is also the same:

public class Foo
{
    private string bar;

    public string Bar
    {
        get { return bar; }
        set { bar = value; }
    }
}

课外无法访问私人会员酒吧。但是公共的 Bar是,它有两个访问器-get,就像上面的示例 GetBar()返回私有成员一样,还有一个访问器-与前面提到的SetBar(string value)方法相对应

The private member bar is not accessible outside the class. However the public "Bar" is, and it has two accessors - get, which just as the example above "GetBar()" returns the private member, and also a set - which corresponds to the SetBar(string value) method in the forementioned example.

从C#3.0及更高版本开始,编译器进行了优化,以至于此类属性不需要以私有成员作为其源。编译器会自动生成该类型的私有成员,并将其用作属性的来源。

Starting with C# 3.0 and above the compiler became optimized to the point where such properties do not need to have the private member as their source. The compiler automatically generates a private member of that type and uses it as a source of a property.

public class Foo
{
   public string Bar { get; set; }
}

代码显示的是一个自动属性,其私有成员由编译器。您没有看到私人成员,但有私人成员。这也带来了其他一些问题-主要是访问控制。在C#1.1和2.0中,您可以省略属性的获取或设置部分:

what the code shows is an automatic property that has a private member generated by the compiler. You don't see the private member but it is there. This also introduced a couple of other issues - mainly with access control. In C# 1.1, and 2.0 you could omit the get or set portion of a property:

public class Foo
{
    private string bar;

    public string Bar
    {
        get{ return bar; }
    }
}

让您有机会限制其他对象的交互方式与Foo类的 Bar属性一起使用。从C#3.0及更高版本开始-如果选择使用自动属性,则必须指定对属性的访问权限,如下所示:

Giving you the chance to restrict how other objects interact with the "Bar" property of the Foo class. Starting with C# 3.0 and above - if you chose to use automatic properties you would have to specify the access to the property as follows:

public class Foo
{
    public string Bar { get; private set; }
}

这意味着只有类本身才能将Bar设置为某个值,但是任何人都可以读取Bar中的值。

What that means is that only the class itself can set Bar to some value, however anyone could read the value in Bar.

这篇关于寻找短片C#中的getter / setter方法的简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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