简体   繁体   English

VB.NET:VB.NET中的静态T(C#)是什么?

[英]VB.NET: What is static T (C#) in VB.NET?

Consider: 考虑:

public static T GetValueOrDefault<T>(this IDataReader reader, string columnName)

 T returnValue = default(T);

I want to implement something like this to check DBNull. 我想实现像这样检查的DBNull。 I can follow the code fine, but I don't quite understand what static T is in VB.NET. 我可以很好地遵循代码,但我不太明白VB.NET中的静态T是什么。 Can someone please explain it a bit? 有人可以解释一下吗?

The equivalent of static in VB in Shared . Shared中VB中的static等价物。 Shared methods are usually put in Helper classes, because they do not require an instance of the class to run. 共享方法通常放在Helper类中,因为它们不需要运行类的实例。

The type T indicates that this is a generic method (this is a new feature in VB 9 and C# 3). 类型T表示这是一种通用方法(这是VB 9和C#3中的新功能)。 A generic method effectively takes a type as an argument or returns a generic type. 泛型方法有效地将类型作为参数或返回泛型类型。

Extension methods are also new in VB 9/C# 3. These allow you to extend an existing type by adding methods. 扩展方法也是VB 9 / C#3中的新增功能。这些方法允许您通过添加方法扩展现有类型。 All you need is a Shared method which is available in the same namespace as your code, and in VB the code has to be in a module, not a normal class. 您只需要一个Shared方法,它可以在与您的代码相同的命名空间中使用,而在VB中,代码必须位于模块中,而不是普通的类。

A module is a class that can't be instantiated and (therefore) only has shared methods. 模块是一个无法实例化的类,因此只有共享方法。 It is declared with the Module keyword in place of the class keyword. 它使用Module关键字声明,而不是class关键字。 Here is your code in VB. 这是你在VB中的代码。

(Also for those that know what's going on "under the covers" strangely setting a value type to Nothing does compile in VB and is the supported way to get the default value of a value type). (对于那些知道“幕后”发生了什么的人来说奇怪地将值类型设置为Nothing在VB中编译并且是获得值类型的默认值的支持方式)。

Imports System.Runtime.CompilerServices
<Extension()> _
Public Shared Function GetValueOrDefault(Of T)(ByVal reader As IDataReader, ByVal columnName As String) As T
Dim returnValue As T = Nothing

End Function

He's creating an extension method. 他正在创建一种扩展方法。 In C#, that's done by creating a static method (Shared in Visual Basic). 在C#中,这是通过创建静态方法(在Visual Basic中共享)来完成的。

The mechanism for creating extension methods in Visual Basic appears to be much different than how you do it in C#. 在Visual Basic中创建扩展方法的机制似乎与在C#中执行它的方式有很大不同。 You'll probably want to read the MSDN entry about extension methods, here: http://msdn.microsoft.com/en-us/library/bb384936.aspx 您可能希望阅读有关扩展方法的MSDN条目,请访问: http//msdn.microsoft.com/en-us/library/bb384936.aspx

This is what the method would look like in VB: 这是VB中的方法:

Imports System.Runtime.CompilerServices 

<Extension()> _
Public Shared Function GetValueOrDefault(Of T)(ByVal reader As IDataReader, ByVal columnName As String) as T
    Dim returnvalue As T = Nothing
End Function

I'm not sure how to do default(T) in VB so I left it out. 我不知道如何在VB中执行默认(T),所以我把它留了出来。

What you are looking at is not "static T", but two separate parts. 你所看到的不是“静态T”,而是两个独立的部分。

  • public denotes the method as publicly visible. public表示该方法是公开可见的。
  • static denotes the method as static. static表示方法为static。 This means it runs for the class, not for an instance. 这意味着它为类运行,而不是为实例运行。
  • T - is the return type. T - 是返回类型。

More information on static function . 有关静态功能的更多信息。

Static functions in VB.NET are know as shared functions. VB.NET中的静态函数称为共享函数。

More information on shared functions . 有关共享功能的更多信息。

C#的static关键字与VB的Shared关键字相同。

T in your example is the type-parameter in your generic method. 您的示例中的T是泛型方法中的type-parameter。

In VB: 在VB中:

Public Function GetValueOrDefault(Of T)(ByVal reader as IDataReader, ByVal columnName as string) as T

Means that when you call the method, you supply a type-parameter (telling what type T will be for the call to the method) 意味着当你调用方法时,你提供了一个类型参数(告诉T将调用该方法的类型)

Not sure about the VB syntax for creating an extension method, though. 但是不确定用于创建扩展方法的VB语法。 (This is what the "this" keyword on your first parameter denotes.) (这是你的第一个参数上的“this”关键字表示的。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM