嗨 我在想,为什么要使用属性而不是获取和设置方法? 我有一个不喜欢属性的同事 - 并且更喜欢写 " string GetName()"和void SetName(字符串名称)例如。 有没有关于为什么属性更好的论据?或者只是 一个问题或偏好?就个人而言,我喜欢他们 - 我认为他们看起来很好,但这就像我能做的一样好: - | (我认为自动属性也是一个很好的增强。) 谢谢, 彼得
Hi I was wondering, why use properties instead of get and set methods? I have a colleague who dislikes properties - and much prefers to write "string GetName()" and "void SetName(string name)" methods for example. Are there any arguments as to why properties are better? Or is it just a matter or preference? Personally I like them - I think they "look nice", but that''s about as good as I can do :-| (And I think automatic properties are a nice enhancement too). Thanks, Peter
推荐答案使用属性存在技术差异和充分理由。我相信有些人会更有能力解释他们。我作为 类文档工具的合着者,在 除了其他的之外还有以下具体原因:) 获取和设置方法需要两个XML注释,并在 生成的类文档中生成两个条目。另一方面,使用属性, 只是一个XML注释和帮助中的一个条目,如在MSDN中:) 有'与记录略有关系的另一个原因。如果没有Form.Text属性,你会感到高兴吗?你如何在设计时设置表格 标题?属性可以在属性 窗口中显示和设置。此外,您可以向属性添加System.ComponentModel.Description 属性,以在Properties 窗口中显示属性描述。这是不可能的方法。 - Peter Macej Helixoft - www.helixoft VSdocman - C#,VB的类文档的评论者和生成器 ..NET和ASP .NET代码 There are technical differences and good reasons to use properties. I believe someone more competent will explain them. I, as a co-author of class documentation tool, have the following specific reasons in addition to the other ones :) Get and set methods require two XML comments and produce two entries in generated class documentation. On the other hand, using property, there is only one XML comment and one entry in the help, like in MSDN :) There''s another reason slightly related to documenting. Would you be happy if there was no Form.Text property? How would you set the form caption at design time? Properties can be shown and set in Properties window. Moreover, you can add System.ComponentModel.Description attribute to your property to display property description in Properties window. This is not possible with methods. -- Peter Macej Helixoft - www.helixoft VSdocman - Commenter and generator of class documentation for C#, VB ..NET and ASP .NET code
Peter Macej写道: Peter Macej wrote: 使用属性存在技术差异和充分理由。我相信有些人会更有能力解释他们。作为 类文档工具的合着者,我有以下具体原因 除了其他的:) There are technical differences and good reasons to use properties. I believe someone more competent will explain them. I, as a co-author of class documentation tool, have the following specific reasons in addition to the other ones :)
引擎盖下的属性和get和set方法基本上是 相同;编译器在编译 属性时实际生成了两种方法。 我的一个朋友称属性为语法糖。他们很可能是语法糖,但它们是非常好的和甜的语法糖。 我认为技术上的差异之一就是当你序列化; 类的属性自动序列化,iirc。
Under the hood properties and get and set methods are basically the same; the compiler actually generates two methods when it compiles the property. One of my friends called properties "syntactic sugar". They might well be syntactic sugar, but they are very nice and sweet syntactic sugar. I think one of the technical difference is when you''re serializing; properties on classes get serialized automatically, iirc.
还有另一个与文档略有关系的原因。如果没有Form.Text属性,你会感到高兴吗?你如何在设计时设置表格 标题?属性可以在属性 窗口中显示和设置。此外,您可以向属性添加System.ComponentModel.Description 属性,以在Properties 窗口中显示属性描述。使用方法无法做到这一点。 There''s another reason slightly related to documenting. Would you be happy if there was no Form.Text property? How would you set the form caption at design time? Properties can be shown and set in Properties window. Moreover, you can add System.ComponentModel.Description attribute to your property to display property description in Properties window. This is not possible with methods.我怀疑Design Time集成可能是Properties在第一次进入.NET时遇到的主要原因之一地点。 - Chris Crowther MBCS C#开发商 RHD研究有限公司
I suspect that the Design Time integration might be one of the primary reasons that Properties found their way in to .NET in the first place. -- Chris Crowther MBCS C# Developer RHD Research Ltd
4月16日,3:54 * am,Peter < xdz ... @ hotmailwrote: On Apr 16, 3:54*am, "Peter" <xdz...@hotmailwrote: 嗨 我想知道,为什么要使用属性而不是获取并设置方法? Hi I was wondering, why use properties instead of get and set methods?
嗯,这些属性实际上是方法:),编译器是翻译它们的那个 。
Well, the properties are actually methods :) , The compiler is the one that translate them.
我有一个不喜欢属性的同事 - 更喜欢写 " string GetName()"和void SetName(字符串名称)方法,例如。 I have a colleague who dislikes properties - and much prefers to write "string GetName()" and "void SetName(string name)" methods for example.使用属性是更清晰,更紧凑的表示法。
Using a property is clearer, more compact notation.
是否有任何关于为什么属性的参数更好?或者只是 一个问题或偏好?就个人而言,我喜欢他们 - 我认为他们看起来很好,但这就像我能做的一样好: - | (而且我认为自动属性也是一个很好的增强。) 谢谢, Peter Are there any arguments as to why properties are better? Or is it just a matter or preference? Personally I like them - I think they "look nice", but that''s about as good as I can do :-| (And I think automatic properties are a nice enhancement too). Thanks, Peter
更多推荐
物业有什么意义?
发布评论