Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8634181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:39:35+00:00 2026-06-12T09:39:35+00:00

I have a program which has various unit conversions defined in a configuration file

  • 0

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#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:39:37+00:00Added an answer on June 12, 2026 at 9:39 am

    String.Format can take an array of objects, and you can get an array of objects from a List with yourList.Cast<object>().ToArray().

    List<float> a = new List<float>();
    a.Add(1.0f);
    a.Add(2.0f);
    
    string fmt = "{0} ft. {1} in.";
    Console.WriteLine(String.Format(fmt, a.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#):

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static object[] ToObjectArray(List<float> a)
            {
                object[] b = new object[a.Count];
                for (int i = 0; i < (a.Count); i++)
                    b[i] = (object)a[i];
                return b;
            }
    
            static void Main(string[] args)
            {
                List<float> a = new List<float>();
                a.Add(1.0f);
                a.Add(2.0f);
    
                string fmt = "{0} ft. {1} in.";
                Console.WriteLine(String.Format(fmt, ToObjectArray(a)));
    
                Console.ReadLine();
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C program which has a function call that is defined in
I have a program which has a text box that the user can edit.
I have a c++ program which has many many functions and I have different
I have a C program which has always used hard coded define statements for
I have a .NET 2.0 program that has a reference to Interop.WIA.dll (.NET), which
I have written a program which analyzes a project's source code and reports various
I have an android program. It has a button which when clicked is supposed
My program has to read files that use various encodings. They may be ANSI,
I have a program which creates JButtons which are then added to a JPanel
I have a program which dynamically generates a GUI. I don't know how many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.