- Home /
Creating a List of arguments,Creating a list of args for a function
When creating a List of args to send to a func (say its limited to only 1 arg and i wanna bypass this issue) what should be the type of the args if i want them to be different in type?
i.e
I'm trying to get a generic question that will not need me to predefine a struct for that.
Please, edit your question and give examples of what you are trying to do. Your explanation is not clear.
Right, as the question stands it's not clear what what the method should and how the method should be used.
Answer by bpaynom · Jan 31, 2019 at 01:55 PM
You want this? params keyword You can use it with object
if you wanna it generic
Yes, this is the most generic approach. Though note that this comes with several drawbacks. First of all all value type parameters would be boxed so they generate garbage. Also inside the method you have to check the type of each argument and cast them to the actual types.
Answer by xxmariofer · Jan 31, 2019 at 01:52 PM
what do you mean by a generic question? first of all, you dont need to pass all args to a method since you can give them a default value example:
void MyMethod(int value = 5) { Debug.Log(value); }
when you call the method without args it will print 5 since is the default value.
if you want a generic method that can be used for multiple type of arguments here is the documentation on how to do it https://docs.microsoft.com/es-es/dotnet/csharp/programming-guide/generics/generic-methods
and what i imagine you want to achieve is done with polimorphism, that is one of the pilars of oriented programmming, here is the documentation. in which you would be able to pass the parent class/interfaze and access all his parameters. https://docs.microsoft.com/es-es/dotnet/csharp/programming-guide/classes-and-structs/polymorphism
Your answer
Follow this Question
Related Questions
Type 'Force' does not contain a definition for 'Operation' 1 Answer
Button Delegate running more than once 1 Answer
No overload for method "X" takes `1' arguments 1 Answer
Scriptable Object Tutorial/Use in Inventory System 1 Answer
No overload for method 'ClampMagnitude' takes '3' arguments 2 Answers