I have a number of helper methods in helper classes. In my company, I see others using these helper methods, as shown below:
var abc = new HelperClass() var def = abc.doAction("ghi");
Is there any advantage to having these non-static classes and instantiating every time? It would not be better to declare the helper class static and do the following:
var def = HelperClass.doAction("ghi");
If I do the latter, do I need to declare both the helper class and the doAction method as static?
Here is an example of the code I'm using:
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(); } } }
It would be correct to say that this should not be static, since it has a constructor that creates instances of other classes.