C
C#

help

Generics and type inference

Nnobody9/28/2022
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
TThinker9/28/2022
There is no partial inference, you either have to specify all or no generic parameters. And yes this is annoying
Nnobody9/28/2022
Oh that sucks I love using generics to simplify code, anyway thank you for your quick help!
TThinker9/28/2022
np catsip language do you come from btw?
Nnobody9/28/2022
@thinker227 I did most of my work with python/c++ and recently very much with Haskell (loving it) - whats your main language?
TThinker9/28/2022
C# but also doing a bit of Haskell occasionally. Was just wondering what language you were using if you were expecting this kind of partial generic inference. C#'s type system is unfortunately nowhere as sophisticated as Haskell's or what you can do with C++ templates.
Nnobody9/28/2022
Yeah its from my experiences with C++ templates and especially Haskell I just like working with strong typesystems. btw thats why Rust is simply awesome imho
TThinker9/28/2022
Fair point Compared to a lot of other ones, C#'s type system is quite tame.
Nnobody9/28/2022
True in comparsion, but its fine C# has some nice features too like record types which I've just learned about. I'm starting to try build up a github profile with some showcases thats why I try to learn different techs (webAPI in the C# case)

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Crash of the background worker because of the SQL connectionI have a background worker running on some machine, which getting random crashes because of the SQL Minimum need data to create an EntityMy entity: ```cs public class Book : BaseEntity { public string Name { get; set; } MediatR commands and DTOs [Answered]Currently, I am passing DTOs to my controllers, I am using MediatR and it supports commands. I use tHow can I send int value from 1 program, to another via internet?```fix Hello. Is there any easy way to connect 2 programs to each other over the internet? I mean,InterfaceHi, i've this interface but there is only 1 class which doesn't need any parameter in `getAll(long iTroubleshooting Visual studio .net 6, debuggerHi, Ran in to a so strange bug/problem this week. I'm working in a multi tier solution with several✅ Amazon Review Panic!!!Hello, I am a Freshman in college who was recently selected for a technical interview at Amazon. I aSearch for text and sort by most hits.I wrote a function like this, it looks for words in the database. I would like to make it sort by thValidation helpNeed help with some simple validationPrevent scrollbar from jumping down when viewing a list?Hi - I'm making a chat room in windows forms and have a listbox that displays all the messages recieWPF Border around transparent imageI'm trying to do a kind of "magic wand tool" selection that some image editors have, and I just can'Stack and Heap Memory [Answered]I've been correcting some of my misconception about stack and heap memory allocation. So, this is myPartialType from Nest.js in CsharpIs there is a possibility to recreate a behaviour like the partialType from Nestjs (=> https://docs.Trying to add GrossWeight each time it shows up in the listTrying to learn how to use a ForEach loop while doing my homework. I feel like this should be easierHow to read DateTime from SQLite database using System.Data.SQLite?I have a sqlite database with a DateTime entry. Since SQLite doesn't have a DateTime data type, it sBlazor VSCode --unable to find an installation of the browser on your system.-- [Answered]I'm getting an error `Unable to launch browser: "Unable to find an installation of the browser on yoCollection of different classesI have a situation where I need to store two different types together, and I'm not sure what's a gooNuGet behind a proxyHey I am in the process of releasing a .NET project as a Docker container. How can I configure NugetFluentAPI, Joining Table of the same entitiesI have entities User: id, payments; Payments: id, user1, user2 I want user to have n payments, and EF sets a property out of nowhere [Solved] [Answered]```cs public int RepliesLength { get; set; } = 0; public int RepliesCount { get; set; } = 0; public