C # regex difference between .Match (). Value and Match (). ToString ()

I have the following code:

Match matcher = new Regex("[0-9]+.[0-9]+.[0-9]+").Match("12/02/1994");

if (matcher.Success)
{
   string matchedString1 = matcher.Value;
   string matchedString2 = matcher.ToString();
}

      

In this case, matchedString1

and matchedString2

contain the same value "12/02/1994"

. Are matcher.Value

and matcher.ToString()

always return the same results for any regex?

+3


source to share


2 answers


The Match class derives from the Group class, and this result comes from the Capture class.

The Capture class overrides the ToString () method with this code:



[__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public override string ToString()
{
    return this.Value;
}

      

so, yes, it's the same meaning.

+5


source


From MSDN;

Capture.Value

Property

Gets the captured substring from the input string.

Capture.ToString()

Method.



Retrieves the captured substring from the input string by calling Value .

Even if we look at .NET Reflector, we can see its Capture

class override the method ToString()

like this:

[__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public override string ToString()
{
    return this.Value;
}

      

So yes. They are of equal value.

+1


source







All Articles