Is it possible to add an additional static method to .Net string
class so I could write:
var header = string.FormatHeader(str1,str2,str3,formatOption);
Is it possible to add an additional static method to .Net string
class so I could write:
var header = string.FormatHeader(str1,str2,str3,formatOption);
TLDR:
No.
Bit more:
Extension methods must recieve an instance to work on:
void static Foo(this string s)
{
// Do something
}
There is no syntax for just off the string.
No, you can't add new static methods to the string class. You'd be better off writing your own StringUtils class or HeaderUtils class or something if there's no logical class for it to be a member of.
No it's not possible, extension methods are just syntactic sugar. It will be converted by the compiler to something like StringExtensions.FormatHeader(..);
. The best you can do here is to create something like a helper class to handle this for you.
public class StringHelper
{
public static string FormatHeader(string str1, string str2, string str3, FormatOption formatOption)
{
throw new NotImplementedException();
}
}