Some Extension Methods

Extension methods are, in my opinion, one of the best additions to the C# language. I’d been out of track with the various language improvements of the last few years, due to various reasons. (Playing with the language and trying to get the most out of it are not high priorities for a meth addict in active addiction. Check out the earlier posts from my other blog if you’re interested in what was high priority back then.) But for the last couple of years, writing code has once again been my main obsession. The better I understand the language, the better the code I can write, and the better the code, the better it’s performance. The better my code performs, the easier my job becomes, and as a result, I spend more time playing with the language to perfect my skills and write better code that I can reuse. Writing programs becomes quick and easy.

Again, the source, for this and a whole bunch of other stuff, is here:

Anyway, the code I’ve shared contains many extension methods, most of which are written by myself, but several come from Microsoft sample code, mostly from either the Parallel Programming with .Net blog, or their Parallel Extensions Extras sample project, and there is some code I’ve stolen borrowed from public CodeProject articles.

I’m not going to copy and paste all the code here – that would just make this post a mess to read. Instead I’ll list them, and paste one or two examples. After extracting the zip file, one of the projects contained is Romy.Core. None of my code has any copyright, but maybe that doesn’t apply to code I’ve used written by others. Check before you change anything, but it should be fine to modify anything I’ve written for your own needs. The default namespace is just a nickname my parents had for me as a kid, and also used by one or two girlfriends in the (distant) past…

(This is mostly Windows Forms.) Romy.Core contains an Extensions directory, which has:

  • CollectionExtensions
    • Parallel Sort
    • ToStringCollection – for ComboBox.ObjectCollection and IEnumerable<string>
    • ToArray – various methods to convert non-generic collections to arrays; which can then be treated like IEnumerable<T> w.r.t. Linq extension methods
    • IList<T>.AddRange; IList<T>.RemoveRange
    • Images to Icon File
    • Images to Icons
  • ControlExtensions
    • GetNestedChildAtPoint
    • SelectAndFocus
  • ExceptionExtensions 
    • mostly logging. You’ll notice a lot of my code uses the pattern:
      try… catch(Exception ex) { ex.Log(); }
  • FileAsync
    • not really extensions. async versions of File.WriteAllBytes/ReadAllBytes/WriteAllLines/AppendAllLines and so on.
  • GraphicsExtensions
    •  Draw/Fill rounded rectangles;
    • DrawShadows
  • ImageExtensions
    • various methods to save as jpeg specifying quality, save as png, convert to byte array in various image formats – has both synchronous and asynchronous overloads
  • ProcessExtensions
    • Suspend/Resume processes as written about here
  • StreamExtensions
    •  CopyToStreamAsync
  • StringExtensions
    • ContainsIgnoreCultureAndCase
    • EqualsIgnoreCultureAndCase
    • CompareIgnoreCultureAndCase
  • TaskExtensions
    • Task.TimeOutAfter
    • Task.Then
    • IEnumerable<T>.ForEachAsync
    • IEnumerable<T>.ForAsync

Here is one example. This is StreamExtensions.cs, my CopyToStreamAsync implementation. Constants.Buffersize is defined as:

public const int BufferSize = 0x2000;
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  5. namespace Romy.Core
  6. {
  7.     public static class StreamExtensions
  8.     {
  9.         /// <summary>My implementation to copy asynchronously from one stream to another, similar to
  10.         /// <see cref=”System.IO.Stream.CopyToAsync(Stream)”/></summary>
  11.         /// <remarks><para>
  12.         /// This was written because the default implementation would sometimes throw OutOfMemoryExceptions
  13.         /// in my FileAsync.ReadAllBytesAsync method, when opening large Bitmap files. Reading with a fixed
  14.         /// size small buffer works for me. This implementation may not be the best for all files, but works
  15.         /// very well as a general solution for me.</para>
  16.         /// <para>
  17.         /// This is a simple and straightforward implementation. It does nothing to change the way streams
  18.         /// are handled asynchronously from async methods, and is probably very similar in effect to the
  19.         /// default implementation (of CopyToAsync). (The only thing I force is the buffer size as stated
  20.         /// above.) A more advanced custom solution would be to implement a concrete custom base stream class.
  21.         /// i.e. implement BeginRead, EndRead, BeginWrite and EndWrite. I saw this as unnecessary.</para></remarks>
  22.         public static async Task CopyToStreamAsync(this Stream source, Stream destination, int bufferSize)
  23.         {
  24.             if (source == null)
  25.                 throw new ArgumentNullException(“source”);
  27.             if (destination == null)
  28.                 throw new ArgumentNullException(“destination”);
  30.             if (bufferSize <= 0)
  31.                 throw new ArgumentOutOfRangeException(“bufferSize”, “bufferSize must be greater than zero”);
  33.             var bytesRead = 0;
  34.             var buffer = new byte[bufferSize];
  36.             while ((bytesRead = await Task<int>.Factory.FromAsync(source.BeginRead, source.EndRead, buffer, 0, bufferSize, null)) > 0)
  37.             {
  38.                 await Task.Factory.FromAsync(destination.BeginWrite, destination.EndWrite, buffer, 0, bytesRead, null);
  39.             }
  40.         }
  42.         public static async Task CopyToStreamAsync(this Stream source, Stream destination)
  43.         {
  44.             await source.CopyToStreamAsync(destination, Constants.BufferSize);
  45.         }
  46.     }
  47. }

About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s