C#自动属性 [英] C# Automatic Properties

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

问题描述

我在C#例如

public string Forename{ get; set; }

我得到你是不必声明一个私有变量节省code,但是当你不使用任何获取或设置逻辑什么是属性点?为什么不使用

I get that you are saving code by not having to declare a private variable, but what's the point of a property when you are not using any get or set logic? Why not just use

public string Forename; 

我不知道这两个语句之间的区别是什么,我一直以为你使用的属性,如果你想要更多的get / set逻辑?

I'm not sure what the difference between these 2 statements is, I always thought you used properties if you wanted additional get/set logic?

推荐答案

属性可以有code放入其中不破坏合同,字段不能有code放入其中没有他们更改为属性(和打破接口)。只能读取或只写属性,字段不能。属性可以是数据绑定,字段不能。

Properties can have code put into them without breaking contract, fields can't have code put into them without changing them to properties (and breaking the interface). Properties can be read only or write only, fields can't. Properties can be data bound, fields can't.

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

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