参数化特性和功能在vb.net之间的区别是什么?
我从C#世界VB.NET来了,这令我费解。为什么有2种方式做同样的事情吗?或者是有一些区别,我不知道呢?
I am coming from the C# world to VB.NET and this puzzles me. 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
和
Public Function Test(ByVal v as String) As Integer
Return SomeOperationOn(v)
End Function
当你使用一个,而不是其他?
When do you use one as opposed to the other?
功能没有区别,它们都返回基于一个参数的值。事实上,属性实际上是转化为函数编译过程中,因为一个的属性的不存在的概念MSIL 。
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.
语义,然而,在如何的的差异应的使用。性质意味着作为一种方式,以暴露一个对象的内部状态。功能,另一方面,都应该操作的上对象的状态要么提供一个答案的一个具体问题(一个的查询的),或修改状态以某种方式(一的命令的)。
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).
下面是一个例子:
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
在尺寸
只是公开的对象,实际上是一个的属性的的 IsSquare
功能为了回答一个问题上执行的对象的内部状态的操作的。
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.
根据这一原则,最常见的用例在VB.NET参数化属性在类的再present序列的项目,这里的参数用于访问特定的元素通过它的位置或一些独特的键顺序进行。换句话说,创造了所谓的索引的在C#。
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. In other words, to create what's known as indexers in C#.