19 November 2014

MVVM: Binding ListBox item double click event to a command

With intention of having 'no code' in code behind file, we often invest lot of time in finding how to invoke a command in your ViewModel whenever a UIElement raises an event. I certainly invested couple of hours googling.. binging for readymade answers. I couldn't find a satisfactory answers (may be I am poor in using right keywords ). But, with help of couple of posts in sites like StackOverflow & MSDN, I arrived at this very simple approach.

Below is my ViewModel which has a collection called Cities which I will be displaying in a ListBox in my View. My ViewModel has a command called ItemSelectedCommand which should be incoked when user double clicks on city.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/// <summary>
/// ViewModel for CityView
/// </summary>
public class CityViewModel : ViewModelBase
{
    /// <summary>
    /// Command that responds item double click event.
    /// </summary>
    public DelegateCommand ItemSelectedCommand { get; private set; }

    /// <summary>
    /// Gets or sets the selected city.
    /// </summary>
    public string SelectedCity { get; set; }

    /// <summary>
    /// Gets the list of cities.
    /// </summary>
    public List<string> Cities { get; private set; }

    /// <summary>
    /// Initializes new instance of <see cref="CityViewModel"/> class.
    /// </summary>
    public CityViewModel()
    {
        ItemSelectedCommand = new DelegateCommand(OnItemSelected);
        Cities = new List<string>() { "Bangalore", "New York", "Sydney", "London", "Washington" };
    }

    /// <summary>
    /// This method is executed when user double clicks on a city.
    /// </summary>
    /// <param name="sender">Unsed Parameter.</param>
    private void OnItemSelected(object sender)
    {            
        //Do the processing you need
    }
}


And here is the XAML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<ListBox HorizontalAlignment="Center" ItemsSource="{Binding Cities}" SelectedItem="{Binding SelectedCity}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}">
                <TextBlock.InputBindings>
                    <MouseBinding Gesture="LeftDoubleClick"                                           
                                  Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.ItemSelectedCommand}"/>
                </TextBlock.InputBindings>
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>                  
</ListBox>


I hope there is no need to explain the code and I hope you enjoyed reading this :)

4 September 2013

A look at extension methods and method overloading

Extension methods are a great way to inject our own methods into some other classes which greatly improve the extension of functionality, code re-use etc. Everything is well and good. However, we often miss some important details which might frustrate us. One such thing with extension methods is - CLR always chooses the closest method than closest match when an overloaded extension method is defined. Confused? Hmm... Let me take you directly into some code.

Below is simple class hierarchy.

class Parent {}

class Child : Parent {}

class GrandChild: Child {}

Very straight forward, isn't it? Now let me create a business class as below,

class MyClass
{
   public void Display(Parent objA) //------------------------------- (A)
   {
       Console.WriteLine("I am not an extension method");
   }
}

And now let me define an overladed extension method for MyClass,

static class Extensions
{
   public static void Display(this MyClass m, Child c) //-------------(B)
   {
       Console.WriteLine("I am an extension method");
   }
}

Ok. Done with setup. Before going further, you might want to have a relook at above classes and their relations. Anyways, below is my consumer class wherein I want to create an object of MyClass and call Display on it.

class Consumer
{
    public static void Main()
    {
       Child child = new Child();

       MyClass obj = new MyClass();
       obj.Display(child);
    }  
}

So, Could you guess what would be the output of above code? From our OOP and C# knowledge we tell that it will call extension method Display because it exactly matches the caller signature. Right? However, that's not correct. In this case, CLR prefers non-extension method (A) over extension method (B) even though the (B) perfectly matches caller's signature. It means, CLR picks the method 'Display' which is inside MyClass than the extension method 'Display' which is outside of MyClass.

Now just comment out Display method inside and run the code and  this time the extension method is called. This perfectly makes sense because when a method is called, CLR first search MethodInfo table of the class on which the method is being called. If it find a multiple method definitions in the same class, it chooses the best match. If it doesn't any entry, it will search up the inheritance hierachy. If it doesn't find any match, then it will look for any extension methods.

I hope you enjoyed reading this post and also might save you some time  when you encounter similar issues related to extension methods.

3 April 2013

MVP of the year

On  Thursday, January 24, 2013, I was a surprised to see my inbox that I am awarded as MVP of the year 2012. I am one among the 32 people awarded as MVP of the year and only person from India. It feels great to receive such a great award. Here is a official site that tells the story,

Developer MVPs of the Year

Thank you Microsoft for such a great honor. I also thank my MVP lead Tanmay Kapoor and Biplab Paul for their support.
 

30 September 2012

Creating RCW Interops from ActiveX (OCX) Controls

Many times we need to use ActiveX controls (ocx) developed in VB6 or C++ in our .NET applications. As you might aware of that, we can't directly use ActiveX components in .NET application. So, we need to create wrapper assemblies around the ActiveX components which we call Runtime Callable Wrappers as I have already talked about that in my previous posts. I am not going to discuss details of RCW, but I will show how to generate the RCWs.
 
You can generate RCW in two ways
1. Using Visual Studio
2. Using Tools TlbImp.exe and AxImp.exe.
 

Using Visual Studio to generate RCW:
This is the simplest way of generating RCWs. Then in your project, open the toolbar, right click on it and select 'Choose controls'. Browse the .ocx file and click OK.
 
 
Now open the 'obj' folder present inside the project folder and you will see two interop dlls are generated namely AxInterop.<FileName>.dll and <FileName>.dll. For example, if the control name is MyControl.OCX then generated assemblies will be AxMyControls.dll and MyControls.dll. The prior one contains the visible components of the user control that can be placed on the form. And later one contains the types.
 
 
 
Using Tools to generate RCW:
Above method of generating RCW fulfils most of the times. However, if you want to have some control over how the interops are generated like default namespace, strong naming etc., you can't do much with Visual Studio. However, for that you can use framework tools to generate RCW yourself.
AxImp.exe and TlbImp.exe are two such tools that ship with NET Framework SDK. In fact AxImp.exe alone is sufficient to generate required interops. We will how that works now. Suppose you have an ActiveX control called MyControl.OCX. Open the visual studio command prompt, change the directory to the folder where MyControl.ocx resides and run the below command.
 
AXIMP MyControl.OCX
 
This will generate two interops AxMyControl.dll and MyControl.dll. The default namespace in the prior one will be AxMyControl and default namespace in MyControl.dll will be MyControl. If you want to change the out putfile name to AxInterop.MyControl.dll, you can run the below command,

AXIMP MyControl.OCX /OUT:AxInterop.MyControl.dll
 
This will generate AxInterop.MyControl.dll and MyControl.dll. You can see that when using AXIMP tool, you do not have any control over other interop dll i.e. MyControl.dll. You also do not have control over default namespace name. So, if I want to give your own names say AxInterop.MyControl.dll and Interop.MyControl.dll with custom namespaces say LegacyControls, then I will do it as below:
First generate the interop assembly containign type definitions using TLBIMP tool as below,

 TLBIMP MyControl.OCX /OUT:Interop.MyControl.dll /namespace:LegacyControls
 
If you want the generated assembly to be strong named, you can supply the strong name key file to above command (as below)

TLBIMP MyControl.OCX ... /KEYFILE:MyCompanyKey.snk
 
Now generate the ActiveX interop using AXIMP tool using the above generated RCW,

AXIMP MyControl.OCX /OUT:AxInterop.MyControl.dll /RCW:Interop.MyControl.dll
 
and of course you can sign this assembly just like as explained before.
 
Now you have generated the two interops AxInterop.MyControl.dll and Interop.MyControl.dll with having namespace AxLegacyControls and LegacyControls respectively.
Note: You must register the OCX before you can use the generated interops.

15 June 2012

Interface Vs. Abstract Class, Why and When

Recently many people asked me this question - what is the difference between an abstract class and an interface? - This is a typical interview question and you can easily find answers like below in any .NET book.
  • abstract classes can contain pure abstract methods as well as concrete methods. But all methods in an interface are implicitly abstract
  • Methods in an abstract class can be protected, private, static etc. But, all items in an interface are implicitly public
  • A class can inherit multiple interfaces, but it can inherit only one abstract class.
  • etc. :)
But, in my view, one shouldn't compare interfaces and abstract classes because each of them have their own strength and weaknesses. I have also seen many people who go dumbfounded if someone asks them - Why do you use an interface? Or Why do you use an abstract class? I suggest you to go through this MSDN  page which gives you an idea of when to use what. In this blog, I will try to explore my knowledge on these two concepts.

Abstract Class
 
An abstract class is just another class with abstract keyword. But, the keyword 'abstract' adds some additional features to the class like - the class can contain just method declarations, an instance of the class cannot be created. So, below one is an abstract class.
 
public abstract class Animal
{
   public void Walk()
   {
      Console.WriteLine("Walking");
   }
   public void Sleep()
   {
      Console.WriteLine("Sleeping");
   }
   public abstract void Eat();
}
 
This class defines the basic characteristics of an animal like every animal sleeps, Moves, eats etc. But look at the Eat function. I have made it abstract in the sense - every animal can eat but what it eats depends upon the kind of animal.
 
class Cow : Animal
{
   public override void Eat()
   {
      Console.WriteLine("Eating grass");
   }
}
class Tiger : Animal
{
   public override void Eat()
   {
      Console.WriteLine("Eating meat");
   }
}
 
I hope above example doesn't need any explanation. You can see that abstract class defines an IS-A relationship. Which means Cow is an Animal, so Cow has all the basic characters of an Animal. e.g. Move and Sleep functions need not be re-defined in Cow class.

Also note that, If I add any new behaviour to base class, child classes need not be changed at all and they simply inherit the new behaviour. For example, if I define a new behaviour 'Drink' in animal class, both the child classes simply adapt the new behaviour immediately.

public abstract class Animal
{
   ...
   public void  Drink()
   {
       Console.WriteLine("Drinking water");
   }
   public abstract void Eat();
}
 
So, visioning is not a problem when you use abstract class. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface. Hence, If you want to create multiple versions of your component, create an abstract class.

One more thing to be noted that all these 2 classes Tiger, Cow are closely related i.e. both are Animals and share common behaviour of an animal. So, when the classes are closely related and share common functionality - Use Abstract classes. 

Interfaces 

In contrast to abstract class, an Interface defines a CAN-DO relationship rather than IS-A relationship. For example, the definition of String class looks like below,
 
class String : IComparable, ICloneable, IConvertible
..
 
This definition tells you that - you can compare string objects, you can clone string objects, you can convert string objects into other type.

Next thing is, when the classes are not closes related and you still want to have a common functionality among them - Use Interfaces. Let's consider an example,

 
public interface IEquatable<T>
{
   bool Equals(T other);
}
public class Person: IEquatable<Person>
{
   private string Name;
   private DateTime DateOfBirth;
   public bool Equals(Person other)
     {
      return other.Equals(other.Name) && other.DateOfBirth == this.DateOfBirth;
   }
}
public class Home : IEquatable<Home>
{
   private int HouseNumber;
   private int AreaCode;
   public bool Equals(Home other)
   {
       return other.AreaCode == this.AreaCode && other.HouseNumber == this.HouseNumber;
   }
}

 
Here 2 non-related classes Home and Person want to have Equality function. See that functionality 'Equals' is common between the 2 classes but the implementation is completely different. Hence, both Home & Person implement IEquatable interface but implement Equals method independently.

Now you can ask, if the implementations are different, what is the need of using interface? Of course, just remove the IEquatable interface and the program still works. So, why interface? Let me try to make it clear step by step.

Firstly, An interface acts as a CONTRACT. This means, any class that implements an interface agrees to implement all the methods in that interface. Also note that, (ideally) once implemented, interface (contract) definition cannot change i.e. you cannot add new methods and modify existing ones in an interface. This is very important aspect of interfaces. This assures the consumer of your class that, the class will implement all the methods defined in the interface contract though the implementation can change. For example, I know that Person class implements IEquatable interface. Hence I am guaranteed that Person class always contains Equals method. Hence, I am ensured that my below code always works.

IEquatable<Person> person1 = new Person();
IEquatable<Person> person2 = new Person();
bool areTheyEqual = person1.Equals(person2);

Having this, a new version of Person class is released and Equals method now includes a check for address of person also.
public class Person: IEquatable<Person>
{
   privatestring Name;
   private DateTime DateOfBirth;
   private Address Address;
   publicbool Equals(Person other)
     {
      return other.Equals(other.Name) &&
             other.DateOfBirth == this.DateOfBirth &&
             other.Address == this.Address;
   }
}

See that, though the implementation has changed, my consumer code  still works. So, having an interface implemented makes your class Backward compatible.

Secondly, interfaces are quite useful when you want call a method on dynamic object. For example, below helper function prints all the items in a collection - A List, Array, String etc.

public void DisplayAllItems<T>(IEnumerable<T> collection)
{
   foreach (var item in collection)
   {
       Console.WriteLine(item);
   }
}

In the above function, you can pass a List, array or any object that implements IEnumerable interface. Otherwise, we would need to implement the display logic for each collection type separately.

These are 2 examples of why to use interfaces. There are other lot of reasons for using interfaces like laying out complex architectures, implementing design patterns etc. But, I hope this blog gives you an idea of why to use interfaces.

That concludes the discussion of differentiating between interface & abstract class, which one to prefer & when. I hope you enjoyed reading my blog. Please put your feedback which help me in improving myself and also to help others.

4 April 2012

Query KB updates installed in Window 7

Prior to Windows Vista, WMI's Win32_QuickFixEngineering class was used for querying the KB updates installed in the system. But, Starting with Windows Vista, win32_QuickFixEngineering returns only the updates supplied by Component Based Servicing (CBS). It will not return updates installed by MSI or Windows Updates. Read this page for more info. So, to query the updates installed via MSI or Windows Updates, you can use Windows Update Agent Object Model. Here I am going to show you, how to query the installed KB updates in C#.

We begin by adding reference to Wuapi.dll. You can do that by selecting "WUAPI 2.0 Type Library" from COM tab in Add Reference dialog in Visual Studio.


Once you add reference to Wuapi.dll, you can create an object of WUApiLib.UpdateSession class. This class cab be used by the caller to perform operations that involve updates, such as download, install or search for updates. Once you have UpdateSession object ready, you can call QueryHistory on it to get history of installed updates. Let me show you the code directly.

WUApiLib.UpdateSession session = new UpdateSession();
IUpdateSearcher us = session.CreateUpdateSearcher();

var updatesCollection = session.QueryHistory(String.Empty, 0, us.GetTotalHistoryCount());
for (int i = 0; i < updatesCollection.Count; i++)
{
    Console.WriteLine(coll[i].Title);
}

Note that, the list of updates resulted from above code may not match with the list of updates in Add/Remove Programs window in control panel.  The missing updates might be installed via Component Based Servicing or they are non-microsoft updates.

References:
  •  Searching, Downloading, and Installing Updates (link)
  •  Using the Windows Update Agent API (link)

24 February 2012

Memory Leaks in .NET Application - Don't let them slip through your eyes.

Before talking about the main topic, I would like to briefly go through the CLR garbage collection mechanism.  When a .NET application is executed, CLR allocates a block of memory which is called managed heap. This managed heap is logically divided among 3 generations - Gen0, Gen1 and Gen2. Usually Gen0 contains the newly created and short lived objects. So, here is a quick view on what happens during garbage collection -
  1. Whenever generation 0 gets full, garbage collection occurs. During garbage collection, the garbage collector examines each object in Gen0 to know whether the object is a root. A root  object is one which has a valid reference (or simply the object is still in use). After the root examination, garbage collector collects all non-root objects and frees the memory occupied by them and the root objects which survived the garbage collection are moved to Gen1. At the end of garbage collection, Gen0 will be 100% empty.
  2. Now, suppose the application needs more memory than which is available in Gen0. So, Garbage collection must occur which collects objects in both Gen0 and Gen1. Again, the garbage collection starts from identifying root objects in Gen1. All the non-root objects are collected and their memory is reclaimed. The survived objects (roots) will be moved to Gen2. Then Garbage collector collects Gen0 objects as explained in Step1.
  3. Sometimes Later the application might require more memory than which is available in Gen0 & Gen1 together. So, the garbage collection must occur on all three generations. So, Garbage Collector starts examining the objects in Gen2. All non-root objects are collected and their memory is freed-up. The survived objects are going to remain in Gen2 only. Then Garbage collector collects Gen1 and Gen0 objects as explained in Step1 & 2.
What is a Memory Leak?

What we can see from the above explanation is that until an object has a root it is going to remain in memory and is always promoted to higher generation. Having this said, let's see what is memory leak.

Consider you have a class which holds a reference to an unmanaged handle as shown below.

class UnmanagedClass
{
         IntPtr handle;
         Int32[] someBigArray = new Int32[200000];  //a dummy array to hold sufficiently large memory.

         public UnmanagedClass()
         {
                 handle = GetUnmanagedHandle(); // consider this method returns an unmanaged handle
         }
}

Then I will create an instance of above class as below,

private void MemoryLeakTest()
{
        for(int i = 0;  i < 1000000; i++)
        {
                String str = new String();
                UnmanagedClass uc = new UnmanagedClass();  //doesn't
        }
}

In the above function, after every loop, both sr & uc become eligible for garbage collection. Suppose, after 5 loops, generation 0 becomes full and GC must run. See that after 5 loops there are 5 string objects and 5 UnmanagedClass objects are created on heap. Garbage collection starts and sees that all five string objects have no roots. So, it frees the memory occupied by the string objects. Then it starts examiniting the Unmanaged class objects. But, each uc object has an unmanaged root. Since GC cares for only managed object but not unmanaged objects, it will not examine the unmanaged handle. Hence, it treats all 5 UnmanagedClass instances to be roots and moves them to Generation1. At this point the generations look like below.


Ultimately Generation0 becomes empty and the loop starts executing again. Now, again after 5 loops Gen0 becomes full and GC occurs. As explained previously, all 5 Unmanaged objects are treated to be roots and they are moved to Generation1. But, there may not be sufficient space in Gen1 to accumulate all objects that survived in Gen0 collection. So, GC has to run on Gen1 as well. Hence, GC starts examining UnmanagedClass objects in Gen1. Again GC sees that they contain a valid handle hence they are moved to Gen2. At this point Gen1 and Gen2 have 5 UnmanagedClass objects each and Gen0 is empty.



In the same way, after another 5 loops, the 5 UnmanagedClass objects in Gen1 will survive GC and moved to Gen2 and Gen0 objects will be moved to Gen1 and the picture looks like below,


You can see that now the generation2 is getting full and it has to be garbage collected. But, again, all the objects in contain an unmanaged handle and they will not be collected at all. Hence, at the next GC, there will be no memory to left in Gen2 to move any objects into it. At this point, you can say that the application is leaking memory.

So, if the application continues to run, at some point of time, there will be no memory left to allocate any objects and CLR will throw OutOfMemoryException and process terminates.

How to avoid Memory Leaks?
  • If your class has an unmanaged handle, implement Finalize and Dispose pattern to release the unmanaged handle. This article gives you an overview of Dispose pattern and this page shows you how dispose an unmanaged handle.
  • If you are a consumer of a class that implements IDisposable, as a developer, you are responsible for calling Dispose on it. Ensure, all Disposable objects are disposed or at least those objects that do not have finalizers.
  • Avoid static collections. You might know that a type loaded in memory is never loaded until the application is shutdown. Since, static members are type members, they are going to stay in the memory always. So, use static members carefully.
I hope you enjoyed reading this article. Happy Programming.

12 February 2012

GC.AddMemoryPressure - Working with native resources.

In this writing, I am going to explain how to deal with a situation where an object occupies a large amount of unmanaged memory while consuming very little managed memory. For example, suppose you are using a Bitmap object in your application. The Bitmap application can consume a lot of native memory. But your application just uses the handle to Bitmap which just uses just 4 bytes in 32 bit machines and 8 bytes in 64 bit machines. This means, your application could create several Bitmaps before the garbage collection kicks in. But at the same time, the native memory consumption by the process can increase enormously.

Let me show you an example. In figure A below, I have allocated 2 bitmaps, each of which occupies some big amount of native memory. But you can see that managed heap just containes wrapper to the Bitmaps which occupy very less memory. I will go ahead and create 2 more bitmaps (figure-B). You can see that native memory usage is gradually increasing while there is still enough memory on managed heap. Since, there is enough memory available on the heap, garbage collection doesn't kickin. So, if some more bitmaps are created and garbage collection doesn't occur, then you might run out of native memory which can result in catastrophic failures.



To deal with such problems, System.GC class  provides two static methods - AddMemoryPressure and RemoveMemoryPressure, whose signature is like below.

public static void AddMemoryPressure(long bytesAllocated);
public static void RemoveMemoryPressure(long bytesAllocated);

To know the advantage of these two methods, have a look at the below BitmapObject class. This class is a wrapper around Bitmap. For simplicity, the constructor accepts an image file and constructs a Bitmap.

class BitmapObject
{
    private System.Drawing.Bitmap _bitmap;
    private Int64 _memoryPressure;

    public BitmapObject(String file, Int64 size)
    {
        _bitmap = new System.Drawing.Bitmap(file);
        if (_bitmap != null)
        {
            _memoryPressure = size;
            GC.AddMemoryPressure(_memoryPressure);
        }
    }
       
    public System.Drawing.Bitmap GetBitmap()
    {
        return _bitmap;
    }

    ~BitmapObject()
    {
        if (_bitmap != null)
        {
            _bitmap.Dispose();
            GC.RemoveMemoryPressure(_memoryPressure);
        }
    }
}

Whenever you want to create an instance of System.Bitmap, you can consider creating an instance of above class instead. For instance, I want to create a Bitmap which can have approximately 5MB size. So, I will create an instance of BitmapOject by passing fileName and size as below,

BitmapObject oBitmap = new BitmapObject("c:\\SomePicture.bmp", 5 * 1024 * 1024);

When the constructor executes, it first creates a Bitmap and stores it reference in _bitmap. Then the constructor calls GC.AddMemoryPressure method passing the size (5MB) to it.  This gives CLR a hint of how much native memory is actually occupied by the object. So, though only 4 bytes (or 8 bytes in 64 bit machines) in managed heap, clr assumes that the object actually consumes 5MB. So, suppose Managed heap is of 50MB, then creating 10 instances of BitmapObject makes CLR think managed heap is full and hence it enforces garbage collection. When the garbage collection occurs, the finalizer method executes disposes the bitmap and removes the memory pressure.

10 February 2012

Contravariance: Is it really what it is?

By definition, contravariance referes to a situtaion where you assign base class instance to a child class reference. Let's directly see an example,

class BaseClass
{
}

class ChildClass: BaseClass
{
}

class TestContraVariance
{       
    delegate void SomeContraVariantDelegate(ChildClass argument);

    public void CreateContravariantDelegate()
    {           
        SomeContraVariantDelegate cvd = new SomeContraVariantDelegate(EventHandlerMethod);
    }

    private void EventHandlerMethod(BaseClass arg)
    {
    }
}

Here, you can see the contravariance - Delegate SomeContraVariantDelegate expects a method that accepts ChildClass instance. But, the function EventHandlerMethod accepts an argument of type BaseClass. So, it appears that a base class object is assigned to a child class object which is not possible in normal scenarios. But, is it really what it seems to be? I don't think so. Let me explain,

In the above example, if you see carefully, the delegate calls the method but not method calls delegate. So, when delegate calls EventHandlerMethod it sends a ChildClass instance as parameter. This is perfectly valid because the method accepts BaseClass instance and the delegate passes ChildClass instance. So, actually ChildClass instance is assigned to BaseClass object but not the other way.

So, there exists nothing like assigning assigning BaseClass instance to ChildClass object and hence no contravariance like thing exists too.

29 December 2011

CLR Optimizations - String Interning

In my previous post, I explained how the immutable nature of strings can hurt the performance of your application. In this post, I am going to explain how CLR optimizes string handling through a technique called String Interning.

To begin with, let's take a look at below simple program.

static void Main()
{
    String s1 = "tiger";
    String s2 = "tiger";

    //compare the values of s1 and s2
    bool valuesEqual = String.Equals(s1, s2);

    //compare the references of s1 and s2
    bool referenceEqual = Object.ReferenceEquals(s1, s2);
}

When you execute this program, valuesEqual will be true which is expected to be true since both s1 and s2 contain the same value "tiger". What about the value of referenceEquals variable? there is a twist here. You expect referenceEquals to be false because both s1 and s2 are completely different objects and hence their references should be different. But wait, the value of referenceEquals will also be true!

To proceed further, just change the value of s2 to something else say "lion" and run your program. Now, valueEquals is false which is expected. Also referenceEqual is false too. Now, if you are wondoring why referenceEquals was true in first case. The answer is - it was because the result of String Interning, an optimization technique adapted by clr for string manipulation. So, let's understand what is String interning.

When you run your application (say the above program iteself), CLR creates an internal hash table. Initially the hash table will empty. Then, string s1 is created on heap with value "tiger". Now, an entry is made in the hash table where key will be "tiger" and value will be reference to string object created on the heap.




You see that s1 is created on the heap and the address (reference) of the object is stored in hash table for "tiger". Then, the CLR sees the second instruction String s2 = "tiger"; Now, instead of creating a new string object on heap, it first searches the hash table for the key "tiger" and it will defnitely find an entry. This means that a string "tiger" already exists on the heap whose reference is 0x100. Hence, CLR simly stores the reference from hash table into s2. This way, creating of a new object is avoided and thereby saving memory.



Later  if s2 is assigned a different value say "lion", then CLR will first search for the key "lion" in hash table. But it will not find any entry in hash table. Hence, a new String object will be created on heap for "lion". Also, a new entry will be made in the hash table with key being "lion" and value being reference to new object on heap.



Pretty interesting right? By adapting string interning mechanism, CLR efficiently controls the creation of strings. If you think that this feature is quite useful and want to take advantage of it, you can refer MSDN for String class's static methods Intern and IsInterned.

Apart from above said advantanges, this mechanism has also some disadvantages. The additional overhead in creating & maintaining hash table, repeated hash table lookups can hurt the performance of your application. If you think, string interning hurts your application performance, you can turn this feature off by supplying assembly level attribute "CompilationRelaxationAttribute" with value "CompilationRelaxation.NoStringInterning". But there is a catch here. Even if you supply this attribute, CLR may ignore this attribute and use String Interning. So, just be aware of this.