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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:30:24+00:00 2026-05-23T20:30:24+00:00

I am passing 20+ arguments of different types to method. There should be some

  • 0

I am passing 20+ arguments of different types to method. There should be some clean way to pass this. Can you please help me.

I can pass array of object having all these 20+ arguments but in target method I have to put checks on type. Is this good way to pass long list of arguments.

Code: Sample from code not full

private DataTable CreateDataTable(string[] args)
{
    DataTable dt = new DataTable();
    dt.Clear();

    foreach (var arg in args)
    {
    dt.Columns.Add(arg);
    }

    return dt;
}

I can pass array in this method because all arguments are of same type.

DataTable dt = CreateDataTable(new string[] { "ProjectId", 
                        "ParentProjectId", 
                        "ProjectName", 
                        "CreationDate");

Now here I have more than 20+ values of diff types like following

int projectId = 100;
int? parentProjectId = SomeCondition ? 10 : null;
string projectName = "Good Project"
DateTime createdDate = DateTime.Now;
.
.
.

In this method I would assign values to columns.

AssignValuesToDataTable(dt, Arguments list ......)
// Implementation would be like this. Here I am passing 20+ arguments.
private DataTable AssignValuesToDataTable(DataTable dt, arguments list ........)
{
    DataRow row = dt.NewRow();
    row["ProjectId"] = projectId;
    .
    .
    .

    dt.Rows.Add(row);
}

Can you please help. I am using C#4

EDIT:
Above code is an example from my real code but I am more interesting to know that what is best method to achieve this.

From Coding Horror (Jeff Atwood)

The more parameters a method has, the more complex it is. Limit the
number of parameters you need in a given method, or use an object to
combine the parameters.

Above quote is from this blog post.
Code Smells

Thanks.

  • 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-23T20:30:25+00:00Added an answer on May 23, 2026 at 8:30 pm

    Pass an object that represents the data instead:

    private DataTable AssignValuesToDataTable(DateTable dt, Project project)
    {
         row["ProjectId"] = project.Id;
         row["ProjectName"] = project.Name;
         ...
    }
    

    with

    public class Project
    {
        public int Id {get;set;}
        public string Name {get;set;}
        ...
    }
    

    of course, then the question becomes : why use DataTable at all? since a Project class is a much better metaphor / mechanism for expressing that data, and List<Project> (or BindingList<Project>) is ideal for a collection of such.

    (hint: I very, very, very rarely use DataTable – or maybe less frequently than that)

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

Sidebar

Related Questions

StructureMap doesn't like passing in Nullable types as constructor arguments. Is there a reason
How do you decide between passing arguments to a method versus simply declaring them
I understand that I can specify system properties to Tomcat by passing arguments with
I'm passing some numeric arguments while creating a process (in VC++) I'm stuck at
How can one call a ColdFusion function, passing in attribute values as arguments, inside
My problem is this. I have lists of different numeric types, for example: QList<qreal>
Is there a reason why I can use a generic function with different type
I am trying to create multiple shortcuts to my application that pass different arguments
In Perl, is there any way to tell which .pl script has initialized this
It seems like g++ ignores difference in array sizes when passing arrays as arguments.

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.