views:

286

answers:

5

Are extension methods available on CE framework as well? I have an extension method for string that works fine in a windows forms project, however it wont build in PocketPC application.

I figured this would be an easy thing to find out, however I was unable to find any info regarding extension methods on PocketPC.

Edit: Ooops this was my mistake. I wrote the extension method in Visual Studio 2008, however the PocketPC project was being compiled in Visual Studio 2005, which I didn't realised. Well that's a hour of my life I'm never getting back. Thanks everyone for answers anyway.

+2  A: 

They're supported in the Compact Framework according to this blog post. However, I expect they require Compact Framework version 3.5. Which version are you using?

Jon Skeet
+2  A: 

What framework version are you targetting? If you are targetting CF 2.0 from VS2008, you may need to declare ExtensionAttribute...

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
         | AttributeTargets.Method)]
    public sealed class ExtensionAttribute : Attribute {}
}
Marc Gravell
Good link to that here: http://blog.markarteaga.com/UsingExtensionMethodsInNETCF20.aspx
Jason Down
Well, maybe - but it is nicer to include the AttributeUsage too ;-p
Marc Gravell
Ya, true enough.
Jason Down
A: 

You can use it with the .NET Compact Framework 2.0 and VS2008 through a small hack according to this blog.

tomlog
+2  A: 

Yes, they are supported in CF 3.5. If you are using CF 2.0 you will need to define the ExtensionAttribute and then they will work.

    // this is a definition of a 3.5 class for use in 2.0.  If we upgrade to target CF3.5, we will need to remove it...
    namespace System.Runtime.CompilerServices 
    { 
        public class ExtensionAttribute : Attribute { } 
    }

namespace TestExtension
{
    public static class Extensions
    {
        public static int TestMethod(this string value)
        {
            return value.ToString();
        }
    }
}
Jack Bolding
+2  A: 

Wanted to clear up a bit of confusion here. Extension methods are a feature of the compiler, not necessarily a particular version of the framework. Therefore, extension methods can be used on any platform where there is a version of the compiler that supports both extension methods and that platform.

The C# 3.0 compiler can down target to 2.0 frameworks and supports extension methods so they should be available on the compact framework.

The only thing the framework actually provides for extension methods is the ExtensionAttribute. However this doesn't have any functionality associated with it and can be defined by your application if it's not available. Here is a blog post I wrote on the subject

JaredPar