问题
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. I can follow the code fine, but I don't quite understand what static T is in VB.NET. Can someone please explain it a bit?
回答1:
The equivalent of static
in VB in Shared
. Shared methods are usually put in Helper classes, because they do not require an instance of the class to run.
The type T indicates that this is a generic method (this is a new feature in VB 9 and 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. 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.
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. Here is your code in 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).
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
回答2:
He's creating an extension method. In C#, that's done by creating a static method (Shared in Visual Basic).
The mechanism for creating extension methods in Visual Basic appears to be much different than how you do it in C#. You'll probably want to read the MSDN entry about extension methods, here: http://msdn.microsoft.com/en-us/library/bb384936.aspx
回答3:
This is what the method would look like in 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.
回答4:
C#'s static keyword is the same as VB's Shared keyword.
回答5:
T in your example is the type-parameter in your generic method.
In 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)
Not sure about the VB syntax for creating an extension method, though. (This is what the "this" keyword on your first parameter denotes.)
回答6:
What you are looking at is not "static T", but two separate parts.
- public denotes the method as publicly visible.
- static denotes the method as static. This means it runs for the class, not for an instance.
- T - is the return type.
More information on static function.
Static functions in VB.NET are know as shared functions.
More information on shared functions.
来源:https://stackoverflow.com/questions/467642/vb-net-what-is-static-t-c-in-vb-net