在C#3.0自动属性和公共领域之间的区别 [英] Difference between Automatic Properties and public field in C# 3.0

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

问题描述

我不明白为什么自动实现的属性的语言特性存在于C#3.0。



这是使差异,当你说什么

 公共字符串名字; 



 公共字符串名字{获得;组; } 


解决方案

由于它们在产生IL代码的实现方式不同(与机器语言)。的自动属性仍暴露为一个公共的getter和setter,而公共领域就是这样 - 一个字段。



因此​​,实施自动属性允许您在一些日后可以更改任何一个getter或setter的内部行为(如添加一个验证器),而无需重新编译或重新编码=使用它......


的任何从属类

I failed to understand why auto implemented property language feature exist in C# 3.0.

What the difference it is making when you say

public string FirstName;

than

public string FirstName { get; set; }

解决方案

Because they are implemented differently in the resulting IL code (and machine language). An Automatic property is still exposed as a public getter and setter, whereas a public field is just that - a single field..

Thus, implementing an auto property allows you at some later date to change the internal behavior of either the getter or setter (like adding a validator) without recompiling or re=coding any dependant classes that use it...

这篇关于在C#3.0自动属性和公共领域之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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