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 851309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:24:35+00:00 2026-05-15T07:24:35+00:00

Given the following method: (real method has a few more parameters, but the important

  • 0

Given the following method: (real method has a few more parameters, but the important ones are below…)

public string DoSomething(string formatter, params string[] values)
{
    // Do something eventually involving a call to String.Format(formatter, values);
}

Is there a way to tell if my values array has enough objects in it to cover the formatter, so that I can throw an exception if there aren’t (short of doing the string.Format; that isn’t an option until the end due to some lambda conversions)?

  • 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-05-15T07:24:36+00:00Added an answer on May 15, 2026 at 7:24 am

    I’m still not clear why you think you cannot use string.Format to test it. If the passed in formatter is supposed to have placeholders for the items in values, then you should be able to do this:

    static void TestFormat(string formatter, params string[] values)
    {
        try
        {
            string.Format(formatter, values);
        }
        catch (FormatException e)
        {
            throw new Exception("the format is bad!!", e);
        }
    }
    

    Sample Usage:

            TestFormat("{0}{1}{2}", "a", "b", "c"); // works ok
            TestFormat("{0}{1}{2}", "a", "b"); // throws exception
            TestFormat("{0}{1}{2}}0{", "a", "b", "c"); // throws exception
    

    Trying to do this with a regular expression is going to be tough, because what about something like this:

    "{0}, {1}, {abc}, {1a4} {5} }{"
    

    {abc} and {1a4} aren’t valid for string.Format, and you also have to determine for each valid number ({0}, {1}, {5}) that you have at least that many arguments. Also, the }{ will cause string.Format to fail as well.

    I just used the former approach described above in a recent project and it worked great.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 454k
  • Answers 454k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You defined the properties for the Privatescategory, but you are… May 15, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer Look up Catmull-Rom splines. Here's an introduction. Further reading: Catmull-Rom… May 15, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer It executes on the main thread on the next iteration… May 15, 2026 at 9:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.