我的帮助器方法应该在C#中使用静态类吗

时间:2011-06-21 23:38:34

标签: c#

我在帮助程序类中有许多辅助方法。在我的公司,我看到其他人使用这些帮助方法如下:

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

将这些作为非静态类并且每次都必须创建实例有什么好处?将辅助类声明为静态并执行以下操作会不会更好:

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

如果我执行后者,那么我是否需要将helper类和doAction方法声明为静态?

以下是我使用的一些代码的示例:

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();
        }
    }
}

说这不应该是静态的是正确的,因为它有一个实例化其他类的构造函数。

3 个答案:

答案 0 :(得分:11)

你应该让它们static以避免在类实例上浪费内存。

一般来说,any method that does not depend on the state of an instance should be static

只包含静态方法的Helper类本身应该声明为static,以防止您意外添加非静态成员和实例化类。

答案 1 :(得分:0)

是和是,最好使用带静态函数的静态类。

这个怎么样?

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();
}
}
}

答案 2 :(得分:0)

我通常对辅助类使用静态方法,但是如果你的助手有状态,并且它们可能适合需要实例化的类,那么你应该有实例方法。

我猜你的情况是依赖