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

  • SEARCH
  • Home
  • 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 8162515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:44:21+00:00 2026-06-06T18:44:21+00:00

That is, I have a method such as the following: public static int CreateTaskGroup(string

  • 0

That is, I have a method such as the following:

public static int CreateTaskGroup(string TaskGroupName, 
    string Market = "en-us", string Project = "MyProject", 
    string Team = "DefaultTeam", string SatelliteID="abc");

I would like to call this method from the command line, by reading the standard array of command line arguments. The obvious way to do it would be as follows:

 if (args.Length == 1) CreateTaskGroup(args[0]);
 if (args.Length == 2) CreateTaskGroup(args[0], args[1]);
 if (args.Length == 3) CreateTaskGroup(args[0], args[1], args[2]);

Is it possible to do this in a more concise way?

  • 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-06T18:44:24+00:00Added an answer on June 6, 2026 at 6:44 pm

    Here’s one alternative, with the downside that you have to redeclare the default value constants:

    CreateTaskGroup(
        args[0],
        args.ElementAtOrDefault(1) ?? "en-us",
        args.ElementAtOrDefault(2) ?? "MyProject",
        args.ElementAtOrDefault(3) ?? "DefaultTeam",
        args.ElementAtOrDefault(4) ?? "abc");
    

    You can reduce this issue by declaring the strings as consts, e.g.:

    public const string MarketDefault = "en-us";
    public static int CreateTaskGroup(string TaskGroupName,
        string Market = MarketDefault, ...)
    
    static void Main(string[] args)
    {
        CreateTaskGroup(
            args[0],
            args.ElementAtOrDefault(1) ?? MarketDefault,
            ...);
    }
    

    But then it’s not guaranteed by the compiler, nor overtly obvious, that MarketDefault is, in fact, still (code can be refactored in the future) the default for Market.

    Edit: Here’s an alternate solution, using reflection:

    var argsForMethod = new List<string>(args);
    var m = typeof(Program).GetMethod("CreateTaskGroup");
    foreach (var p in m.GetParameters().Skip(args.Length))
        if (p.Attributes.HasFlag(ParameterAttributes.HasDefault))
            argsForMethod.Add((string)p.DefaultValue);
        else
            throw new NotImplementedException();
    var result = (int)m.Invoke(null, argsForMethod.ToArray());
    

    This can be a bit hard to read, and won’t be too fast, but it does what you asked, without resorting to repetitive code, or having any uncertainty as to the default value of the parameters. You’ll probably want to add some error handling for too few or too many parameters. I prefer this solution.

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

Sidebar

Related Questions

When you have a method such as the following: public synchronized void addOne() {
I have the following Method public static List<T> MergeAndSort<T>(List<List<T>> listOfLists) where T : Foo
I have two Arrays that are initialized as such: public static double[] arrayweight= new
I have this following interface : public interface FruitDetails { public String getFruitName(); }
I have the following function: public class InfrStadium { private InfrStadium(int iTeamId) { _teamId
I have the following code and an input file that has all numbers such
I have the following test method: [TestMethod] public void TestHarvestMethod() { HarvestTargetTimeRangeUTC time =
I've got some 3rd party beans that have method signatures that fit quite well
I saw DatabaseErrorHandler interface that have onCorruption method , this interface can be passed
Hi I want to create a WCF service that have login method, which is

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.