Thursday, August 09, 2007

C# 3.0 - Extension Methods

Think of extension methods as normal static utility methods you can add to any object.

Extension Methods can be defined in any class. There are only two things you need to do to define an EM. First the method must be public static in a static class, and you must add this to any parameters.

Example
public static class MyExtensions
{
   public static string AddNewLine( this string s )
   {
      return s += "\r\n";
   }
}

Usage

string s = "This is my Test";
Console.Write(s.AddNewLine());
Console.Write("I'm now on a new line");
Console.ReadLine();

This is a fairly trivial example, but I'm sure you are starting to see the power of extension methods.

In C# 2.0 days the above would be:

string s = "This is my Test";
Console.Write(MyExtensions.AddNewLine(s));
Console.Write("I'm now on a new line");
Console.ReadLine();

To my mind, the above is not as readible, but also you have to remember which utility class you put AddNewLine in, or more importantly another coder has to know which class you put the method in.

Microsoft suggest you should use your own Extension Methods sparingly and warns not to extend existing types that you don't control. This is because changes in the existing type could break your code.

I guess this is all logical stuff. I think that most people will use Extension Methods for adding utility functions to standard types, for example the one I can think of, off the top of my head would be something like ToInt() to convert a string to an Int.

Great Post

Here is a great post explaining the intangible benefits of Mac use. Gets especially insightful around page five ;-)

Wednesday, August 08, 2007

What do you want in a computer?

If you hate Macs, then don't bother reading on.

Anyone who reads this blog will know that I am a developer for a living, what's more I develop on Windows using Microsoft tools. Without a shadow of a doubt Visual Studio and in particular C# are the best tools for getting the job done. Period. Windows, well not so much.

When I get home, I just want my computer to work. I want to catch up on email, read and write blog posts, surf the net, watch video, listen to music and import, edit and view of Digital photos. I would estimate that 80% of computer users would have the same requirements.

I don't want to have to piss around making sure my Virus, Malware and Spyware apps are up to date and my computer is clean. I can't be arsed with my machine crashing when I'm half way through something. In other words I use a Mac. Now, I can respect people thinking they are saving money by buying a budget Dell or equivalent. I'm sure they are fine and do the job 90% of the time. More power to them. For the rest of us, I really and honestly don't understand why so many people persevere with Windows.

I think other than the fact that OSX just works without fuss and is virtually Virus and Spyware free, the biggest pro for getting a Mac is the bundled software. iPhoto, iDVD, iMovie, iTunes, GarageBand, iMail and iCal are brilliant. There are apps on Windows that do the same thing for free, but none of them are included with the operating system, or work as seamlessly.

There is a new version of the so called iLife apps out. Link here. Having seen what is on offer, I think that the new iPhoto could be the killer app to make Windows users switch. It automatically imports photos from your Camera and breaks them into Events, making it easier to view your photos. This with iPhoto's already impressive ease of use, speedy search and "it just works" factor will make viewing your photo library painless.

I know this sounds like an ad for Apple, but it's more wanting to let everybody know how much simpler life with a computer can be. If you've ever used an iPod then used any other MP3 player you will understand what I mean.

The future is Linq and Linq to SQL

I have been playing with VS 2008 Beta 2 all morning. Where to being. I will post some more specifics later but Linq - wow.

I have read some propeller heads not liking Linq to SQL, because it isn't a true ORM, whatever that means. Me, I like it. It gives enough abstraction to handle the meanial tasks, but gives me enough flexibility to let me do what I want, and not have to buggar around with config files.

Get used to seeing code like this:

var totalInvoiceValue = db.Invoices.Where( cust => cust.City == "London" ).Sum( cust => cust.ExtendedPrice );

In a nut shell, that is all you would have to do (once the mappings are setup and thats Visual) to get all the sum of all invoices for London. No more query strings. Very nice.

It is my intention to door some more posts around Linq as I get more into it, mostly so I can write down my thoughts as I find having to explain something in words help me get a better grasp of it too, and if it is useful to others along the way then all the better.

But until then, do yourself a favour and download the beta and start playing.

Tuesday, August 07, 2007

New iMacs Released

New iMacs here

Even though I am an unabashed Apple fan boy, I have to admit I was surprised at how cool the new iMacs are. Might even have to get one I think.

Not sure about the new keyboard, but I like the USB port at either end.

I would wait until Leopard comes out in the next month or two before getting one though. Oh and they'll run Windows just fine, trust me. I have just installed Visual Studio 2008 Beta 2 on mine and it runs great.