© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
13 replies
florent

Generics and type inference

    public TDest MapTo<TSource, TDest>(TSource src)
    {
        var res = _mapper.Map<TSource, TDest>(src);
        return res; 
    }
    public TDest MapTo<TSource, TDest>(TSource src)
    {
        var res = _mapper.Map<TSource, TDest>(src);
        return res; 
    }

Why can't the compiler infer my type of TSource? If I call that mehod I always have to specify it like this
Mapper.MapTo<ObjectA, ObjectB>(instance of ObjectA); 
Mapper.MapTo<ObjectA, ObjectB>(instance of ObjectA); 
It should be possible to call it just like this
Mapper.MapTo<ObjectB>(instance of ObjectA);
Mapper.MapTo<ObjectB>(instance of ObjectA);
I am just starting with .NET
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Type Inference strategies?
C#CC# / help
3y ago
❔ Generics with different input and output type
C#CC# / help
4y ago
✅ Enum and generics
C#CC# / help
3y ago