2

I've got a couple of extension functions that I want to transfer between classes.

I have a class called Helpers.cs that I want to have the following:

namespace XYZ
{
    public class Helpers
    {
        public static string Encrypt(this string plainText){
            //... do encrypting
        }
    }
}

In my other class Impliment.cs I want to have the following:

string s = "attack";
s.Encrypt();

How can I implement this?

3 Answers3

3

You're close - extension methods need to be in a static class:

public static class Helpers
{
    public static string Encrypt(this string plainText){
        //... do encrypting
    }
}

If you tried what you posted you'd get a pretty clear compiler error that says basically the same thing:

Extension method must be defined in a non-generic static class

Note that your usage will be slightly different that what you want. You can't do:

string s = "attack";
s.Encrypt();

becasue strings are immutable. Best you can do is overwrite the existing varialbe or store the result in a new one:

string s = "attack";
s = s.Encrypt();  // overwrite

or

string s = "attack";
string s2 = s.Encrypt();  // new variable
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • One extra bit of information is missing from this answer, the need to add a using statement at the top of the file, in this case `using XYZ`, I've took the liberty of making the edit for you, hope that's OK. – Doctor Jones Jul 16 '14 at 20:02
  • @DoctorJones all due respect, please don't edit others' answers to add your own content. Your comment is sufficient to indicate a `using` may be necessary. If the extension class is in the same namespace as the calling class, it will not be. I didn't think that was germane to the overall problem and didn't specify it. – D Stanley Jul 16 '14 at 20:09
  • Fair enough, but I can see that you decided to remove the information from your answer. People shouldn't have to look in the comments for part of the answer. Rewrite it and put it in your own words, but don't leave a vital part of the answer out. – Doctor Jones Jul 17 '14 at 08:14
2

You need to make the class, static as well and use a using statement.

Example

FileA.cs:

namespace XYZ {

    public static class Helpers {

        public static string Encrypt(this string plainText){
            //... do encrypting
            return plainText;
        }

    }

}

FileB.cs:

using XYZ;

public class MainClass {

    public static void Main() {
        string s = "input";
        s = s.Encrypt();
        Console.WriteLine(s);
    }

}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
1

To create an Extension Method, the class must be static, but in general it has to follow these rules:

  • The class must be public and static
  • The method must be public and static
  • The type you want to create the extension method, in C#, must have the first parameter with the this keyword before the parameter

In your case, change the class to be static , for sample:

public static class Helpers
{
    public static string Encrypt(this string plainText)
    {
         //... do encrypting
         // return a string;
    }
}

I also like to create the classes with name like Type and Extensions sufix, for sample: StringExtensions.cs, DateTimeExtensions.cs, etc.

Obs: Remember it is not a type method, it is just a static method, but you use as a method from a type.

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194