I saw something like this in some graphics libs today, and it seems like it could be very usefull.
In the demo I saw it looked like this:
Texture2D texture = Content.Load<Texture2D>("Textures//Road");
Effect shader = Content.Load<Effect>("Effects//Road");
I assume it’s a function that returns whatever type is defined in the <> brackets, and does a different action for different types.
I want to implement something similar myself, how is it used?
The
Loadmethod can be used to load lots of different types of content, including things like images and sounds. Therefore, it may return instances of different Types (such as yourTexture2dandEffect).There are two ways of creating a method that can do this. First, and most simply, you can return an
Object, which is the base class of every type in the .NET and XNA world.you would have to use it thusly:
The second way is to use generics. With generics, you can handle the cast for the user, making their code simpler. A generic version of this method would be:
You’re still using the old load, but you’re performing the cast (to whatever type that T is) prior to returning the value.
Generics are useful, as you can use them to create type safe code without creating lots and lots of type-specific method versions, like