C
C#

help

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

HHugh12/13/2022
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
Eero12/13/2022
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>();
HHugh12/13/2022
Thanks - that's really helpful - I didn't realise that I could do overloads like that

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended 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 Azu❔ 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❔ SQLite in-memory Database read from ReadOnlyMemory bytesI'm receiving an SQLite database payload from an AMQP-0-9-1 message in the form of `ReadOnlyMemory<b