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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:43:33+00:00 2026-05-12T05:43:33+00:00

I have a class with overloaded constructor (C#) It can be initialized in few

  • 0

I have a class with overloaded constructor (C#) It can be initialized in few ways, and some parameters are optional – so in result – there is a confusing bunch of constructors

new Object(StrA, StrB, ObjA)
new Object(StrA, StgB, ObjB, StrC)
new Object(StrA, StrB, ObjA, StrD)
new Object(StrA, StrB, ObjB, StrC, StrD)
new Object(StrA, StrB, StrE, ObjA)
new Object(StrA, StrB, StrE, ObjB)
new Object(StrA, StrB, StrE, ObjA, StrC)
new Object(StrA, StrB, StrE, ObjB, StrC, StrD)

I see a two ways to improve situation
a) create a structure to hold optional parameters

new Config(StrA, StrB, StrD, StrE) 
new Object(Config, ObjA)
new Object(Config, ObjB, StrC)

b) set optional parameters as properties

A = new Object(ObjA)
A.StrA = some;
A.StrB = some;
A.StrD = some;
A.StrE = some;

Which is the best way?

And is such code refactoring necessary – the previous code maintainer says that “while intellisense used, complexity of constructor doesn’t matter – it always possible to check tips and select the correct one”

  • 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-12T05:43:33+00:00Added an answer on May 12, 2026 at 5:43 am

    Yes, I would refactor this. IntelliSense is only so much helpful, and staring at a 5-argument constructor which takes 3 seemingly random strings trying to figure out which one means what is not going to improve code readability any. In VB (or C# 4.0), I’d still go with constructor, but use named arguments. For C# 3.0, I’d make a separate class to hold the initialization info:

    class ObjectSettings
    {
        public string StrA { get; set; }
        public string StrB { get; set; }
        ...
        public string ObjA { get; set; }
        public string ObjB { get; set; }
    }
    

    and then take a single argument of that type in constructor:

    class Object
    {
        public Object(ObjectSettings settings);
    }
    

    and use object initializer when invoking it:

    new Object(new ObjectSettings { StrA = ..., StrB = ..., ObjA = ... })
    

    The main advantage of this compared to just having properties in Object is that this pattern guarantees that Object will be properly initialized as soon as it is constructed. With properties on object itself, it will be in invalid state until client sets them all correctly, and you’ll have to validate that on pretty much every call.

    This pattern is actually used in .NET FCL – see XmlReader / XmlReaderSettings for an example of it.

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

Sidebar

Related Questions

I have a class with both a overloaded constructor and a overloaded method. If
I'm trying to have an overloaded constructor for a class. I think this should
I have class method that returns a list of employees that I can iterate
In Java is there anyway to have one constructor that will accept an array
I have a class which has a constructor that takes a const char* .
Suppose I have a class with overloaded operator new . class Class { public:
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class with a member function that takes a default argument. struct Class

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.