Domanda

9 times out of 10, when I want to use the Substring() method on a string, it's because I want to shave some characters off the END of the string, not the start. While the default Substring does have support for this, it does so by taking a second parameter which is the length of the desired string, rather than the endpoint. This means if I consistently want to shave off N characters off of a series of strings of differing length, I have to do an extra step, which can result in a good deal more code. Take for example:

//Shaving the first N chars
string trimmed = foo.bar.widget.GetType().Name.Substring(N);

vs.

//Shaving the last N chars
string trimmed = foo.bar.widget.GetType().Name.Substring(0, foo.bar.widget.GetType().Name.Length - N);

or maybe to save the extra function call, use a second line:

string name = foo.bar.widget.GetType().Name;
string trimmed = name.Substring(0, name.Length - N);

Either way, you're basically doubling the amount of code necessary to shave characters off the end of the string rather than the beginning. My modification would be simple. If you pass a negative N (which would otherwise be meaningless), it would shave -N characters off the end of the string instead of the beginning. I can already code this up with an extension method:

public static string MySubstring(this string str, int val)
{
    return (val > 0) ? str.Substring(val) : str.Substring(0, str.Length + val);
}

And then when I want to shave off the final N chars, I just do:

string trimmed = foo.bar.widget.GetType().Name.MySubstring(-N);

Short and sweet, just like shaving off the beginning characters. My question is - would it be possible to override the behavior of the default Substring() function so that I can do this without having to use my own unique name for the function? It's not like it would invalidate any existing code, because previously there was no reason to pass it a negative number, and doing so would simply throw an exception and crash. This is just one of those simple no-nonsense features that feels like it should've been part of the implementation to begin with.

È stato utile?

Soluzione

According to C# documentation, you can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. So the answer is "No".

Arguably, this is a good thing™, because otherwise your code would become a nightmare to read to someone not familiar with your extension.

Note: str.Substring(0, str.Length + val); can be replaced with str.Remove(str.Length + val)

Altri suggerimenti

You can't override a method on string in the strict sense using extension methods, as the compiler will always choose an instance method over an extension method with the same signature when compiling a method call. However, you can achieve something close to what you want using named arguments. This should also help avoid readability issues. Here's an example

public static string Substring(this string @this, int trimFromEnd)
{
    return @this.Substring(0, @this.Length - trimFromEnd);
}

// if you do
"abc".Substring(1) -> returns "bc"

// if you do
"abc".Substring(trimFromEnd: 1) -> returns "ab"

Personally, I find this a bit more readable than Substring(-1) or just Substring(varName), where varName happens to be negative.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top