VB.net:什么是在VB.net静态T(C#)? [英] VB.net: What is static T (C#) in VB.net?

查看:114
本文介绍了VB.net:什么是在VB.net静态T(C#)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 T returnValue = default(T);

您好,我想实现像<一个href="http://weblogs.asp.net/skillet/archive/2008/06/18/idatareader-extension-methods.aspx">this

要检查的DBNull。我可以按照code罚款,但我不太明白什么是静态T是在VB.net。是否有人可以解释一下?

to check DBNull. I can follow the code fine but i don't quite understand what is static T is in VB.net. Can someone please explain a bit?

感谢

杰克

推荐答案

静在VB中共享的等效。共享方法通常放在辅助类,因为它们不需要类的实例来运行。

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.

T是类型表明这是一个通用的方法(这是VB9和C#3的新特性)。一个通用的方法,有效地接受一个类型作为参数或返回一个泛型类型。

The type T is indicates that this is a generic method (this is a new feature in VB9 and C# 3). A generic method effectively takes a type as an argument or returns a generic type.

扩展方法也有新的VB9 / C#3,这些允许您通过添加方法来扩展现有的类型。所有你需要的是一个共享的方法,这是在同一个命名空间为您的code可用,在VB中的code的是在一个模块,而不是一个普通的类。模块是不能被实例化,(因此)不仅拥有共享的方法的类。它与到位class关键字的模块关键字声明。这是你的code在VB。

Extension methods are also new in VB9/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, 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.

(也为那些知道是怎么回事在幕后奇怪的设定值类型为Nothing并在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

这篇关于VB.net:什么是在VB.net静态T(C#)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆