Sunday, March 21, 2010

Singleton Class in C#

Singleton pattern is very popular pattern used by programmer. The idea behind the singleton pattern is to have only one instance of a class at any time. This kind of pattern is very useful for for heavy resources like Linq Data Context etc. It is also useful in multithreaded application where different thread of application try to crate instance of one class but it should be thread safe. It can be also useful for global resources and state objects. In singleton class there are two things required. One static variable which hold the instance of class and other is a static method which will return the instance of singleton class. Here is the example of singleton class

 public class MySingleTon
{
private static MySingleTon mySingleTonInstance = new MySingleTon();

private MySingleTon()
{
//private constructor
}
public static MySingleTon GetInstace()
{
return mySingleTonInstance;
}

}
Now we are done with calling single ton class like following.
 MySingleTon objSingleTon = MySingleTon.GetInstace(); 
The above example is not thread safe so it may be possible that different thread can create different instance of classes. To make above singleton class thread safe we need to change code like following.
   public class MySingleTon
{
private static MySingleTon mySingleTonInstance = new MySingleTon();

private MySingleTon()
{
//private constructor
}
public static MySingleTon GetInstace()
{
lock (typeof(MySingleTon))
{
if (mySingleTonInstance == null)
{
mySingleTonInstance = new MySingleTon();
}
return mySingleTonInstance;
}

}
}
Hope this will help you…

Shout it
kick it on DotNetKicks.com
Share:
Saturday, March 20, 2010

Visual Studio 2010-Automatically Adjust Visual Experience.

Visual studio 2010 is great IDE and i am discovering everyday some thing new. Today i have discovered one of fantastic option to increase performance of your visual studio IDE. Visual Studio 2010 has option when we enabled it it will automatically adjust the visual and client experience as per your hardware configuration. It will increase the performance of visual studio and productivity in lower hardware also. You can find that checkbox under Tools->General-> Automatically adjust visual experience based on client performance. There are two checkbox is given one for above and another for Rich User Experience but they may decrease your visual studio performance. Like following.

Visual Studio 2010, User Experience

Shout it
kick it on DotNetKicks.com
Share:

Indexer class in C#

While taking interviews for Microsoft.NET i often ask about indexer classes in C#.NET and i found that most of people are unable to give answer that how we can create a indexer class in C#.NET. We know the array in C#.NET and each element in array can be accessed by index same way for indexer class a object was class can be accessed by index. Its very easy to create a indexer class in C#. First lets looks some points related to indexer class in C#.

  • Indexer class object can be accessed by index only.
  • We can use same mechanism to access object of indexer class as we are doing it for array.
  • You can can specify any valid C# type as return type of indexer classes.
  • You must have to create a property with parameter using this keyword for indexer class.
  • You can also implement multi parameter indexer also.

Let’s create simple indexer class with single parameter and for string type.

 public class MyIndexer
{
private string[] MyIndexerData;
private int SizeOfIndexer;

//declaring cursor to assing size of indexer
public MyIndexer(int sizeOfIndex)
{
this.SizeOfIndexer = sizeOfIndex;
MyIndexerData = new string[SizeOfIndexer];
}

public string this[int Position]
{
get
{
return MyIndexerData[Position];
}
set
{
MyIndexerData[Position] = value;
}
}
}
Now we done with indexer class and lets create object of that indexer class and will see output of our console application also. Following is the code for that.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int indexSize=5;
MyIndexer objMyIndexer = new MyIndexer(indexSize);
for (int i = 0; i < indexSize; i++)
{
objMyIndexer[i] = i.ToString();
Console.WriteLine("Indexer Object Value-[{0}]: {1}",
i, objMyIndexer[i]);
}
Console.ReadKey();
}
}
}
and Here is the output for the indexer class console application.

Indexer Class Example in C#,Indexer Class,C#

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews