1

Sorry in advance if this is already answered (I couldn't find it on SO). We have published a .NET SDK library which exposes one method with generic constraints. My objective is to overload this method and expose another method with different generic constraints.

// This already exists in the SDK.
public T UpdateConfiguration<T>(T entity) where T : ISystemConfig
{
   ...
}

// This is our objective. Expose another method for users.
public T UpdateConfiguration<T>(T entity) where T : IDataConfig
{
   ...
}

Apparently, this isn't allowed in C# (.NET 4.7.2). The compiler raises error: A member UpdateConfiguration<T>(T) is already defined. Rename this member or use different parameter types.

Coming from a Java background, I was surprised by this error. Although I'm overloading this method, the parameters/arguments are constrained by a different type.

Here are the code samples to show what I mean. This is allowed in Java but disallowed in C#.


What's the best way to handle this in terms of best engineering practices? I don't want to rename the method as UpdateConfiguration2 since we want to maintain some uniformity in our SDK for consumers.

As the compiler suggests, is changing the Argument type the only solution?

T UpdateConfiguration(ISystemConfig entity) & T UpdateConfiguration(IDataConfig entity)?

The second option will revoke some of the advantages of type inference. Currently, UpdateConfiguration(SomeType obj) would infer T as SomeType. By changing the arguments (second option), I'll have to do UpdateConfiguration<SomeType>(SomeType obj) every time to infer T.

Also, any logical reason why this feature isn't supported in C#?

Adithya Upadhya
  • 2,239
  • 20
  • 28
  • 1
    If you go the second option, why would you keep T then? `ISystemConfig UpdateConfiguration(ISystemConfig entity)` will be fine. Also, Eric Lippert explains why features aren't implemented https://stackoverflow.com/a/8673015/7484766 – JohanP Sep 22 '20 at 00:12
  • 2
    Which method would you expect to be called if you passed an `entity` that implements both `ISystemConfig` and `IDataConfig`? – devNull Sep 22 '20 at 00:17
  • @JohanP the idea is that since `ISystemConfig` and `IDataConfig` are interfaces, the consumers will pass inherited class objects and after update, we return the same class object. If we don't return T and instead return the interface, the consumers will have to keep typecasting the returned object back to the class type. It's hard to explain without the entire code base, I hope you understand. – Adithya Upadhya Sep 22 '20 at 00:22
  • https://stackoverflow.com/questions/23359262/c-sharp-overloading-with-generic-constraints – Jeremy Lakeman Sep 22 '20 at 00:33
  • @devNull what you asked me is a University level freshman exam question. If the compiler detects any ambiguity in the passed argument, the compilation itself will fail. This goes for any set of overloaded methods and it isn't related to this question. For your reference: https://ideone.com/LILOUx. – Adithya Upadhya Sep 22 '20 at 00:34
  • @AdithyaUpadhya Sorry if that offended you. Just asking for clarification on what you're expecting. – devNull Sep 22 '20 at 00:40
  • @JeremyLakeman Thank you for sharing the question. The shared question does answer "Why" but I'm still unclear about the best way to deal with this problem. Don't you think this deserves an answer before being closed? If you read my post again, I asked how to best handle this case and why doesn't C# support this feature. – Adithya Upadhya Sep 22 '20 at 00:52
  • "What is the best" is opinion based anyway. "best" in what way? Rather than closing this as opinion based, I thought I would rather link this to an existing post, which will be more helpful to future visitors. – Sweeper Sep 22 '20 at 00:58
  • Answer; you can't create two methods like that on the same class. Though you could have ISystemConfig & IDataConfig extend the same base interface... – Jeremy Lakeman Sep 22 '20 at 01:09
  • @Sweeper What would you do if you were in the same situation? I hope I provided enough context in the question. I'm just trying to see if there's any other alternative that I didn't consider. If so, it's a good learning experience. – Adithya Upadhya Sep 22 '20 at 02:51

0 Answers0