VB.Net正在初始化对飞阵列 [英] VB.Net Initialising an array on the fly

查看:97
本文介绍了VB.Net正在初始化对飞阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这一点 - 很简单 - 函数,然后不知道是否VB有一些pre-内置功能来做到这一点,但找不到任何具体

I wrote this - very simple - function, and then wondered does VB have some pre-built functionality to do this, but couldn't find anything specific.

Private Shared Function MakeArray(Of T)(ByVal ParamArray args() As T) As T()
    Return args
End Function

与其说要使用像

Dim someNames() as string = MakeArray("Hans", "Luke", "Lia")

由于这是可以做到的。

Dim someNames() as string = {"Hans", "Luke", "Lia"}

但更像

public sub PrintNames(names() as string)
   // print each name
End Sub

PrintNames(MakeArray("Hans", "Luke", "Lia"))

任何想法?

推荐答案

任何理由不这样做:

Dim someNames() as string = New String()("Han", "Luke", "Leia")

唯一的区别是类型推断,至于我可以告诉。

The only difference is type inference, as far as I can tell.

我刚刚检查,VB 9有隐式类型的数组太:

I've just checked, and VB 9 has implicitly typed arrays too:

Dim someNames() as string = { "Han", "Luke", "Leia" }

(这将不会在VB 8工作,据我所知,但明确的版本会。隐含的版本是必要的匿名类型,这也是新VB 9)。

(This wouldn't work in VB 8 as far as I know, but the explicit version would. The implicit version is necessary for anonymous types, which are also new to VB 9.)

这篇关于VB.Net正在初始化对飞阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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