Monday, March 22, 2010

C# 4.0-string.IsNullOrWhiteSpace()

We already have string.IsNullOrEmpty() method to check whether the string is null or empty.Now with Microsoft.NET Framework 4.0 there is a new another method that is called string.IsNullOrWhiteSpace() which will also check whether string is having whitespaces or not with null and empty string.This method is help full where we can check string is having whitespace rather then its empty or null. It will return a true if a string is empty,null or its having whitespaces. Let’s create a simple example and will see how its works. Here we are going test it with 4 options Empty string,Normal String,White spaces and Null String. Following is a code for that.

class Program
{
static void Main(string[] args)
{
string TestString = string.Empty;
Console.WriteLine("Test with empty string:{0}",
string.IsNullOrWhiteSpace(TestString));
TestString = "Normal String";
Console.WriteLine("Test with normal string:{0}",
string.IsNullOrWhiteSpace(TestString));
TestString = " ";
Console.WriteLine("Test with whitespace:{0}",
string.IsNullOrWhiteSpace(TestString));
TestString = null;
Console.WriteLine("Test with null:{0}",
string.IsNullOrWhiteSpace(TestString));
Console.ReadLine();
}
}
Following is a output of the above code here you can see its returning false with Normal string and for all other options its returning true as expected.
image

Shout it
kick it on DotNetKicks.com
Share:
Sunday, March 21, 2010

C#-Constructors,Static Constructors and Destructors Execution in Inheritance

While taking interview for .NET Technologies i often ask about the execution sequence of the constructor and destructor in inheritance But from the my experience i have found that lots of people are still confused with execution sequence of constructor and destructors. Lets create a simple example and learn some basic things that is very important while using inheritance in C#.

  • Constructors will be executed in from parent to child sequence means first parent class constructor will be executed then after that child class constructor will be executed.
  • Destructors execution order is reverse then constructors first it will execute child class destructor and then it will execute the parent class destructor.
  • Static constructors are different then the normal constructors and its executes when first object of class is created it will be executed. Most of people are very confused this kind of scenario in inheritance. Here scenario will be like when the first object of child class created then it will execute the child class static constructor and then after the parent class static constructor is executed. After that it will never got executed.

Lets create a simple class which will illustrate the above worlds. First lets create a class A with constructor,destructor and a static constructor.

public class A
{
static A()
{
System.Console.WriteLine("A Static Constructor");
}
public A()
{
System.Console.WriteLine("A public constructor");
}

~A()
{
System.Console.WriteLine("A Destructor");
}
}
Now we will inherit this class with the another class B which is also having constructor,destructor and a static constructor.
public class B : A
{
static B()
{
System.Console.WriteLine("B Static Constructor");
}
public B()
{
System.Console.WriteLine("B public constructor");
}

~B()
{
System.Console.WriteLine("B Destructor");
}
}
Now lets create two objects of Class B in main function of our console application to see how constructors works and after that we will destroy the object via assigning null values and then forcefully we will do Garbage Collection via GC.Collect() to see how destructors works below is the code for that.
 class Program
{
static void Main(string[] args)
{
B b1 = new B();
B b2 = new B();
//code return to destroy object of a class
b1=b2 = null;
GC.Collect();
Console.ReadLine();

}
}
After running the console application output will be as follows.

Construcotr-Destructor in Ineritance,C#

As you can see static constructors are only executed when the first object of a class is created and Constructors are executed like parent to child way and in reverse destructors are executed from child to parent way. Hope this will help you understand execution sequence of constructor in inheritance.

Shout it
kick it on DotNetKicks.com
Share:

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:

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