Multiple types in a params without casting?

Feel like I'm hoping for an unrealistic expectation, but is it possible to have multiple types in a params without casting to avoid GC? eg...
public void DoSomething(params object[] objs)
{
for (int i = 0; i < objs.Length; i++)
{
if (i % 2 == 0)
Debug.Log((int)objs[i]);
else
Debug.Log((string)objs[i]);
}
}
public void DoSomething(params object[] objs)
{
for (int i = 0; i < objs.Length; i++)
{
if (i % 2 == 0)
Debug.Log((int)objs[i]);
else
Debug.Log((string)objs[i]);
}
}
3 Replies
Becquerel
Becquerel3y ago
you can use generics:
private T MyMethod<T>(params T[] vals)
{
// ...
}
private T MyMethod<T>(params T[] vals)
{
// ...
}
Thinker
Thinker3y ago
That wouldn't allow for multiple types Unless you use object, you can't have an array of multiple types You could make a kind of wrapper type which represents one type or another, but there is no built-in way to do this.
FirstGearGames
FirstGearGamesOP3y ago
okay thanks. I'll probably just eat the cast cost. its not really super performant crucial and shouldn't hurt anything in the long run.
Want results from more Discord servers?
Add your server