What is Static class?
Static classes are used to create data and functions that can be accessed without creating an instance of the class. We can access any members of Static classes without creating instance of the class.
Example:
//Start
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HowToBecomeAnArchitect.OOAD.StaticClass
{
static class Logger
{
public static int logLevel = 1;
public static void LogMessage()
{
//Write code here
Console.WriteLine("Logger:LogMessage() is executed");
}
}
class Demo
{
static void Main(string[] args)
{
Console.WriteLine("Main method is started");
Logger.LogMessage();
Console.WriteLine("Logger Level = " + Logger.logLevel);
Console.ReadKey();
}
}
}
//End
Note:
- Static classes contain only static members.
- Static classes cannot be instantiated.
- Static classes are sealed.
- Static variable is stored on the heap, regardless of whether it's declared within a reference type or a value type. There is only one slot in total no matter how many instances are created. This heap is separate from the normal garbage collected heap - it's known as a "high frequency heap", and there's one per application domain.
When to use Static classes?
- Static classes can be used when there is no data or behavior in the class that depends on any object.
- Static keyword will make your code a bit faster since no object creation is involved.
- Static classes used for logger and utility methods which often needed to access without creating any instances of the class to make faster and allocating with less memory.
- Static classes used for declaring the global constants for your project and these constants values read from config file and this is good practice for your development.
For example,
Config settings are
<appSettings>
<add key="MaxNoOfUsersAllowed" value="8" />
<add key="CachingServerName" value="au.cachingserver1.google.com" />
</appSettings>
Static class as follows
GoogleGlobal.cs
public static class GoogleGlobal
{
private static string cachingServerName = int.Parse(System.Configuration.ConfigurationManager.AppSettings["CachingServerName "].ToString());
private static int maxNoOfUsersAllowed = int.Parse(System.Configuration.ConfigurationManager.AppSettings["MaxNoOfUsersAllowed"]);
public static int CachingServerName { get { return cachingServerName; } }
public static int MaxNoOfUsersAllowed { get { return maxNoOfUsersAllowed; } }
}
Access Modifiers:
The following access modifiers are available in .NET
- 1. Private – Access is limited to within class only.
- 2. Protected – Access is limited to within class and to any class that inherits from the class.
- 3. Public - There is no restriction for accessing the public members.
- 4. Internal – Access is limited to the current project assembly.
- 5. Protected Internal – Access is limited to the current project assembly and any types derived from the class.
Example:
//Start
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HowToBecomeAnArchitect.OOAD.AccessModifiers
{
public class BaseClass
{
// For testing only
private int privateVariable = 1;
protected int protectedVariable = 2;
public int publicVariable = 3;
internal int internalVariable = 4;
protected internal int protectedInternalVariable = 5;
}
public class DrivedClass :BaseClass
{
public DrivedClass()
{
// print the BaseClass variables
//Console.WriteLine("Private Variable Value = " + privateVariable); //error on this line due to private variable
Console.WriteLine("Protected Variable Value = " + protectedVariable);
Console.WriteLine("Public Variable Value = " + publicVariable);
Console.WriteLine("Internal Variable Value = " + internalVariable);
Console.WriteLine("Internal Protected Variable Value = " + protectedInternalVariable);
}
}
class Demo
{
static void Main(string[] args)
{
Console.WriteLine("Main method is started");
DrivedClass drivedObj = new DrivedClass();
Console.ReadKey();
}
}
}
//End
Please visit the following URL to know more about how to use objects and classes.
http://msdn.microsoft.com/en-us/library/ms173109.aspx
No comments:
Post a Comment