Archive

Posts Tagged ‘c-sharp’

101 LINQ Samples

January 14th, 2011 No comments

OK, I’ve just discovered this. If, like me, you’re always struggling to remember the name of that LINQ extension method, or precisely how it’s supposed to work, then this is really useful.

http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

Every single LINQ extension method, categorized, and all on one page without all the method overload signatures that just end up confusing you. Then, once you’ve identified the one you need a single click will take you to a concise yet precise example of how to use it, and what result you can expect.

Simply brilliant! I’ve no idea who did this, but thank you.

Tags: ,

Feature Jealousy

March 25th, 2009 No comments

What’s got LINQ, but doesn’t have a yield keyword and no Action or multiline lambdas? What’s supposed to be an easy to learn language, but supports exception filters when other supposedly more complex languages don’t?

The answer, VB.NET.

“Co-evolution” has been announced for VB.NET 10.0 and C# 4.0. However, VB.NET doesn’t appear to be getting yield, and C# won’t be getting XML literals (which I personally think is a good thing).

I think VB.NET shed the “Beginners” bit from BASIC a long time ago. What do you think?

Oh, and whilst I’m at it, who’s up for an __il keyword in C# so you can have inline blocks of IL?

Tags: , ,

Flexible Enumerations

February 4th, 2009 7 comments

Sometimes enumerations in .NET just don’t cut it. In the end they’re just a numeric value to which a piece of string metadata is attached to some of the values. Consider the following enumerations:

public enum OfficeLocationNames
{
    London,
    Edinburgh,
    Redmond
}

public enum OfficeLocationCodes
{
    LON,
    EDI,
    RED
}

What are these enumerations actually trying to say? I see these and think that there are three office locations, London, Edinburgh and Redmond, and each has a name and a three letter code, i.e. one type of thing, an office location, with two pieces of descriptive information about that thing, name and code. But using enumerations has forced us to define two things, when it’s really one. So what solutions are there?

Here’s one solution I like. I’m sure there are more, so feel free to suggest variations or completely different patterns in the comments. First off I define an interface, IOfficeLocation, to specify what makes up an office location:

public interface IOfficeLocation
{
    string Code { get; }
    string Name { get; }
    double Latitude { get; }
    double Longitude { get; }
}

I’ve taken the liberty of adding two more pieces of descriptive information to the office location too, latitude and longitude, just to prove the point. Note that this is immutable, i.e. you can’t change the values once they’ve been created.

Next, I define the OfficeLocations static class that will be our container for the three possible office locations. Inside this class I define the OfficeLocation class, which is a concrete implementation of the IOfficeLocation interface. Again, this class is immutable, and importantly it’s a private class to the OfficeLocations static class. This ensures that only the OfficeLocations class can construct instances of the OfficeLocation class. Finally I define the three office locations as static read-only fields of the OfficeLocations class, all using the IOfficeLocation interface, but constructed from the private OfficeLocation class.

public static class OfficeLocations
{
    private class OfficeLocation : IOfficeLocation
    {
        private readonly string code;

        public OfficeLocation(string code, string name, double latitude, double longitude)
        {
            this.code = code;
            this.name = name;
            this.latitude = latitude;
            this.longitude = longitude;
        }

        private readonly string name;

        private readonly double latitude;

        private readonly double longitude;

        public string Code
        {
            get { return code; }
        }

        public string Name
        {
            get { return name; }
        }

        public double Latitude
        {
            get { return latitude; }
        }

        public double Longitude
        {
            get { return longitude; }
        }
    }

    public static readonly IOfficeLocation London = new OfficeLocation("LON", "London", 0d, 0d);
    public static readonly IOfficeLocation Edinburgh = new OfficeLocation("EDI", "Edinburgh", 0d, 0d);
    public static readonly IOfficeLocation Redmond = new OfficeLocation("MAN", "Manchester", 0d, 0d);
}

Using the OfficeLocations class is very similar to an enumeration, and because there is only ever one instance of London, Edinburgh and Redmond I can test for equality without needing to override the Equals method in the OfficeLocation class. I also like the fact that I cannot do more than or less than comparisons against office locations. It doesn’t make sense, to see if London is more than Edinburgh, does it? However, if it did I could implement IComparable on OfficeLocation and then I could make those comparisons.

public bool IsInUnitedKingdom(IOfficeLocation location)
{
    return (location == OfficeLocations.London) || (location == OfficeLocations.Edinburgh);
}

So, as usual, comments are always welcome. Are there any variations on this theme? Or perhaps issues with this implementation? Surely not!

Tags: ,

LINQ is amazing!

May 21st, 2008 No comments

I’m just starting to get my head around what LINQ is all about, and I have to say I love it! What’s really struck me is the power of LINQ to Objects to concisely and accurately describe the intention of the code without the usual complexity of loops and ifs. To give you a example, I’m putting together a simple proof of concept publish/subscribe message bus in .NET at the moment. It’s at the very early stages at the moment, and I’m sure it’ll be the subject of future posts, but one of the first things I needed to do is match a published message to a set of subscriptions to identify the subscribers to activate.

The original version of the code involved a foreach loop and a number of if conditions to find the correct subscriptions for the published message. It worked just fine, and read fairly well too. But just I think the LINQ version speaks for itself:

var subscriptionsToActivate = from subcriptionObject in subscriptions
           where subcriptionObject is Subscription<T>
           let subscription = (Subscription<T>)subcriptionObject
           where subscription.ShouldActivateFor(message)
           select subscription;

Nuff said!

Tags: ,