Simple Example of Singleton Design Pattern in C# Example
UI Code:
ApplicationState state = ApplicationState.GetState();
state.LoginId="Kanchan";
state.RoleId= "Admin";
ApplicationState state2 = ApplicationState.GetState();
label3.Text = state2.LoginId;
label5.Text = state2.RoleId;
label6.Text = (state == state2).ToString();
Singleton Class Code:
class ApplicationState
{
private static ApplicationState instance=null;
// State Information
public string LoginId
{ get; set;}
public string RoleId
{ get; set; }
private ApplicationState()
{
}
//Lock Object
private static object lockThis = new object();
public static ApplicationState GetState()
{
lock (lockThis)
{
if (ApplicationState.instance == null)
instance = new ApplicationState();
}
return instance;
}
}
No comments:
Post a Comment