I'm fairly new to generics and I'm having some trouble understanding parts of how they work and also failing to get it to work the way I want.
So far, I have this;
public interface IObjectMapper
{
T MapObject<T>() where T : new();
}
public class CustomObjectMapper : IObjectMapper
{
T IObjectMapper.MapObject<T>()
{
T retObject = new T();
//do stuff......
}
}
This works fine, but I don't understand what "where T : new()" is doing. Can someone please explain? Also, for my second question - I want a second method called DemapObject that takes 2 parameters, the same generic type T from object and then a different generic type of U - U should also be the return type.
public interface IObjectMapper
{
T MapObject<T>() where T : new();
U DemapObject<T, U>() ??????? what goes here?
}
Finally, once I get the interface method for DemapObject done - how is it called from the implementation class?
public class CustomObjectMapper : IObjectMapper
{
NameValueCollection IObjectMapper.DempaObject<T, NameValueCollection>()
{
//is this right????????????
}
}