The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,551,710
since: 19 Jan 2005

Language Extensions in C# 3.0

posted Wed 31 May 06
The beauty of language extensions is that they are scope based. If there is a static class "nearby" that happens to work on an object of the type with which you're working - the extension will be available. If the extension is not in scope, either because it hasn't been referenced or because you haven't included the extension's namespace with a using... clause, then the extension is unavailable.

As far as I can tell, VB requires you to throw a bunch of ugly attributes on your classes to make them extensions. In C#, all you have to do is add the this keyword to the first parameter of your extension method, and you have extended the data type of that parameter.

For example, to extend the string class, just create a static class like so:

static class StringExtender
{
     public static string DoSomethingFancy(this string input)
     {
        ...
     }
}


This creates a language extension on all string objects, so you can then do:

"Hello".DoSomethingFancy();

or

myString.DoSomethingFancy();


Simply by having your extension be scope-visible to the code in which you are invoking the extension.

Here's an example of a class I wrote that adds an InAlternateCulture(string culture) method to the string class to force a string to display in a given culture without regard for the current thread's culture:

using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Data.DLinq;
using System.Globalization;

namespace languageExtensions
{
    public static class StringCultureExtender
    {
        public static string InAlternateCulture(this string sourceString, string altCulture)
        {
           CultureInfo ci = new CultureInfo(altCulture);
           return Strings.ResourceManager.GetString(sourceString, ci);
        }
    }
}


That's it. All you have to do in order to switch a string is send the string's resource keyword as follows:

Console.WriteLine("Hello".InAlternateCulture("en-AU")) can print something like "G'day".

Here's a screenshot from a program that displays strings in Hindi or English:

tags:        

links: digg this    del.icio.us    technorati    reddit




1. cal left...
Mon 23 Apr 07 5:51 pm

How do you see this functionality in comparison to objc classifications?


2. cal left...
Wed 25 Apr 07 6:26 pm

ah - sorry - i meant categories ... (i'm a cocoa newbie) .. and I see you have another post already addressing this very thing!


Tag Related Posts

CLINQ v1.1.0.0 Released!

Fri 02 May 08 5:38 P GMT-05
tags:          

LINQ to REST - A much better name for Astoria

Tue 11 Dec 07 1:23 P GMT-05
tags:        

Unexpected and Unsafe behavior in LINQ

Wed 14 Nov 07 8:09 P GMT-05
tags:    

Continuous LINQ - Can I write games with it?

Mon 13 Aug 07 3:09 P GMT-05
tags:        

Continuous LINQ

Mon 06 Aug 07 1:21 P GMT-05
tags:    

Dynamic, Observable LINQ Views

Tue 31 Jul 07 1:21 A GMT-05
tags:        

Installing Orcas Beta 1 - VMware Style

Mon 23 Apr 07 12:16 P GMT-05

Visual Studio "Orcas" - March CTP is Available

Wed 28 Feb 07 12:28 P GMT-05
tags:            

Objective-C Categories vs C# 3.5 Language Extensions

Mon 26 Feb 07 1:05 P GMT-05
tags:                

DLinq vs the ADO.NET Entity Framework

Fri 23 Jun 06 4:01 P GMT-05

WPF/XAML and LINQ - A match made in heaven

Tue 06 Jun 06 11:32 A GMT-05
tags:                  

Language Extensions in C# 3.0

Wed 31 May 06 2:17 P GMT-05

Lambda Lambda Lambda

Sun 21 May 06 1:01 A GMT-05

The Adventures of LINQ (Not Zelda)

Fri 19 May 06 11:21 P GMT-05
tags: