Podcast #6 - Project Design & The Future of C#

by Matt 1. December 2009 14:38

In this episode Chris, Matt and Eric discuss project design, Visual Studio 2010 Beta 2, ASP.NET MVC, the future of C#... and much more!

  • The guys start out by discussing some of the best practices for initial project design.
  • Chris and Matt used a Microsoft product called Sketchflow that is part of the Expression Blend family of products.  Another popular screen layout program is Balsamiq Mockups.
  • Writing specs can be an important part of the design process, even though some developers dislike this part.
  • Now that Chris and Matt have more experience with ASP.NET MVC, they talk about what they like and dislike about it, as well as how the repository pattern has helped with their current process.
  • Visual Studio 2010 Beta 2 is out now, and is a huge improvement over Beta 1.
  • One of things we like about C# is how much it evolves as a language, which is also one of the things a lot of developers dislike.  One of the articles discussed is K. Scott Allen's article about where C# is in the development lifecycle.
  • To wrap up the podcast, the guys discuss DJ Hero and The Beatles Rock Band.

If you have a question for the podcast, please send an e-mail to podcast@craftycoders.com.  You can either send your question in text form, or record it and send us the audio file.

Download Podcast #6 MP3

"Using" Extension Methods in C#

by Matt 12. August 2009 11:08

Extension methods have become one of my favorite parts of C# as of late.  They can be very powerful and save you from writing some redundant code.  A great extension method I discovered a couple weeks ago was wrapping up the "using" statement like so:

public static void Use<T>(this T item, Action<T> action) where T : IDisposable
{
    using (item)
    {
        action(item);
    }
}

This allows you to write using statements using a Lambda expression like this:

void UseStream()
{
    string contents;

    new StreamReader(@"c:\file.txt").Use(s =>
        {
            contents = s.ReadToEnd();
        });

    // Do something with the contents
}

Instead of this:

void UseStream()
{
    string contents;

    using (StreamReader s = new StreamReader(@"c:\file.txt"))
    {
        contents = s.ReadToEnd();
    }

    // Do something with the contents
}

OK, so this really doesn't save you a lot of code, but it keeps you from writing a small amount of redundant code when instantiating what you are "using".  After about a week of using it, I modified it it to make it a little more powerful.  I created an additional extension method that looks like this:

public static TResult Use<T, TResult>(this T item, Func<T, TResult> action) where T : IDisposable
{
    using (item)
    {
        return action(item);
    }
}

This allows you to return a type from within your using statement very easily:

string ReturnStreamContents()
{
    return new StreamReader(@"c:\file.txt").Use(s =>
        {
            return s.ReadToEnd();
        });
}

Wrapping up a using statement this way could also allow you to perform any custom logic inside the extension method that you need to.

Dictionary Key Case Sensitivity in C#

by Bradley 8. May 2009 16:26

By default you must make sure that a key used to look up a value in a dictionary matches the case of key used to store the value in a dictionary. Simply put, list["A"] would not return the same value as list["a"]. It would be safe to assume that the default behavior was selected for efficiency. After all would you want your code slowed down by doing list["a".ToUpper()], if you knew that the key used to look up a dictionary value would never have a different case than what was used to store it. With that in mind, if case were an issue, most of us would change our code to perform the ToUpper just described. Although this approach is functionally sound, your code could become unnecessarily cluttered. An equally functional approach that would keep your code uncluttered would be to use one of the alternate constructors for your dictionary, see example below.

using System.Collections.Generic;

Dictionary<string, string> list = 
    new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
list.Add("A", "Value");

if (list["A"] != null)
{
    Console.WriteLine("A can be A");
}

if (list["a"] != null)
{
    Console.WriteLine("a can also be A");
}

Tags: , ,

.NET | C#

Sorting Files Using LINQ & Lambda Expressions

by Michael 30. December 2008 13:41


using System.Collections.Generic;
using System.IO;
using System.Linq;

class SortFiles
{
    public IEnumerable<FileInfo> Sort()
    {
        // Set the directory you want to search
        DirectoryInfo di = new DirectoryInfo(@"C:\"); 

        // Get all of the .jpg files from the directory and order
        // them by file name in descending order
        IEnumerable<FileInfo> files =
            di.GetFiles("*.jpg").OrderByDescending(e => e.Name); 

        return files;
    }
}

You can sort it by name, creation time, length, or any other FileInfo property.  You can find many other LINQ examples here:  http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

Tags: , ,

.NET | C# | LINQ

Powered by BlogEngine.NET 1.5.0.7
Original Theme by Mads Kristensen | Modified by Crafty Coders