I want to have a class in which there is a variable. This variable can be of multiple types, depending on the instance. If in that instance it's initialised as an int, it's an int, if it's initialised as a string, it's a string.
This is what I currently have:
class bar
{
public <???> foo;
}
CodePudding user response:
These problems can be solved using Generics, with Generics you're able to specify the type of your attributes when you're creating the object, for your problem this is a possible solution
public class Bar<T>
{
public T foo;
}
And here is the Generics' documentation,https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/generics