Scott Hanselman

.NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

April 01, 2009 Comment on this post [16] Posted in ASP.NET | DLR | Javascript | Learning .NET | Microsoft | Musings | Open Source | PHP | Programming | Python | Silverlight | Source Code | Tools | VB | Web Services | Windows Client | XML
Sponsored By

As web programmers, we use a lot of strings to move data around the web. Often we’ll use a string to represent a date or an integer or a boolean. Basically "1" in instead of 1, or "April 1, 2009" rather than a proper ISO-8601 formatted culture-invariant date.

While these strings are flying around via HTTP it's not a huge deal, but sometimes this loose, even sloppy, use of strings can leak into our own code. We might find ourselves leaving the data times as strings longer and longer, or not even bothering to convert them to their proper type at all. This problem is made worse by the proliferation of JSON, and schema-less/namespace-less XML (that I've often called "angle-bracket delimited files" as they're no more useful than CSVs in that point.

.NET 4.0 is pretty much locked down, but version 4.1 still has some really cool "Futures" features that are being argued about. If we don't know the type of a string, or we want to leave the string, as a string, longer than usual, what if we had an class that could be both a string and another type, essentially deferring the decision until the variable is observed. For example:

StringOr<int> userInput= GetUserInput("Quantity"); 
string szUserInput=userInput.StringValue; 
int intUserInput=userInput.OtherValue;

Sometimes you just don't know, or can't know.

This reminds me of a similar, but orthogonal physics concept, that of the Heisenberg Uncertainty Principle. Sometimes you know that an object is a string, and sometimes you know how long it is, but you can’t know both at the same time.

One of my favorite jokes goes:

Heisenberg gets pulled over by the police. The officer asks, “Do you know how fast you were going?” Heisenberg answers, “No, but I know exactly where I am!”

This library doesn't solve THAT issue, with respect to strings, but we’ve got folks in DevDiv working on this and many other metaphysical - and physical - problems as they apply to computer science.

Big thanks to Eilon, who's working hard to get this pushed into the .NET 4.1 Base Class Library. Visit Eilon's blog for more details on this new library, more code, graphics and details on how Intellisense will handle this new case.

Hopefully, someone is working to make this important new library Open Source.

Your thoughts, Dear Reader?

Related Posts

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
April 01, 2009 12:02
Oh, is it like generic?
April 01, 2009 12:03
No, it is like April the 1st.
April 01, 2009 12:43
It didn't occur to me that this might be an April Fool's until I read "make this important new library Open Source". I'm now questioning my sanity for thinking this might be genuinely useful.
April 01, 2009 13:15
After reading the comments, I felt so duh
April 01, 2009 13:15
Version 4.1, as in April 1st :)
April 01, 2009 13:34
Excellent stuff Scott, I've got a few changes of my own to propose. I've posted about them on my blog:
April 01, 2009 14:42
i know (or think at least) you're kidding but this might actually be somewhat useful :P you could have a TryGetValue that returns a bool and if its false you have the original value to display in validation errors ;)

perhaps not needed in the core framwork though ;)
April 01, 2009 14:51
While you're at it, please suggest to mr Hejlsberg a couple of language extensions too; maybebreak, maybereturn and maybebreakorreturn. These would complement the normal break and return keywords but by introducing a measure of unpredictability; let the runtime decide what to do...
April 01, 2009 15:28
I know this is entirely offtopic but I figured your most recent blogpost was the easiest way to get your attention.

Could you please make the code for your BabySmash with MEF project publicly available (i.e. via CodePlex etc.)?

Thanks in advance!
April 01, 2009 16:55
It's a good thing you don't do comedy for leaving hum? keep up with the outstanding IT work !!!!
April 01, 2009 17:30

Babysmash source code is available, see
April 01, 2009 17:39
Oh my god, they're organizing against us.
April 01, 2009 18:30
Funny prank ;)
April 02, 2009 19:11
I don't see this as far-fetched or silly except for the tone of the post.
Isn't this the basic concept of Nullable<int>? It's an int or maybe it's not an int. You defer the determination of the type until later. As useful as int? is, I see it as a C-style union struct where you're ramming two unrelated types into one thing. Isn't the essense of Nullable<int> the same as StringOr<int> (or Stringable<int>)?
Please note that I am advocating such a thing. My comparison of Nullable<> to unions was not a complement.
April 06, 2009 3:57
A sign oustide a German bed and breakfast reads "Heisenberg May Have Slept Here".
April 08, 2009 2:27
"If we don't know the type of a string, or we want to leave the string, as a string, longer than usual,"

This doesn't make any sense. Great post Scott, you really had us going.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.