If my helper methods use static classes in C #

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.

+4
source share
3 answers

You must make them static in order to avoid memory loss on class instances.

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

Helper classes that contain nothing but static methods must themselves be declared static to prevent accidentally adding non-static elements and instantiating classes.

+8
source

Yes and Yes, it is advisable to use a static class with static functions.

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

I usually use static methods for helper classes, but if your helpers have a state and maybe they fit into a class that needs to be instantiated, then you should have instance methods.

I suppose that depends on your situation.

0
source

All Articles