Monday, June 6, 2011

C# Extension Methods and Java SE 7 Project

This blog is about the C# extension methods and summarization of Java language changes for Java SE 7 project.

Extension Methods (C# 3.0)

According to MSDN an extension method is a special kind of static method but they are called as if they were instance methods on the extended type. It allows a programmer to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Typically when a programmer wants to add new method to an existing types he will create a new derived type or modify the source code of the original type. Consider the following example, I want to add a new method to the existing built-in String type of C# but this is not possible because I don't have the source code of C# String and another problem is that String was a declared as a sealed type therefore I cannot also create a new derived type of String. Now staring with C# 3.0, I can now do this with the help of extension method. Take a look at the code I wrote in C# that "add" new methods to existing built-in String type of C#:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExtensionMethods
{
    public static class StringExtension
    {
        public static string Reverse(this string arg)
        {
            StringBuilder reversedArg = new StringBuilder();

            for (int i = arg.Length - 1; i >= 0; --i)
            {
                reversedArg.Append(arg[i]);
            }

            return reversedArg.ToString();
        }

        public static void Main()
        {
            string message = "iamnotapalindrome";
            string reverseMessage = message.Reverse(); // call as if an instance method of string
            bool isPalidrome = reverseMessage.Equals(message);
           
            Console.WriteLine("Message: {0}, Reverse Message: {1}",
                message, reverseMessage);

            Console.WriteLine("Message Equal? {0}", isPalidrome ? "Y" : "N");
            Console.ReadKey(true);
        }
    }
}


In the definition of an extension method (the code in bold), the this before the first argument specifies that its an extension method to the type String in this example. Got the picture?

As of now Java does not support extension methods. For more information about extension methods see MSDN Extension Methods (C# Programming Guide). Next, is a listing of Java language changes for JDK7 project.

Java SE 7 Project

You can get the early access snapshots of JDK 7 at Java.net which includes several features and enhancements such as module support, type annotations, and the project coin. The project coin is a small language changes like strings in switch, improved exception handling, automatic resource management, improved type inference, null safe operators, and simplified varargs. To see more project coin proposals see this link.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.