May 042010
 

So, I guess I’ll give this tutorial thing a try.  I’ll begin with something simple.

Let’s learn about Extension Methods in C#!

Extension Methods were introduced in C# version 3.0 (C# Version 3.0 Specification).  Extension methods allow you to add or “extend-to” a pre-existing class a method of some sort.  This is particularly useful when you wish extend a method to a type that is defined in C# (or something closed source) without having to create another class and derive another type.

The format is as follows:

public static void ExtensionMethod(this extensiontype var, type additionalvar1, type additionalvar2, .....)
{
    //Do something...
}

Ideally, you might wish to encapsulate these extension methods in their own class.  Perhaps even a class that only contains extensions for the particular type you’re extending:

public static class SomeTypeExtensions
{
    public static void ExtensionMethod(this extensiontype var, type additionalvar1, type additionalvar2, .....)
    {
        //Do something...
    }
}

Furthermore, you could encapsulate the multitude of extension classes you create into their own namespace and optionally include the use of the extensions on a class to class and/or file to file basis in the using statements.  I’ll let you decide on that at your own discretion.  I like to follow this convention in my projects where I need to use extensions:

namespace Extensions
{
    public static class SomeTypeExtensions
    {
        public static void ExtensionMethod(this extensiontype var, type additionalvar1, type additionalvar2, .....)
        {
            //Do something...
        }
    }
}

It’s important to note that the class containing your extension method must be static and the method you are extending must also be static.  This is a requirement for extensions methods.

OK.  Now that we have the appropriate syntax in our heads for defining an extension method, let’s put what we’ve learned to use.  Let’s define a simple extension to the int type that allows us to print the value of the int to stdout:

namespace Extensions
{
    public static class IntExtensions
    {
        public static void PrintInt(this int integer)
        {
            //Print the integer value for the int object caller
            //and a newline
            Console.Write(integer.ToString() + '\n');
        }
    }
}

You can choose optionally which classes you wish to implement this extension based on the namespace where it lives.  If you have encapsulated the class in an ‘Extensions‘ namespace like I suggested earlier, then you can simply add it among the using statements of the classes you wish to have access to your extensions:

using Extensions;

Once a class has access to your extension you may call the extension method on any variable of the type you provided an extension for:

int number = 5;
number.PrintInt();

The output to the console will look like this:

ExtensionsExample1Output

It doesn’t get much straightforward than that.  If you wish to try out the code explained above here is a link to download the Visual Studio Project (requires Visual Studio 2008. Though, it may work with Visual C# 2008 Express or 2010 Express which are free.  I haven’t tested it.  You’re welcome to.):

Example1.zip

I have taken the time to create another, more advanced, example using the Microsoft.Xna.Framework (this requires XNA 3.1).  In the second example we are modifying a Texture2D object to extend a draw method to it.  The end result is a sand texture stretched across the game window.  Here’s a link to the second example project:

Example2.zip

For more guidance and examples you can consult the C# Programming Guide for Extension Methods at MSDN where they provide the general syntax and usage as well as recommendations for when extensions should and should not be implemented.

Hope this was helpful.  Thanks for reading!

Sources:

C# Version 3.0 Specification
(http://msdn.microsoft.com/en-us/library/ms364047%28VS.80%29.aspx)
Extension Methods, C# Programming Guide
(http://msdn.microsoft.com/en-us/library/bb383977.aspx)
Sand Texture Graphic for Example 2
(http://mayang.com/textures/Nature/images/Sand/sahara_sand_patterns_220511.JPG)
Sep 162009
 

I’ve been studying some PHP here and there by reading the PHP manual (PHP.net) online.  I’ve been mostly learning, or rather, acquainting myself with Javascript at W3schools (w3schools.com).

Thus far, I am really quite pleased with how similar javascript is to C family languages; in particular C++ and C# as I have had the most experience with both of them.  I’ve been blazing through the examples at w3schools and am itching to get my hands dirty with some javascript.  I will, however, move on to some PHP programming before I begin anything big regarding the construction of the main site.

For now, I think I’ll stick to the learning and maybe work on some preliminary or concept design work on the look of the main page in regard to navigation of the site.

Thanks for reading!