The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Regex Static Methods

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Peter G Provost

Posts: 849
Nickname: pprovost
Registered: Aug, 2003

Peter G Provost is a Solution Architect for Interlink Group in Denver, CO.
Regex Static Methods Posted: Dec 26, 2004 6:45 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Peter G Provost.
Original Post: Regex Static Methods
Feed Title: Peter Provost's Geek Noise
Feed URL: /error.aspx?aspxerrorpath=/Rss.aspx
Feed Description: Technology news, development articles, Microsoft .NET, and other stuff...
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Peter G Provost
Latest Posts From Peter Provost's Geek Noise

Advertisement

In this post, Jason Bock says...

Today a co-worker pointed out the following line in the documentation for Regex:

Using a static method is equivalent to constructing a Regex object, using it once and then destroying it.

Um...what??

Jason then shows a bunch of code that compares a type initializer (aka static constructor) with an instance constructor. He concludes with...

The key point to see is that the type initializer for StaticMethodApproach is only called once. The constructor for InstanceMethodApproach is called every time you invoke CallMe(). In other words, the type is only loaded once, but you get three instances of InstanceMethodApproach. That doesn't seem "equivalent" to me.

Instead of "equivalent", I would go with "analogous", although even that seems to be too strong of a connection between the concepts. Also, I checked the .NET 2.0 docs and it hasn't changed. I don't know, this just doesn't feel like the right wording to me.

I think Jason is missing the point of the documentation. If we take a look back at the Regex Class docs on MSDN, it says this:

The Regex class contains several static methods that allow you to use a regular expression without explicitly creating a Regex object. Using a static method is equivalent to constructing a Regex object, using it once and then destroying it.

This isn't making any statements about type initializers. It also isn't making a general statement about static methods vs. instance methods. It is instead giving you a hint about the implemetation of the these particular static façade methods.

Take the static (Shared for you VB folks) Match method, for example, it has the following signature:

   1:  public static Match Match(
   2:     string input,
   3:     string pattern,
   4:     RegexOptions options
   5:  );

Now combine that with the documentation that says this method is "equivalent to constructing a Regex object, using it once and then destroying it." We can predict that Match will be implemented something like the following code. (A quick experiment with Reflector confirms it.)

   1:  public static Match Match( string input,
   2:                             string pattern,
   3:                             RegexOptions options )
   4:  {
   5:      return new Regex(pattern,options).Match(input);
   6:  }

So I would say that the phrase "is equivalent to" is completely accurate here. Wouldn't you agree?

Read: Regex Static Methods

Topic: "You unlock this door with the key of imagination" Previous Topic   Next Topic Topic: Darwin in full effect

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use