Passing a constant array to a function in VB.NET

前端 未结 4 547
悲哀的现实
悲哀的现实 2021-01-18 11:30

I know that you can easily pass an array to a function, like the code below shows

Private Sub SomeFunction(ByVal PassedArray() As String)
    For i As Intege         


        
4条回答
  •  无人及你
    2021-01-18 11:48

    No; there is no such thing as a constant array in CLI; arrays are always mutable. Perhaps a ReadOnlyCollection would be suitable?

    In C# (so presumably similar in VB) you can do something like:

    private readonly static ReadOnlyCollection fixedStrings
        = new ReadOnlyCollection(
            new string[] { "apple", "banana", "tomato", "orange" });
    

    Which gives you a static (=shared) non-editable, re-usable collection. This works especially well if the method accepts IList, IEnumerable, etc (rather than an array, T[]).

提交回复
热议问题