Skip to main content
gamedev.net
🔒 Locked

[.net] About Generic Methods

Started by oggs91 Aug 25, 2011 at 4:05 PM 4 replies 1.1k views
Original Post
oggs91
oggs91
I try to write a content management system for the sake of learning c#
Because it feels right, i tried to make a generic Load() function for my content manager

after some trial and error i found out, i can't instanciate a generic type, and i also can't call static methods of generic types
i googled around and found this solution, it works as i want ... but please tell me what i'm doing here xD
what means the "new()" ?!?!


public T Load<T>(string name) where T:Resource, new()
{
T resource = new T();
resource.Load(name);
return resource;
}
Nik02
Nik02
The part after the "where" clause states that the templated type this method accepts must inherit or derive from Resource class or interface, and that it must have an parameterless constructor (the new() part). The implementation of the method in your post requires that these two criterias are filled - the object is initialized with its default constructor and uses the non-templated Load method which it inherits from Resource base class or interface.

See "c# where clause" in MSDN for all possible uses.

EDIT: changed "empty" to "parameterless" to clarify - the constructor implementation can of course do what it needs to do.
Niko Suni
oggs91
oggs91

The part after the "where" clause states that the templated type this method accepts must inherit or derive from Resource class or interface, and that it must have an parameterless constructor (the new() part). The implementation of the method in your post requires that these two criterias are filled - the object is initialized with its default constructor and uses the non-templated Load method which it inherits from Resource base class or interface.

See "c# where clause" in MSDN for all possible uses.

EDIT: changed "empty" to "parameterless" to clarify - the constructor implementation can of course do what it needs to do.


thx, that's what i've thought too ... why doesn't it work if i add the constructor to the Resource interface and remove the new from the where? that's what i've tried before
Nik02
Nik02
Without the constraint, there is no guarantee that the supplied type actually has a parameterless constructor (it is possible to mask out the default constructor of a base class in at least two ways). You have to take into account that the type could come from places other than the module in which the method is defined; not just your own code where you could implement the requirement loosely.
Niko Suni
oggs91
oggs91

it is possible to mask out the default constructor of a base class in at least two ways


thx again
how could this be done ?
Nik02
Nik02
Way 1: define a constructor with parameters on the derived class, but omit the parameterless one. The derived class doesn't get the empty ctor from the base automatically, but the default constructor of the derived class would call the parameterless base ctor automatically if the derived class' ctor were actually generated. The definition of a parametrized constructor prevents the default ctor generation.

Way 2: create a dynamic class (with reflection) that derives from the base class. It doesn't inherit the base class ctor at all (though it has been a while since I tried this, this may have changed).

I don't remember whether or not it is the language's (c#) or CIL trait to generate the default ctor if no others exist. If other languages would not generate it, case two would apply there too.
Niko Suni

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.