Adventures in .NET 3.5 Part 1

A "Print" To the Console

Because I write a lot of small console applications, I find myself typing ...

        Console.WriteLine("some text"); 

in C# again and again.  I've always pined for the ability to simply be able to have an object "Print" itself to the console.

In other words, I'd really like to be able to type statements like those below ...


Well .NET 3.5 makes that easy with Extension Methods.  Check out this post to get started.  The code to implement such a "Print" method is simple ... 

      public static void Print(this object o) {



Another thing I need to do all the time is output the contents of a collection as a string on a single line with a definable separator.  Now that we have a "Print" method, I'd like to be able to type ...

      /* output to the console: Soccer,Rugby,Tennis,Cricket */
      var sports = new string[] { "Soccer", "Rugby", "Tennis", "Cricket" };

Here's code to implement "Concatenate()".

      public static string Concatenate<T>(this IEnumerable<T> items, string separator)
          StringBuilder buffer = new StringBuilder();
          int totalItems = items.Count();
          int counter = 0;
          foreach (var item in items)
              if (counter < totalItems)
          return buffer.ToString();


Coming Next

Implementing Ruby Blocks in C# (well almost).

No Comments