VB.Net Initialising an array on the fly

前端 未结 5 1653
悲哀的现实
悲哀的现实 2020-12-20 18:30

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          


        
5条回答
  •  無奈伤痛
    2020-12-20 18:55

    Any reason not to do:

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

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

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

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

    (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.)

提交回复
热议问题