C#的getter / setter [英] c#: getter/setter

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

问题描述

我看到类似的地方下面,和不知道是什么意思。我知道他们是getter和setter,但想知道为什么string类型是这样定义。感谢您帮助我。

 公共字符串类型{搞定;组; }


解决方案

这些都是自动实现的属性(自动属性的简称)。

,编译器会自动生成以下简单的实现等价的:

 私人字符串_type;公共字符串类型
{
    得到{_type; }
    集合{_type =价值; }
}

I saw something like the following somewhere, and was wondering what it meant. I know they are getters and setters, but want to know why the string Type is defined like this. Thanks for helping me.

public string Type { get; set; }

解决方案

Those are Auto-Implemented Properties (Auto Properties for short).

The compiler will auto-generate the equivalent of the following simple implementation:

private string _type;

public string Type
{
    get { return _type; }
    set { _type = value; }
}

这篇关于C#的getter / setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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