It seems I can not find the link if necessary using C # 4.0 in a .Net 2.0 project.
Yes it is. The compiler needs this attribute. Regardless of whether you define it yourself or use the one located in System.Core , it is up to you. Although in your specific case, System.Core not an option, since it is only part of .NET 3.5.
After upgrading to a later version 3.5 or later, you can completely remove this attribute from your project and simply use it in System.Core .
If you updated Visual Studio 2010; then you are using the c # 4.0 compiler. That means all you need is an ExtensionAttribute :
namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class ExtensionAttribute : Attribute { } }
It must be in this exact namespace.
Once you get this place, you can declare the extension method in the usual way (since you are using the C # 4.0 compiler):
public static class Extensions { public static string ToTitleCase(this string str) {
And then use it as an extension method:
var str = "hello world"; str.ToTitleCase();
You yourself should never really put ExtensionAttribute on anything; The C # 4.0 compiler does this for you. Essentially, all compiler needs should be able to find an attribute named ExtensionAttribute .
source share