4
Answers

In which scenario u have to use static class with example

Anjali Khan

Anjali Khan

7y
240
1
Hi frnds When we use static class ? Please give the real time scenario with example?
Answers (4)
0
Tapan Patel

Tapan Patel

NA 8.1k 100.8k 7y
Assume that you are working on an application that heavily relies on logging data to some desitnation.
If you are using log4net or application insights, you will create a separate class that does this for you.
Every time, request comes in to log something from your application, ideal way would be to create an object of this class as below and then call LogData method.
Instead of doing that, you can do something like below using static keywords (where you will not be creating an object of logger class everytime)
 
  1. public class Logger  
  2. {  
  3.     //some other memebers in class  
  4.     public Logger()  
  5.     {  
  6.           
  7.   
  8.     }  
  9.     public void LogData(object[] data)  
  10.     {  
  11.         //log data  
  12.     }  
  13.   
  14.   
  15. }   
  16.   
  17. in your calling application code:  
  18.     Logger  _logger = new Logger();  
  19.     Logger.LogData(array);  
 
Using static keyword: making class static, you don't want any one else to create an object of this class and let this class only handle the way of logging. (like creating ILog objects and how it should log the data)
 
 
  1. public static class Logger  
  2. {  
  3.     //some other memebers in class  
  4.       
  5.     public static void LogData(object[] data)  
  6.     {  
  7.         //log data  
  8.     }  
  9. }  
  10.   
  11. in your calling application code:  
  12.     Logger.LogData(array);  
 
Another example would be to open or close connection to SQL server or Oracle DB. Where you can easily make a call to methods to close or open the connection using class.MethodName.
Let's say your static class would make appropriate connection to either sql server db or oracle db based on the data you are providing to this method.
Instead of your calling class making decision what method to call (either sql logging or oracle logging), let logger class only decide what to do if data is of specific type.
So in all cases, you will end up making call to Logger.LogData() method and that class will handle the rest instead of you making call to Logger.LogSQL() or Logger.LogOracle().
When you define static class to make SQL connection, this class will control the behavior of how it should connect to sql server and what needs to be done in case of closing the connection.
So, when you don't want other parts of applications to control the behavior of certain functionality, you should put that in static class, doing thus, you are making sure that functionality won't be changed by other classes.
 
Accepted
0
Tapan Patel

Tapan Patel

NA 8.1k 100.8k 7y
Anjali, I have very limited knowledge on MVC/Web development.
But, I found this to start with:

http://www.c-sharpcorner.com/UploadFile/ff2f08/creating-custom-validation-attribute-for-data-annotation/ 
0
Anjali Khan

Anjali Khan

NA 738 41.3k 7y
Hi tapan Thanks for the update.its a really helpful to me.can u provide to me custom validation means how to create in mvc like chk box
0
Manas Mohapatra

Manas Mohapatra

NA 29.3k 3.3m 7y
Static class which all members and methods needs to be static. 
 
Need to create instance before calling the methods
Cannot have more than one constructor
Constructor cannot have any parameter
Access modifier cannot be use with constructor
Constructor is called before calling any method internally
Cannot be instantiated
Cannot be inherited because by default it is sealed
 
https://forums.asp.net/t/1748030.aspx?In+which+scenario+should+we+use+a+Static+Class+
 
https://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp