Ernst Kuschke

     Arbitrary thoughts and musings on life, the universe and everything else

Syndication

News

    ernst kuschke (v1.0)

    My Photos

    Microsoft Most Valuable Professional

    Member in good standing

    View Ernst Kuschke's profile on LinkedIn

    Add to Technorati Favorites

Blogs I read

Books I recommend

General Links

November 2005 - Posts

I started posting on new features in C# quite some time ago; with the speed that Microsoft has been churning out new products I better keep it up so I can cover all of it before the announcement of the C# 4.0 specs.

So without any delay, let's look at extension methods. These are methods that you add to existing types. That's right - you can add methods to any existing type, even good old trusted int! To be correct - you don't add methods to existing types, but it "feels" as if you do. Let's look at the syntax before we discuss the possible results of this feature.

We're going to add an extension method to int that will enable us to get the value of an int squared; suppose we want be able to compile and run the following code:

int theInt = 2;
int theIntSquared = theInt.Squared();

We'd like the value of theIntSquared to be 4 in this case. The syntax for creating the extension method is as follows:

namespace Extensions
{
   public static class IntExtensions
   {
      public static int Squared(this int intToBeSquared)
      {
         return intToBeSquared * intToBeSquared;
      }
   }
}

Note that the class as well as the extension method has to be declared as static. The only "strange-looking" creature here is the parameter to the extension method that is prefixed with the "this" keyword - this tells the compiler which type we are "extending" with our extension method. 

In .NET 2.0 you would do the above exactly the same, but without the "this" keyword, which would enable you to do the following:

using Extensions;

int theInt = 2;
int theIntSquared = IntExtensions.Squared(theInt);

All extension methods really do is to save you lots of typing, it does (as you can see) not really sit on the type you are extending. As cute as it looks, it could make the same types seem quite different in different scenarios! As Ian Griffiths notes: "if I do a using FooSoft.FooLib;, I might get more than just the classes I wanted to use from FooLib - I might pick up a load of extension methods I didn't particularly want, but which the authors of FooLib thought I wouldn't be able to live without. Let's hope everyone follows Microsoft's lead and segragates these things into separate namespaces - the extension methods that form part of LINQ are all in System.Query for example, so you won't get them unless you ask for them. Then again, that's not the only stuf in that namespace so maybe we're already in trouble..."

I'm not 100% sure on this one. However, once you see where (and how snug) it fits into LINQ you can see most of the reasoning behind this functionality.
(To see just how snug it fits into the LINQ plan, come around on Wednesday evening!)

Posted by Ernst Kuschke | 5 comment(s)
Filed under: ,

It's almost time for our next SADeveloper meeting! Please let us know if you will be attending over here.

When:            23 November 2005, 18:00 – 19:30
Venue:           (NB!) Torque IT, Rivonia (
Map)

Topic 1:          LINQ - .NET Language Integrated Query (the most exciting of the features coming in C# 3.0)
Presenters:     Armand du Plessis & Ernst Kuschke
Level:             Advanced
Objective:       An investigation of the LINQ query functionality and the new language features that enable it. LINQ defines a set of general-purpose standard query operators that allow traversal, filter, and projection operations to be expressed in a direct yet declarative way in any .NET-based programming language.

Topic 2:           Dummies guide to blogging
Presenter:       Kevin Trethewey
Level:             Even the people in sales would probably get it

Objective:       
A very brief introduction to blogging - specifically relating to developer & tech blogs; what blogging is, what benefits you gain from reading blogs (they're huge!), and how to start a blog of your own.

I hope to see many SADeveloper members there! We'll have the usual interesting chats afterwards over a few beers.

Posted by Ernst Kuschke | with no comments

It's quite interesting to notice the ongoing debates about interfaces vs. abstract base classes (latest example). The vast majority of developers today still do not grasp simple OO, and that, my opinion, is largely to blame on educational institutions. By now the OO world should have matured enough so we can take the principles thereof as granted knowledge and start thinking ahead. This is certainly the expectation from the guys at the forefront of innovation - at my recent trip to Microsoft Corp. Anders Hejlberg noted that the OO world has matured enough by now that it can be expected that most developers design OO structures naturally and also expect OO rules and behavior from a language. It would be great, but I suspect that he's wrong - the developing world (not only SA) still has a lot to learn regarding OO design.

There's a new kid on the block, and his name is Generics. I'm not going to illustrate and run through the whole base class vs. interface debate again; I think that one should be pretty clear by now. Interesting though is that a class's interface can also be defined by a generic now. Let's look closer. Probably the widest application for generics is to make general collections for any type. Before generics, you'd have to do something like this:

interface IEnumerable
{
   IEnumerator GetEnumerator();
}

interface IEnumerator
{
    object Current { get; }
    bool MoveNext();
    void Reset();
}

You would now need a separate implementation of the IEnumerable and IEnumerator interfaces for each type that you want to enumerate – something like this:

public class MyItemTypeCollection : IEnumerable
{
   public struct Enumerator : IEnumerator
   {
      public MyItemType Current { get {… } }
      object IEnumerator.Current { get { return Current; }}
      public bool MoveNext() { … }
      …
   }
   public Enumerator GetEnumerator() { … }
   IEnumerator IEnumerable.GetEnumerator() { … }
   …
}

You can now create enumerable collections of MyItemType types. As soon as you’d want a collection of YourItemType, you’d have to go do another implementation of the IEnumerable interface for YourItemType. Lots of typing!!!

Thanks for Generics! You can define interfaces more generically now which means less typing, and that’s just what the .NET team has done with IEnumerable. It is extended to look like this:

public interface IEnumerable<T> : IEnumerable
public interface IEnumerator<T> : IDisposable, IEnumerator

 

This enables you to create classes like the List class, which can contain any type; you don’t need to rewrite it for each type you want to contain:


public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

 

Important to note is that, with generics, you can define interfaces without the need of the interface keyword. Look at the following:


interface MyBusinessEntity
{
    Guid Id { get; }
}

 

interface MyDataLayer
{
    void Persist(MyBusinessEntity entity);
    MyBusinessEntity Retrieve(Guid id);
}

 

This can be re-written to the following generic code:

 

class MyDataLayer<T> where T : MyBusinessEntity

{
    void Persist(T entity);
    T Retrieve(Guid id);
}

 

This almost has the feeling of declaring the interface and implementation thereof in one and the same place! This might swing the old interface vs. base class discussion towards base classes. One caveat with not explicitly declaring the interface (as interface) is the fact that .NET does not support multiple inheritance.

When I first started thinking about generics, I thought that it actually breaks some OO rules, but Generics is definitely the next logical evolvement of OO principles, and certainly requires new ways of thinking.

Posted by Ernst Kuschke | 8 comment(s)
Filed under: ,

About a month ago I complained about the ridiculous service I have been receiving from Standard Bank. They have since contacted me on numerous occasions, and this is what they could do for me.

I received a phone call to apologise a month ago. The lady who phoned said that to make me feel better, they would increase the limit on my credit card. In my opinion, that does *not* help me at all, although it might come in handy. But even that, up till today, has not been done!

I received an email from Donovan Ray at Customer Relations, where he said that he would follow up on the cheque account issue. No feedback until today.

I am furious since I feel totally helpless - what can I do? Drive over there and smack someone? I have made one conclusion:

STANDARD BANK IS PATHETIC, and I will not for much longer be their customer.