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

本文关键字:静态类 方法 我的 | 更新日期: 2023-09-27 17:59:45

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

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

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

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

您应该将它们设为static,以避免在类实例上浪费内存。

通常,任何不依赖于实例状态的方法都应该是静态的。

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

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

我想这取决于你的情况。

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

这个怎么样

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