简体   繁体   中英

Should my helper methods use Static classes in C#

I have a number of helper methods within helper classes. In my company I see that others use these helper methods as below:

var abc = new HelperClass()
var def = abc.doAction("ghi");

Is there a benefit to having these as non-static classes and having to create an instance each time around? Would it not be better to declare the helper class as static and do the following:

var def = HelperClass.doAction("ghi");

If I do the latter then do I need to declare both the helper class and the doAction method as static?

Here's an example of some code I use:

namespace Power.Storage.Helpers
{
    public class SimplerAES
    {
        private static byte[] key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
        private static byte[] vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 221, 112, 79, 32, 114, 156 };
        private ICryptoTransform encryptor, decryptor;
        private UTF8Encoding encoder;

        public SimplerAES()
        {
            RijndaelManaged rm = new RijndaelManaged();
            encryptor = rm.CreateEncryptor(key, vector);
            decryptor = rm.CreateDecryptor(key, vector);
            encoder = new UTF8Encoding();
        }

       ...

        public byte[] Encrypt(byte[] buffer)
        {
            MemoryStream encryptStream = new MemoryStream();
            using (CryptoStream cs = new CryptoStream(encryptStream, encryptor, CryptoStreamMode.Write))
            {
                cs.Write(buffer, 0, buffer.Length);
            }
            return encryptStream.ToArray();
        }

        public byte[] Decrypt(byte[] buffer)
        {
            MemoryStream decryptStream = new MemoryStream();
            using (CryptoStream cs = new CryptoStream(decryptStream, decryptor, CryptoStreamMode.Write))
            {
                cs.Write(buffer, 0, buffer.Length);
            }
            return decryptStream.ToArray();
        }
    }
}

Would it be correct to say this should not be static as it has a constructor that instantiates other classes.

You should make them static to avoid wasting memory on class instances.

In general, any method that does not depend on the state of an instance should be static .

Helper classes that contain nothing but static methods should themselves be declared static to prevent you from accidentally adding non-static members and from instantiating the classes.

Yes and Yes, preferably use static class with static functions.

What about this

namespace Power.Storage.Helpers
{
public class SimplerAES
{
private static byte[] key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
private static byte[] vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 221, 112, 79, 32, 114, 156 };
private static RijndaelManaged rm = new RijndaelManaged();

public byte[] Encrypt(byte[] buffer)
{
MemoryStream encryptStream = new MemoryStream();
ICryptoTransform encryptor = rm.CreateEncryptor(key, vector);

using (CryptoStream cs = new CryptoStream(encryptStream, encryptor, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
}
return encryptStream.ToArray();
}

public byte[] Decrypt(byte[] buffer)
{
MemoryStream decryptStream = new MemoryStream();
ICryptoTransform decryptor = rm.CreateDecryptor(key, vector);

using (CryptoStream cs = new CryptoStream(decryptStream, decryptor, CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
}
return decryptStream.ToArray();
}
}
}

I normally use static methods for helper classes, but if your helpers have state and perhaps they fit in a class that needs instantiation, then you should have instance methods.

I guess it is dependent on your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM