简体   繁体   English

vb.net中参数化属性和函数有什么区别?

[英]What is the difference between a parameterized property and function in vb.net?

I am coming from the C# world to VB.NET and this puzzles me. 我来自C#世界到VB.NET,这让我很困惑。 Why are there 2 ways of doing the same thing? 为什么有两种方法可以做同样的事情? or is there some difference I am not aware of? 或者我不知道有什么不同?

What is the difference between the following: 以下是有什么区别的:

Public ReadOnly Property Test(ByVal v as String) As Integer
  Get
     Return SomeOperationOn(v)
  End Get
End Property

and

Public Function Test(ByVal v as String) As Integer
   Return SomeOperationOn(v)
End Function

When do you use one as opposed to the other? 你什么时候使用一个而不是另一个?

Functionally there is no difference, they both return a value based on a parameter. 在功能上没有区别,它们都返回基于参数的值。 In fact, properties are actually transformed into functions during compilation, since the notion of a property doesn't exist in MSIL . 事实上,属性实际上是在编译期间转换为函数,因为MSIL中不存在属性的概念。

Semantically , however, there is a difference in how they should be used. 然而,在语义上应该如何使用它们。 Properties are meant as a way to expose the internal state of an object. 属性用于表示公开对象的内部状态。 Functions, on the other hand, are supposed to operate on an object's state to either provide an answer to a specific question (a query ) or to modify the state in some way (a command ). 另一方面,函数应该在对象的状态上操作 ,以提供特定问题( 查询 )的答案或以某种方式( 命令 )修改状态。

Here's an example: 这是一个例子:

Public Class Rectangle
    Private _size As Size

    ReadOnly Property Size() As Size
        Get
           Return _size
        End Get
    End Property

    Public Function IsSquare() As Boolean
        Return _size.Width = _size.Height
    End Function
End Class

While Size simply exposes a property of the object, the IsSquare function actually performs an operation on the object's internal state in order to answer a question. Size只是公开对象的属性 ,而IsSquare函数实际上对对象的内部状态执行操作以回答问题。

Based on this principle, the most common use case for parameterized properties in VB.NET is in classes that represent a sequence of items, where the parameter is used to access a specific element in the sequence by its position or by some unique key. 基于此原则,VB.NET中参数化属性的最常见用例是表示项目序列的类,其中参数用于通过其位置或某个唯一键访问序列中的特定元素。 In other words, to create what's known as indexers in C#. 换句话说,在C#中创建所谓的索引器

There's plenty of history behind this question, this goes back to 1997 when Microsoft released the COM Automation specification. 这个问题背后有很多历史,这可以追溯到1997年微软发布COM自动化规范时。 Which allowed property setters/getters to have arguments. 哪个允许属性setter / getters有参数。 Visual Basic was an early adopter of the spec, it was driven in no small part by the language to find a replacement for the VBX extension model. Visual Basic是该规范的早期采用者,它在很大程度上是由语言驱动的,以找到VBX扩展模型的替代品。 Which ran out of gas around that time, it was heavily dependent on the 16-bit coding model. 在那段时间里,气体耗尽,它严重依赖于16位编码模型。

The C# team took a pretty no-nonsense attitude to the feature, they absolute hate syntax ambiguities. C#团队对该功能采取了一种非常严肃的态度,他们绝对讨厌语法含糊不清。 That just doesn't belong in a brand new language. 这不属于一种全新的语言。 VB.NET didn't have the same luxury, they had to at least support some of the features of the previous generation, VB6 at the time. VB.NET没有同样的奢侈品,他们不得不至少支持上一代VB6的一些功能。

Zip forward 10 years, the C# team had to back-pedal a bit by popular demand. 拉链前进10年,C#团队不得不受到大众需求的反击。 Indexed properties are rife in, for example, the Office object model. 索引属性在例如Office对象模型中很普遍。 In C# version 4 they allowed indexed properties exclusively for COM interfaces to soften the pain of writing C# Office code. 在C#版本4中,它们允许专用于COM接口的索引属性,以减轻编写C#Office代码的痛苦。 And more, optional and named arguments got added as well to deal with the Type.Missing misery. 此外,还添加了可选和命名参数来处理Type.Missing苦难。 And the dynamic keyword to support late binding, another important feature of COM and Visual Basic that was really painful to do in C# without that keyword. 动态关键字支持后期绑定,这是COM和Visual Basic的另一个重要特性,在没有该关键字的C#中真的很痛苦。

Long story short, COM is beautiful, the elegance of IUnknown is stark. 长话短说,COM很漂亮,IUnknown的优雅是鲜明的。 Tony Williams is the genius behind it. 托尼威廉姆斯是背后的天才。 Video is here , much worth watching. 视频在这里 ,非常值得关注。 The subset of COM Automation, IDispatch, is not so beautiful. COM Automation的子集IDispatch并不是那么漂亮。 But it was incredibly successful. 但它非常成功。 Languages ignore it at their peril. 语言会忽视它的风险。 C# didn't. C#没有。

These details might sound arcane from an era long gone but they are not. 这些细节可能听起来很久以前就已经过去,但事实并非如此。 The next version of the Windows API, WinRT is completely based on IUnknown. 下一版本的Windows API,WinRT完全基于IUnknown。 Otherwise known as "Metro" or "Modern UI". 否则称为“Metro”或“Modern UI”。 IDispatch did not survive, replaced by IInspectable. IDispatch没有生存,取而代之的是IInspectable。

The property can also have a setter: 该物业还可以有一个二传手:

Public Property Test(ByVal v as String) As Integer
   Get
      Return SomeDictionary(v)
   End Get
   Set
       SomeDictionary(v) = Value
   End Set
End Property

That makes a difference, because it allows you to write something like this: 这有所不同,因为它允许你写这样的东西:

MyObject.Test(index) = SomeValue

C# only allows you to assign like that via property indexers: C#只允许您通过属性索引器进行分配:

MyOjbect[index] = SomeValue;

That means in C# you can only have one indexed property per type. 这意味着在C#中,每种类型只能有一个索引属性。 VB.Net allows more than one indexed property on a type. VB.Net允许在一个类型上有多个索引属性。 To get equivalent syntax, C# would either have to expose the underlying dictionary directly or, if you have other code in the getter/setter(such as logging) you would have to create an additional type to wrap your dictionary. 要获得等效的语法,C#必须直接公开底层字典,或者如果getter / setter中有其他代码(例如日志记录),则必须创建一个附加类型来包装字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .net 中的 readonly 属性和函数有什么区别? - What is the difference between readonly property and function in .net? VB.NET中的公共变量和公共属性有什么区别? (代码分析VS2010,CA1051:Microsoft.Design) - What's the difference between Public variable and Public Property in VB.NET? (Code Analysis VS2010, CA1051 : Microsoft.Design) VB.net中的myObject.MyField和myObject.MyField()有什么区别? - What's the difference between myObject.MyField and myObject.MyField() in VB.net? C# static class 和 ZBD5C8A1AFE53C80BA44F60C9AFC6 模块之间有什么区别(如果有) - What, if any, difference is there between a C# static class and a VB.NET Module C#和VB.NET中的转换之间的区别 - Difference between casting in C# and VB.NET VB.NET中SystemEvents的Handles声明和AddHandler之间的区别 - Difference between Handles declaration and AddHandler for SystemEvents in VB.NET 阴影(VB.NET)和新(C#)之间的区别 - Difference between Shadows (VB.NET) and New (C#) VB.NET中的Integer和Int32有什么区别吗? - Is there any difference between Integer and Int32 in VB.NET? C#和VB.NET控制台会话之间的区别 - Difference between C# and VB.NET console session VB.Net与C#“As New WebControl”的区别 - Difference between VB.Net and C# “As New WebControl”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM