Say I have a class called LogHelper and it has a static method called GetLogger(string name).
Is there a way to add a static extension method that would just be GetLogger()?
I know that normally extension methods are static where they are declared, but is there a way to appear static on the class they are "Helping"?
You could use an extension method:
public static class StringExtensions
{
public static ILogger GetLogger(this string value)
{
...
}
}
And then you could use like this:
string foo = "bar";
ILogger logger = foo.GetLogger();
You can also use
public static ILogger GetLogger(string value)
{
...
}
public static ILogger GetLogger()
{
return GetLogger("Default Value");
}
This way when you called GetLogger() it would call GetLogger(string value) but with your default value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With