Using this simple example where :
void SomeMethod(int varA, string varB) {}
void SomeMethod(List<int> varA, string varB) {}
void SomeMethod(int varA, List<string> varB) {}
void SomeMethod(List<int> varA, List<string> varB) {}
this would grow in n*n and i feel there must be a better way. one of my ideas would be to make it generic as in the following
void SomeMethod<T1,T2>(T1 varA, T2 varB)
where T1 in { int, List<int> }
where T2 in { string, List<string> }
{
if(varA is int intA)
//do int actions
else
//do List<int> actions
//...
}
I have found this one similar question (C# generics - Can I make T be from one of two choices?), but the answers there don't apply here
Any ideas ?
EDIT: I am simplifying here for simplicity sake, the objective would be to check if the param is a value or a collection of values, and if it is a simple value, turn into a collection with a single value and pass it to another overload that takes a collection, @charlieface in the comments has the idea, but it will take multiple params, in which each can be simple value T or a collection of T.
A js working example of what i pretend to achieve in c# below
var nonArrayToArray = (someValue) => Array.isArray(someValue) ?
someValue : [someValue];
var doSomething = (valueCanBeListOnObject, valueCanBeListOnObject2,
valueCanBeListOnObject3) => {
const guaranteedList = nonArrayToArray(valueCanBeListOnObject);
const guaranteedList2 = nonArrayToArray(valueCanBeListOnObject2);
const guaranteedList3 = nonArrayToArray(valueCanBeListOnObject3);
doSomethingOverload(guaranteedList, guaranteedList2, guaranteedList3);
}
var doSomethingOverload = (guaranteedList, guaranteedList2, guaranteedList3) => {
[...guaranteedList, ...guaranteedList2, ...guaranteedList3].forEach(v => console.log(v))
}
doSomething('someString',['someString'],1);