C sharper with Extensions

Here’s another post to continue on the Java developer C# experience.

Extension classes, with naming convention of Extensions.cs

public enum Fruits {
  Apple,
  Banana,
  Orange
}

public static class FruitsExtensions {
  public static bool IsRed(this Fruits fruit) {
     return fruit == Fruits.Apple;
  }
}

this would allow you to do

var myfruit = Fruits.Apple;
myfruit.IsRed();

It looks like a Decorator pattern, except upon reading more, the extensions miss the dynamic properties of the design pattern – it cannot alter behaviors of the existing class. All it does is to provide a way to add static method to an existing class. So it is NOT a Decorator.

Again, the Visual Studio peek definition doesn’t help again. It shows the class with empty methods. A search for the entire project helped though. The code was embedded in a common class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s