I have a program which has various unit conversions defined in a configuration file including string formats for how they should be displayed. To display metres, centimetres, etc. only one input is required for the format, however for feet and inches two inputs are required (and potentially more could be needed for other unit conversions).
I know the number of arguments at run time from the length of a List<> generic containing input values. Can I then somehow input this into a C# String.Format?
Clarification with an example:
So the format defined in the configuration could be "{0} ft. {1} in." in the case of feet and inches or "{0} cm." in the case of centimetres. The code generates one or two values accordingly and puts them in a List<float> generic container. I then need to get the code into a String.Format statement, however String.Format does not take List<float> as an argument (as far as I am aware).
I know Python much better than C# and thanks to its dynamic nature I can unpack arguments in a list using the asterisk. i.e. my_format.format(*args_list) where my_format is a format string.
Is there anything similar in C#?
String.Format can take an array of objects, and you can get an array of objects from a List with
yourList.Cast<object>().ToArray().Or if you’re using .NET 2.0 (and there may be a neater way to do this; I don’t know much about C#):