开发者

return a variable length string array

开发者 https://www.devze.com 2023-01-25 06:13 出处:网络
what\'s the best method to return a variabl开发者_如何学JAVAe length string array I\'d prefer to use a generic collection such as: List<string> (or IList<string>), or IEnumerable<string

what's the best method to return a variabl开发者_如何学JAVAe length string array


I'd prefer to use a generic collection such as: List<string> (or IList<string>), or IEnumerable<string> depending on how you plan to use it. Generic collections are typically easier to work with than arrays, having a much more robust interface.


There is nowhere near enough detail in your question to understand exactly what you are after.

This works though:

string[] GetStringArray(int length) {
    return new string[length];
}

(C# isn't like C, if that is your concern.)


public String[] someFunction(){
    String[] variableLengthStringArray = new String[5]();

    // some logic

    return variableLengthStringArray;
}


In the old .NET 1, there was the StringCollection class. it is still supported, and required in a few places, but I would prefer the generic List collection. Or there's the old C-style string[] as per the first answer.

You don't give any specifics, so everything else being equal, I would go for the generic list.

0

精彩评论

暂无评论...
验证码 换一张
取 消