thuydiemngobao
New member
#vb.net #.net #NAMEOF #Coding #Programming ## nameOf trong vb.net
Toán tử `nameOf` trong vb.net trả về biểu diễn chuỗi của một biểu thức.Nó có thể được sử dụng để có được tên của một biến, phương thức hoặc thuộc tính.
## Cú pháp
Cú pháp cho toán tử `nameOf` như sau:
`` `VBNet
Tên (Biểu thức)
`` `
trong đó `expression` là biểu thức có tên bạn muốn nhận được.
## ví dụ
Mã sau đây cho thấy cách sử dụng toán tử `nameof` để lấy tên của một biến:
`` `VBNet
Dimer myvar như số nguyên = 10
Console.WriteLine (NameOf (MyVar))
`` `
Mã này sẽ xuất ra như sau:
`` `
myvar
`` `
Mã sau đây cho thấy cách sử dụng toán tử `nameof` để lấy tên của một phương thức:
`` `VBNet
Sub mymethod ()
Console.WriteLine (NameOf (MyMethod))
Kết thúc phụ
`` `
Mã này sẽ xuất ra như sau:
`` `
mymethod
`` `
Mã sau đây cho thấy cách sử dụng toán tử `nameof` để lấy tên của một thuộc tính:
`` `VBNet
Lớp myclass
Tài sản công cộng MyProperty As Integer
Kết thúc lớp học
Dim MyObject như MyClass mới
Console.WriteLine (NameOf (MyObject.MyProperty)))
`` `
Mã này sẽ xuất ra như sau:
`` `
Tài sản của tôi
`` `
## Lời khuyên
* Toán tử `nameOf` có thể được sử dụng để lấy tên của một biểu thức ngay cả khi nó không được khai báo trong cùng một phạm vi.
* Toán tử `nameOf` có thể được sử dụng để lấy tên của một biểu thức là tham số loại chung.
* Toán tử `nameOf` có thể được sử dụng để lấy tên của một biểu thức là biểu thức Lambda.
## Người giới thiệu
* [MSDN: Toán tử tên] (https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/nameof-operator)
=======================================
#vb.net #.net #NAMEOF #Coding #Programming ## nameof in VB.NET
The `nameof` operator in VB.NET returns the string representation of an expression. It can be used to get the name of a variable, method, or property.
## Syntax
The syntax for the `nameof` operator is as follows:
```vbnet
nameof(expression)
```
where `expression` is the expression whose name you want to get.
## Examples
The following code shows how to use the `nameof` operator to get the name of a variable:
```vbnet
Dim myVar As Integer = 10
Console.WriteLine(nameof(myVar))
```
This code will output the following:
```
myVar
```
The following code shows how to use the `nameof` operator to get the name of a method:
```vbnet
Sub myMethod()
Console.WriteLine(nameof(myMethod))
End Sub
```
This code will output the following:
```
myMethod
```
The following code shows how to use the `nameof` operator to get the name of a property:
```vbnet
Class MyClass
Public Property MyProperty As Integer
End Class
Dim myObject As New MyClass
Console.WriteLine(nameof(myObject.MyProperty))
```
This code will output the following:
```
MyProperty
```
## Tips
* The `nameof` operator can be used to get the name of an expression even if it is not declared in the same scope.
* The `nameof` operator can be used to get the name of an expression that is a generic type parameter.
* The `nameof` operator can be used to get the name of an expression that is a lambda expression.
## References
* [MSDN: nameof operator](https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/nameof-operator)
Toán tử `nameOf` trong vb.net trả về biểu diễn chuỗi của một biểu thức.Nó có thể được sử dụng để có được tên của một biến, phương thức hoặc thuộc tính.
## Cú pháp
Cú pháp cho toán tử `nameOf` như sau:
`` `VBNet
Tên (Biểu thức)
`` `
trong đó `expression` là biểu thức có tên bạn muốn nhận được.
## ví dụ
Mã sau đây cho thấy cách sử dụng toán tử `nameof` để lấy tên của một biến:
`` `VBNet
Dimer myvar như số nguyên = 10
Console.WriteLine (NameOf (MyVar))
`` `
Mã này sẽ xuất ra như sau:
`` `
myvar
`` `
Mã sau đây cho thấy cách sử dụng toán tử `nameof` để lấy tên của một phương thức:
`` `VBNet
Sub mymethod ()
Console.WriteLine (NameOf (MyMethod))
Kết thúc phụ
`` `
Mã này sẽ xuất ra như sau:
`` `
mymethod
`` `
Mã sau đây cho thấy cách sử dụng toán tử `nameof` để lấy tên của một thuộc tính:
`` `VBNet
Lớp myclass
Tài sản công cộng MyProperty As Integer
Kết thúc lớp học
Dim MyObject như MyClass mới
Console.WriteLine (NameOf (MyObject.MyProperty)))
`` `
Mã này sẽ xuất ra như sau:
`` `
Tài sản của tôi
`` `
## Lời khuyên
* Toán tử `nameOf` có thể được sử dụng để lấy tên của một biểu thức ngay cả khi nó không được khai báo trong cùng một phạm vi.
* Toán tử `nameOf` có thể được sử dụng để lấy tên của một biểu thức là tham số loại chung.
* Toán tử `nameOf` có thể được sử dụng để lấy tên của một biểu thức là biểu thức Lambda.
## Người giới thiệu
* [MSDN: Toán tử tên] (https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/nameof-operator)
=======================================
#vb.net #.net #NAMEOF #Coding #Programming ## nameof in VB.NET
The `nameof` operator in VB.NET returns the string representation of an expression. It can be used to get the name of a variable, method, or property.
## Syntax
The syntax for the `nameof` operator is as follows:
```vbnet
nameof(expression)
```
where `expression` is the expression whose name you want to get.
## Examples
The following code shows how to use the `nameof` operator to get the name of a variable:
```vbnet
Dim myVar As Integer = 10
Console.WriteLine(nameof(myVar))
```
This code will output the following:
```
myVar
```
The following code shows how to use the `nameof` operator to get the name of a method:
```vbnet
Sub myMethod()
Console.WriteLine(nameof(myMethod))
End Sub
```
This code will output the following:
```
myMethod
```
The following code shows how to use the `nameof` operator to get the name of a property:
```vbnet
Class MyClass
Public Property MyProperty As Integer
End Class
Dim myObject As New MyClass
Console.WriteLine(nameof(myObject.MyProperty))
```
This code will output the following:
```
MyProperty
```
## Tips
* The `nameof` operator can be used to get the name of an expression even if it is not declared in the same scope.
* The `nameof` operator can be used to get the name of an expression that is a generic type parameter.
* The `nameof` operator can be used to get the name of an expression that is a lambda expression.
## References
* [MSDN: nameof operator](https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/nameof-operator)