LINQ Has Changed Me

In the old days, the post-colonial, pre-LINQ days of yore, I’d have written a one-way MD5 encryption like so:

static string GetMD5String(string s) {
  MD5 md5 = new MD5CryptoServiceProvider();
  byte[] hash = md5.ComputeHash(Encoding.ASCII.GetBytes(s));
  StringBuilder sb = new StringBuilder();
  foreach( byte b in hash ) sb.AppendFormat("{0:x2}", b);
  return sb.ToString();
}

This implementation is fine and has served me well for 10 years (I pulled it out of the first .NET project I ever really did). However, after using LINQ for so long, it’s hard not to see every problem as an operation over sets:

static string GetMD5String(string s) { 
  return (new MD5CryptoServiceProvider()). 
    ComputeHash(Encoding.Unicode.GetBytes(s)). 
    Aggregate(new StringBuilder(), (working, b) => working.AppendFormat("{0:x2}", b)). 
    ToString(); 
} 

I can’t say that the LINQ version is any better, but it felt better. However, you’ll notice that I’m not using any of the LINQ keywords, e.g. “select”, “where”, etc. I find that I don’t really use them that much. It’s too jarring to mix them, e.g. “(from f in foos select f).Take(3)”, since not everything has a LINQ keyword equivalent. I tend to do “LINQ-less LINQ” more often then not.

P.S. I assume someone will be able to tell me how I can do it better. : )

P.P.S. I’m using the Insert Code for Windows Live Writer add-in. I love WLW!



Comment Feed 11 comments on this post

Jon Skeet:


Any reason not to just use String.Join? New and improved in .NET 4!

return string.Join("", ComputeHash(Encoding.Unicode.GetBytes(s))
                       .Select(b => b.ToString("x2")));

Wednesday, Oct 27, 2010, 11:08 PM


RichB:


I know it's not the same, but it's a more compact representation to use UTF8 & Base64:

return Convert.ToBase64String(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(s)));

Wednesday, Oct 27, 2010, 11:42 PM


kenny:


How about as an extension method?
static string GetMD5String( static string s)

Thursday, Oct 28, 2010, 5:33 AM


Chris Sells:


Aha! Join! I didn't know we did that. Good for us! : )

Rich, I like the Base64 version, too. That would be better than looping.

Kenny, I'm not a fan of custom extension methods in general unless I'm doing the fluent functional LINQ thing. I guess it's an unfair bias.

Thursday, Oct 28, 2010, 6:49 AM


justin chase:


I agree with RichB.

Thursday, Oct 28, 2010, 7:47 AM


John Vottero:


Call me a luddite but, I think the original version is much easier to understand. Although, I would replace the last three lines with:

return BitConverter.ToString(hash).Replace('-', ':');

Friday, Oct 29, 2010, 8:30 AM


Sriram Krishnan:


I love these fluent interfaces (I acquired my love for them in the Javascript world – jquery in particular made them very popular). However, I do have one gripe with them in C# which is really an artifact of how I debug. I just love to set breakpoints and inspect return values of functions. With fluent intefaces, it is hard to do that, especially if you don’t own the code.

I often find myself writing fluent code, inserting temporary variables when I want to debug and removing them back again. : )

Sunday, Oct 31, 2010, 10:55 AM


Chris Sells:


I agree, Sriram. I often write my fluent code in stages, checking the output as I go. LINQPad is great for that. However, as I get better and more comfortable, I find I can write multi-statement fluent code in one go and it just works, which always surprises the crap out of me. : )

Sunday, Oct 31, 2010, 10:56 AM


christophep:


LINQ is the ugliest part of what I call "what is this stuff ? Is it usefull to use it ?". If I can do it easier, using efficient code, my "devise" is "No LINQ Way, no problem.". LINQ is a Java-like feature imported into the .NET Framework 3.5 to let people think they are smart because they use it.
I hate LINQ.

It is slow and it's for liquor salesman guys.

regards,
christophep | WIN32/64 Visual C++ Guy
christophepichaud@hotmail.com

Friday, Nov 12, 2010, 2:43 AM


Website Markup checker:


Whenever i see a website for web design (freelance or not), programmers and programming, I can't help but validate their site for valid markup, Why?
It says more than you think about the effort of their coding

result: HTML
413 errors
13 warnings

Thursday, Nov 18, 2010, 7:20 AM


Chris Sells:


Cool! What tool did you use?!

Thursday, Nov 18, 2010, 8:04 AM





comment on this post

HTML tags will be escaped.

Powered By ASP.NET

Hosted by SecureWebs

Mensa

IEEE