VB.NET:什么是 VB.NET 中的静态 T (C#)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/467642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET: What is static T (C#) in VB.NET?
提问by Hyman
Consider:
考虑:
public static T GetValueOrDefault<T>(this IDataReader reader, string columnName)
T returnValue = default(T);
I want to implement something like thisto 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?
我想实现像这样检查的DBNull。我可以很好地遵循代码,但我不太明白 VB.NET 中的静态 T 是什么。有人可以解释一下吗?
采纳答案by Christopher Edwards
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.
相当于static
VB 中的Shared
. 共享方法通常放在 Helper 类中,因为它们不需要类的实例来运行。
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.
类型 T 表示这是一个泛型方法(这是 VB 9 和 C# 3 中的新功能)。泛型方法有效地将类型作为参数或返回泛型类型。
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.
VB 9/C# 3 中的扩展方法也是新的。它们允许您通过添加方法来扩展现有类型。您所需要的只是一个与您的代码在同一命名空间中可用的 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. Here is your code in VB.
模块是一个无法实例化的类,(因此)只有共享方法。它是用 Module 关键字代替 class 关键字声明的。这是您在 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
回答by Adam Peck
What you are looking at is not "static T", but two separate parts.
您正在查看的不是“静态 T”,而是两个独立的部分。
- 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.
- public 表示该方法是公开可见的。
- static 表示该方法是静态的。这意味着它为类运行,而不是为实例运行。
- T - 是返回类型。
More information on static function.
更多关于静态函数的信息。
Static functions in VB.NET are know as shared functions.
VB.NET 中的静态函数称为共享函数。
More information on shared functions.
有关共享功能的更多信息。
回答by Arjan Einbu
C#'s static keyword is the same as VB's Shared keyword.
C# 的 static 关键字与 VB 的 Shared 关键字相同。
回答by Jim Mischel
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#. You'll probably want to read the MSDN entry about extension methods, here: http://msdn.microsoft.com/en-us/library/bb384936.aspx
在 Visual Basic 中创建扩展方法的机制似乎与在 C# 中的创建方式大不相同。您可能想要阅读有关扩展方法的 MSDN 条目,请访问:http: //msdn.microsoft.com/en-us/library/bb384936.aspx
回答by Arjan Einbu
T in your example is the type-parameter in your generic method.
示例中的 T 是泛型方法中的类型参数。
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. (This is what the "this" keyword on your first parameter denotes.)
不过,不确定用于创建扩展方法的 VB 语法。(这是第一个参数上的“this”关键字所表示的。)
回答by Nathan W
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 中执行 default(T) 所以我把它省略了。