簡體   English   中英

Vb.Net屬性語法

[英]Vb.Net Properties Syntax

我曾在工作中詢問過我的同事,甚至試圖在互聯網上查詢,但我一直無法得到答案。

有什么區別

Public Property Name As String

Public Property Name() As String

在屬性名稱后添加()有什么區別?

首先,您可能會發現Property與Method有許多相似之處。 從這個預期的,用於參數的屬性中的括號。 如果屬性沒有參數,則可以省略它。 以下是完整的屬性聲明語法:

[Default] [Modifiers] Property PropertyName[(ParameterList)] [As DataType]
[AccessLevel] Get
    ' Statements of the Get procedure.
    ' The following statement returns an expression as the property's value.
    Return Expression
End Get
[AccessLevel] Set[(ByVal NewValue As DataType)]
    ' Statements of the Set procedure.
    ' The following statement assigns newvalue as the property's value.
    LValue = NewValue
End Set
End Property

您可以在以下鏈接中找到有價值的提示: vb.net中參數化屬性和函數之間的區別是什么? https://msdn.microsoft.com/en-us/library/e8ae41a4.aspx

您可以在VB.NET語言規范中查找這些詳細信息。 這是一個非常正式的文件,但仍然很可讀。 第9.7章包含有關Property關鍵字語法的所有詳細信息。 你會看到:

PropertySignature  ::=
    Property  Identifier  [  OpenParenthesis  [  ParameterList  ]  CloseParenthesis  ]
        [  As  [  Attributes  ]  TypeName  ]

[]括號表示語法的可選部分。 所以,你可以很容易地看到你沒有使用()如果屬性不帶任何參數。 換句話說,當它不是索引屬性時。

所以沒有區別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM