C
C#2y ago
Hugh

✅ When calling a generic function, how can I tell it that I'm going to be passing null?

I've got a generic function which is something like this:
public ReturnType GetValue<ReturnType, ContentType>(ContentType content);
public ReturnType GetValue<ReturnType, ContentType>(ContentType content);
I always want to call it with a ReturnType specified, but sometimes I want to pass in null as the content. How can I call this to say that I want to pass in null? Ideally, I'd like to be able to do something like this:
string result = GetValue<string, NullType>(null);
string result = GetValue<string, NullType>(null);
Or even:
string result = GetValue<string, NullType>();
string result = GetValue<string, NullType>();
This second one is possible if I pick any type as the second generic type and change the function definition to this
public ReturnType GetValue<ReturnType, ContentType>(ContentType? content = null);
public ReturnType GetValue<ReturnType, ContentType>(ContentType? content = null);
However, I don't like putting a valid type in there if that's not what I mean, as that will be confusing to future developers (including future myself) Thanks
2 Replies
ero
ero2y ago
you need to specify multiple overloads for that, i think. passing in null isn't exactly a good pattern (at least in my opinion) for that, i'd just have a second, empty overload
public TReturn GetValue<TContent, TReturn>(TContent? content);
public TReturn GetValue<TReturn>();
public TReturn GetValue<TContent, TReturn>(TContent? content);
public TReturn GetValue<TReturn>();
Hugh
Hugh2y ago
Thanks - that's really helpful - I didn't realise that I could do overloads like that
Want results from more Discord servers?
Add your server
More Posts
❔ WPF Textbox decimal onlyHello, i'd like to know how we are supposed to configure our textbox in order to accept only decimalmaui deserialize problemI am having a problem when trying to deserialize api data ```C# string content = await response.C❔ Log from helper class without creating new context? (Serilog)I have a bunch of services with their own dedicated loggers that have config'd settings for filterin❔ Swagger Open API not showing Request examples in the UII've written an Azure Function with Open API in C#, but when I open up the Swagger/UI to test my AzuLooking solution for get query from _dbContext.SaveChangesAsync() for save logs and easier debuggingHello, i have a .net core 6.0 app that save entites in MSSQL database with EfCORE 6.0 so my function❔ I'm building an MVC core webshop. How should I be structuring controllers, actions and views?So, right now, I have one single view, and this view renders both information about the product and ❔ How to make an object follows my mouse?so I am new at programming and trying to make a top down shooting games in windows form app. My lect❔ Is this the right way to implement an Interface```csharp interface IObstacles { void Draw(Graphics g); } class Redbox : IO❔ Is there any tool converting csv file to Class (seeding data)?I am using EF to seed data and I have an excel file containing like 500 records. If that's around 1❔ Is it possible to have WPF window hidden until Blazor WebView loads page?I'm trying to hide a WPF window until the Blazor WebView loads, but it seems that the initialization