公共访问.NET [英] Public accessor .net
问题描述
我想应该是在.NET 2.0中,微软引入了一个缩写为像
一个访问 公共字符串名称{;组; }
但有上面的代码之间的真正区别,简单地说:
公共字符串名称;
的主要区别是,如果以后需要添加逻辑到您的getter或setter和其他DLL已经被编译反对你的,你可以轻松地更改
公共字符串名称{ ;组; }
到
公共字符串名称{{/ *特殊代码* /}集合{/ *特殊代码* /}}
和它不会是一个重大更改,以发布新的DLL,并有其他DLL不会被重新编译。
然而,如果你改变了
公共字符串名称;
到
公共字符串名称{{/ *特殊代码* /}集合{/ *特殊代码* /}}
那么你需要确保你使用任何DLL被重新编译,因为他们访问一个字段为访问属性更改。
这显然,当你运送DLL来其他程序员(作为一个开源项目或作为例如组件供应商)一个更大的问题比如果你只是建立一个应用程序为你自己/雇主
I think it was in .net 2.0, microsoft introduced an accessor that was abbreviated to something like
public string Name { get; set; }
But is there any real difference between the above code, and simply:
public string Name;
The main difference is that if you later need to add logic into your getter or setter, and other DLLs have already been compiled against yours, you can easily change
public string Name { get; set; }
into
public string Name { get{/*special code*/} set{/*special code*/} }
and it won't be a breaking change to publish your new DLL and have other DLLs not be recompiled.
Whereas if you changed
public string Name;
into
public string Name { get{/*special code*/} set{/*special code*/} }
then you'll need to make sure any DLLs that use yours are recompiled, since they change from accessing a field into accessing a property.
This is obviously a bigger problem when you're shipping DLLs to other programmers (as an open source project or as a component vendor for example) than if you're just building an app for your own self / employer
这篇关于公共访问.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!