Posts

Showing posts with the label Andy

Mesh and IE Favorates

Image
I have been playing around with Mesh a bit of late. Mostly good other than on a few of my machines, Mesh refused to install. Either the install itself failed or it just never allowed me to sign in. But, I figured out one use for Mesh this morning that made it all come together: sharing IE Favorites. After installing Mesh, share the following folder on each machine: Xp, 2003: C:\Documents and Settings\%USERNAME%\Favorites\Links Vista, 2008: C:\Users\%USERNAME%\Favorites\Links Make sure when resolving the share location of the folder on subsiquent machines, you browse to the location above. Now your IE links are synced across machines.  

Fuse Extension Method

Image
Previously, a few of us held a discussion about the best way to concatinate a number of string array elements into a single string. You can find details here . Last week, I needed something a bit more general purpose. Came up with an extension method on IEnumerable that will accomplish this for any type. public static string Fuse<TSource>(this IEnumerable<TSource> source, string separator) {     if (source is string[])     {         return string.Join(separator, source as string[]);     }     return source.Aggregate(new StringBuilder(),         (sb, n) => sb.Length == 0 ? sb.Append(n) : sb.Append(separator).Append(n),         sb => sb.ToString()); } The method is optimized to use the string.Join method when the underlying type is a string. Join uses unsafe code and is very quick. Here are a few example u...

Compare Byte Arrays

Image
Slightly more efficient by starting off with a reference compare. If the same two arrays are passed in, no need to compare the elements. I don't think there is a better way unless you use unsafe code / pointers. The use of generics makes this more general purpose. bool Compare<T>(T[] left, T[] right) {     // handles the same array     // handles both null     if (left == right)         return true ;       // fails when either are null     if (left == null || right == null )         return false ;       if (left.Length != right.Length)         return false ;       for ( int i = 0; i < left.Length; i++)     {         if (!left[i].Equals(right[i]))      ...

Compare IEnumerable (a still better option)

Image
My office mate Chad came up with yet an even better option. You need to be using 3.5 and include the System.Linq extension method namespace. It didn't perform quite as well but pretty close. Even more general purpose. Note that the default SequenceEqual method throws an exception when either operator is null so I handled those cases first. bool Compare<T>( IEnumerable <T> left, IEnumerable <T> right) {     // handles the same array     // handles both null     if (left == right)         return true ;       // fails when either are null     if (left == null || right == null )         return false ;       return left.SequenceEqual(right); }  

Comma Delimited String From An Array using LINQ

Image
In response to the previous posting . A few weeks ago, I would certainly have solved this the same way that you did in "OldSchool" with the exception of using IEnumerable < string >. Why limit your code to just string arrays? Now that we have LINQ, we can get pretty fancy! Technically one line within the method but I split it up to aid readability.   static void Main( string [] args) {     string [] names = new string [] { "Andy" , "Marcy" , "Cindy" , "Jennifer" , "Aaron" };       Console .WriteLine(NewSchool(names)); }   static string NewSchool( IEnumerable < string > list) {     return list.Aggregate( new StringBuilder (),         (sb, n) => sb.Length == 0 ? sb.Append(n) :          sb.Append( "," ).Append(n),         sb => sb.ToString()); }   And for even ...

The Beauty of LINQ

Image
I was recently working with an RDP settings file and its myriad of settings and corresponding lines. I couldn’t find what I needed so I thought it would be nice to sort the lines. I am sure there are utilities out there that can quickly sort a bunch of text lines but like most programmers, I thought it best that I develop my own solution. So I fired up VS and created a new Windows Forms project, added a standard TextBox and Button control to my form and wired up the following OnClick event: private void ButtonSort_Click( object sender, EventArgs e) {     TextBoxMain.Lines = TextBoxMain.Lines         .OrderBy(s => s)         .ToArray(); } Just too simple. Man I love LINQ!