### Tuesday, December 02, 2003

## pirates

Avast, me hearties?!

Unbelievable how quick it happens in the wild wild east. Pirated Longorn CD's are for sale in Malaysia, for less than $2 a piece! Shiver me timbers!!!

Unbelievable how quick it happens in the wild wild east. Pirated Longorn CD's are for sale in Malaysia, for less than $2 a piece! Shiver me timbers!!!

### Monday, December 01, 2003

## new frontiers

This is real cool - humankind have reached out to the end of their solar system. (Or have we?!)

Other new frontiers covered over the weekend: I studied C# Generics. I never really realised how powerful this is, especially since it's inherently supported by MSIL. A quick run of what it is:

If you have ever written a class that accepts certain types as members, only later to realise that you need the exact same functionality for a different type of member? The following class is extremely simple, and possibly useless, but serves as a convenient example:

class Math

{

public Math { }

public int Sum(int FirstNumber, int SecondNumber)

{

return FirstNumber + SecondNumber;

}

}

Now this class could be usefull for other types, like float, single or double. Great, let's override the methods to accept and return different datatypes. Laborious, but it will work for our simple example. This would mean that you'd have to override the method for every different type you expect. A better approach: let's use generics to write a generic class, as follows:

class Math<T>

{

public Math<T> { }

public T Sum(T FirstNumber, T SecondNumber)

{

return FirstNumber + SecondNumber;

}

}

Note the use of the generic type T. To call this method on int types:

Math<int> math = new Math()<int>;

int firstNumber = 1;

int SecondNumber = 3;

int sum = math.Sum(firstNumber , SecondNumber);

The method will be called with T of type int, as specified by the calling method. To work with float types:

Math<float> math = new Math()<float>;

float firstNumber = 1;

float SecondNumber = 3;

float sum = math.Sum(firstNumber , SecondNumber);

Never would you have the need to change your generic class. Neat, isn't it!!!!!

There are some issues regarding type-safety. What will happen in the following case?

Math<float> math = new Math()<float>;

string s = "myString";

SqlConnection cn = new SqlConnection();

int madness = math.Sum(s, cn);

Some crazy-ass returned value? Definately a runtime exception. Type safety just went to the moon here. There are generic constraints to build into your server-class to prevent this sort of abuse of your class. Constraints could tell the compiler what class T should inherit from, or which interfaces T should implement.

Here's some interesting reading on Generics available on the web, which discusses generic constraints in more detail.

Other new frontiers covered over the weekend: I studied C# Generics. I never really realised how powerful this is, especially since it's inherently supported by MSIL. A quick run of what it is:

If you have ever written a class that accepts certain types as members, only later to realise that you need the exact same functionality for a different type of member? The following class is extremely simple, and possibly useless, but serves as a convenient example:

class Math

{

public Math { }

public int Sum(int FirstNumber, int SecondNumber)

{

return FirstNumber + SecondNumber;

}

}

Now this class could be usefull for other types, like float, single or double. Great, let's override the methods to accept and return different datatypes. Laborious, but it will work for our simple example. This would mean that you'd have to override the method for every different type you expect. A better approach: let's use generics to write a generic class, as follows:

class Math<T>

{

public Math<T> { }

public T Sum(T FirstNumber, T SecondNumber)

{

return FirstNumber + SecondNumber;

}

}

Note the use of the generic type T. To call this method on int types:

Math<int> math = new Math()<int>;

int firstNumber = 1;

int SecondNumber = 3;

int sum = math.Sum(firstNumber , SecondNumber);

The method will be called with T of type int, as specified by the calling method. To work with float types:

Math<float> math = new Math()<float>;

float firstNumber = 1;

float SecondNumber = 3;

float sum = math.Sum(firstNumber , SecondNumber);

Never would you have the need to change your generic class. Neat, isn't it!!!!!

There are some issues regarding type-safety. What will happen in the following case?

Math<float> math = new Math()<float>;

string s = "myString";

SqlConnection cn = new SqlConnection();

int madness = math.Sum(s, cn);

Some crazy-ass returned value? Definately a runtime exception. Type safety just went to the moon here. There are generic constraints to build into your server-class to prevent this sort of abuse of your class. Constraints could tell the compiler what class T should inherit from, or which interfaces T should implement.

Here's some interesting reading on Generics available on the web, which discusses generic constraints in more detail.