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

The Archive Base Latest Questions

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

I have a generic class. It has 2 constructors. Those are widely used in

  • 0

I have a generic class. It has 2 constructors. Those are widely used in my organization’s codebase.

class MyClass<T> {
  MyClass() { ... }
  MyClass(T defaultValue) { ... }
}

I would like to add some functionality but keep backward-compatible. So I would like to introduce a new boolean optional parameter to each constructor:

class MyClass<T> {
  MyClass(bool someFlag = false) { ... }
  MyClass(T defaultValue, bool someFlag = false) { ... }
}

However, I already have a heap of usages out there in which T is boolean and a default value is passed:

class Usage {
  MyClass<bool> Booly = new MyClass<bool>(false);
}

Now, according to the laws of overload preference – the compiler is tying all such constructor usages to the overload accepting someFlag, since typed methods “know better”. While making perfect sense in most cases, this is obviously breaking my backward-compatibility.

My question is simple: Is there a language feature available for me to override the default laws of overload preference, and define the old generic overload as the preferred one, so that I don’t have to change all such usages?

Of course, a drawback of this design is that whenever I would want to call the first overload (with only the someFlag parameter) – I would have to specify a named parameter as per C# 4 specifications.

Suggestions for other designs are also welcome, but please try to answer my question first :).

  • 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-12T15:35:15+00:00Added an answer on June 12, 2026 at 3:35 pm

    Assuming you can have an Initialize() type method that you can call from each constructor, your best bet is to have three constructors:

    MyClass() { Initialize(null, false); }
    MyClass(T default, bool someFlag = false) { Initialize(default, someFlag); }
    MyClass(bool someFlag)
    {
       if (typeof(T) == typeof(bool))  Initialize(someFlag, false);
       else Initialize(null, someFlag);
    }
    
    private Initialize(T default, bool someFlag)
    { 
       // Do whatever
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a generic class class MyClass : MyClass<string> { } //this line
I have a Generic class like this : public class Mapper<TEntity, TDataAccess> where TEntity
I have a wrapper generic class that intended to be used with a set
I have a generic class that needs to limit an enum depending on the
I have a generic class that takes a type T . Within this class
I have a generic class HierarchicalBusinessObject. In the constructor of the class I pass
I have a generic class NamedValue<TValue> : public class NamedValue<TValue> { public string Name
If say I have some generic class, for example: public class Attribute<T> { }
I want to write in Delphi (2009 - so I have generic dictionary class)
I know Java's generics are somewhat inferior to .Net's. I have a generic 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.