Archive for the 'C#' Category

Covariance and Contravariance in C#3

Monday, March 9th, 2009

A short introduction to Covariance and Contravariance in C# 3 preparing you to an article about that in C# 4.
So what is covariance ?
Covariance is basically using a method which returns something derived from the expected type.

An exemple ? It’s safe to have a method returning a cat when you expect it to return an animal.
(more…)

Combinatory logic from scratch

Friday, November 28th, 2008

Cause it’s sooooo sexy, let’s speak about Combinatory Logic !
Rule 1 : You don’t talk about Combinatory Logic
Rule 2 : You don’t talk about Combinatory Logic
Rule 3 : Combinatory Logic is based on Lambda Calculus (see Wikipedia for both)
Rule 4 : A combinator is a Lambda expression taking One and only One combinator as parameter, and returning a Combinator.

(more…)

The art of Events

Friday, August 29th, 2008

The art of using events to build more independent classes.
/* Found a better example */
Imagine you have a class A and a class B.
A builds B, and B have to communicate with A (call methods … ?)
Some developers (Boooo (I’ve done it … (Booo >> me))) will pass a reference of A to B
“A(){B my_B = new B(this);}” /* Please note the newB private joke */
But what happens when you want an other class, C, who don’t know A to build B ?

So we have to remove the A’s reference in the B ctor.
How B should communicate now ?
– Using Events !
B exposes an event, the builder of B can register on it.
– B can speak to A throwing this event.
– Every class can build a B, and can, if it needs, register on its events, doing whatever he wants when the event is thrown.
B is now fully reusable !

Howto invoke an event via reflection

Friday, August 29th, 2008

Why this article ? because of this note found on the msdn’s EventInfo page:
“EventInfo is not intended to be used to raise events. An object raises events as dictated by its internal state. ”

Let’s try to raise an event with reflection …

(more…)