Sometimes we just want to have one and only instance of a class. Like a class used for logging or a class used for modifying registry settings or some configuration files. If we have more than one instance of this class we could run into a mess and confusion. To solve this we have the singleton class.

Singleton class is class which has only one instance and cannot be intantiated more than once.

Singleton classes need to be implemented as shown below.

using System;

namespace LearnCs
{
public class Singleton
{
/// <summary>
/// We have a static variable to hold a instance of this class.
/// </summary>
private static Singleton SingletonInstance;
private static object dummyObject = new object();
// Why do we need this dummyObject?? U will learn later
/// <summary>
/// A private constructor ensures that no one can instantiate this class
/// by using new keyword outside this class
/// </summary>
private Singleton()
{

}

/// <summary>
/// Get Instance method returns the one and only instance of Singleton class.
/// </summary>
/// <returns>One and only instance of Singleton</returns>
public static Singleton getSingletonInstance()
{
if(SingletonInstance == null)
SingletonInstance = new Singleton();

return SingletonInstance;
}

//Other methods corresponding to this class here
}
}

Please note that this could run into problem if multiple threads call getSingletonInstance() method for first time.

If we know that multiple threads could call this getSingletonInstance() method, then use the lock keyword in the following manner inside that function.

public static Singleton getSingletonInstance()
{
if (SingletonInstance == null)
{
//SingletonInstance = new Singleton();
// The above line should not be used if multiple threads
// are going to access this method simultaneously.
// lock makes sure that the access to this portion of code
// is synchronized by various threads.
lock (dummyObject)
{
if (SingletonInstance == null)
SingletonInstance = new Singleton();
}
}
return SingletonInstance;
}

But remember, using lock might decrease the performance of your code upto 100 times. So better avoid it, if not needed. Rather try to initialize SingletonInstance variable in the beginning itself !!

ASP.NET Listbox or DropDown Listbox is basically a HTML select control.
We can use Javascript to manipulate the listbox at client side.

Following is the code snippet for doing that

To remove all options from a listbox:

 function clearListBox(listboxID)
 {
 	// returns 1 if all items are sucessfully removed
	var mylistbox = document.getElementById(listboxID);
	if(mylistbox == null)
	  return 1;
	while(mylistbox.length > 0)
	{
	  mylistbox.remove(0);
	}
	  return 1;
 }

To add an options to a listbox:

 function AddOptions(listboxID,text,value)
 {
	var mylistbox = document.getElementById(listboxID);
	var myOption = new Option(text, value);
	mylistbox.options[mylistbox.options.length] = myOption;
 }

This is a basic thing but can be quite confusing also!! Let me give a try to explain this….

Methods without definitions in base class (are basically abstract methods) and
must be implemented in derived class with “override” keyword. The “new” keyword cannot be used here.

A method with definition in base class is normal (which doesn’t have “virtual” keyword)
There is no need to override/hide it in the derived class.
But it “can” be hidden in derived class with “new” keyword

Consider a method with definition in base class which has “virtual” keyword, this “can” be overridden in derived class with “override” or hidden using “new” keyword.

Overriding Vs Hiding

When an reference of abstract class contains instance of the derived class
and an method found in both classes is called, then here we can note a difference in it.

New keyword simply hides the method in base class.
So its actually present and can be called from a reference of the base class.

But override overwrites the method in base class.
So a overridden method in base class is not called when called from a reference of a base class.

Note this example:


using System;

namespace aruns_code
{
abstract class abs
{

	// A method without definition in base class is abstract 
	// and should be implemented in derived class 
	// with "override" keyword
	public abstract void absMethod();

	public void nonAbs()
	{
		Console.Write("inside nonAbs of abs\n");
	}

	public virtual void nonAbs2()
	{
		Console.Write("inside nonAbs2 of abs\n");
	}
}

class der : abs
{
	public override void absMethod()
	{
		Console.Write("inside absMethod of der\n");
	}


	// A method with definition in base class is normal one
	// and can be overridden in derived class with new keyword
	public new void nonAbs()
	{
		Console.Write("inside nonAbs of der\n");
	}

	// A method with definition in base class is normal 
	// but has virtual keyword
	// and "may" be overridden in derived class 
	//with "override" or "new" keyword
	public override void nonAbs2()
	{
		Console.Write("inside nonAbs2 of der\n");
	}
}

class testHiding
{
	[STAThread]
	static void Main(string[] args)
	{
		abs absObj = new der();
		absObj.absMethod();
		absObj.nonAbs();
		absObj.nonAbs2();
	}
}
}

/*

OUTPUT IS :

inside absMethod of der
inside nonAbs of abs
inside nonAbs2 of der
Press any key to continue 

 */

So you got it. If you still did not get it, then please put a comment below. I will get back to you !!

You could have seen a object initialization like this

A a = new B();

where B is a derived class of A. But we cannot do the other way around.

B b = new A(); // because A is an abstract class

Take a look into the below example and its output.You can understand how it works.

using System;
using System.Text;

namespace aruns_code
{
	class testAbstract
	{
		[STAThread]
		static void Main(string[] args)
		{
			der derObj = new der();
			derObj.nonAbs();

			Console.WriteLine("\n\n");

			abs absObj = new der();
			absObj.nonAbs();
			absObj.absMethod();
		}
	}

	class der : abs
	{
		public der()
		{
			Console.Write("initialized der\n");
		}

		public override void absMethod()
		{
			Console.Write("inside der\n");
		}

		public new void nonAbs()
		{
			Console.Write("inside nonAbs of der\n");
		}
	}

	abstract class abs
	{
		public abs()
		{
			Console.Write("initialized abs\n");
		}

		public abstract void absMethod();

		public void nonAbs()
		{
			Console.Write("inside nonAbs of abs\n");
		}
	}

}

The following is the output.

initialized abs
initialized der
inside nonAbs of der

initialized abs
initialized der
inside nonAbs of abs
inside der
Press any key to continue

See that, when an instance of derived class is created, it’s initializing the base class also. This explains why it happened so!! Also note the selection of methods done here (whether from derived or base class).