Define a generic variable without defining the type

3

I'm doing some tests on Unity for me to practice programming. I'm a beginner and this question can be kind of stupid. I want to know if this is possible:

I created the following abstract class:

public abstract class Variable<T> : ScriptableObject {}

And I want to use this class as a variable that will accept children of that class:

private Variable<Int> variable;

But I want the variable to accept children of any kind and not just declared something like:

private Variable variable;

Is this possible?

    
asked by anonymous 22.07.2018 / 22:34

2 answers

1

So you do not want to use generics . This mechanism is to have a specific type.

In some cases you could use a object which is the type of all types. I would avoid this, it is almost never fitting and is probably making a poor choice of how to architect your application.

You may still need to change the type at runtime, you should use dynamic , but I would avoid it even more .

If you do not know how to use these mechanisms very well, the application will become unstable.

It would be good to understand typing .

It seems to me that this inheritance is also wrong, but I can not say just what you have in the question.

    
22.07.2018 / 22:47
0

Dude, in that case, you can use inheritance, with a superclass or an interface, and as you identify the common needs, you assign the interface / SuperClass.

I think it can help, another option is the same object, since it is the basis of the types.

I hope to have helped, if I spoke foolishly I'm sorry!

    
23.07.2018 / 20:47