How overriding Object.ToString() helps

System.Object provides a ToString() method which gives a human readable string about the object. Many people haven’t realized how useful is this tiny method.

Consider the following code:

namespace PersonDetails
    class Person
        public Person(string firstName, string lastName, int age)
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

    class Program
        static void Main(string[] args)
            Person chuckNorris = new Person("Chuck", "Norris", 20);
            Console.WriteLine(chuckNorris.ToString()); // Prints PersonDetails.Person

The information printed is not at all helpful. Let us override ToString() and see the difference. Continue reading

Understanding equality and object comparison in .NET framework

.NET framework provides several methods to do comparison and equality check for objects. It can be pretty confusing at the beginning by looking at the documentations for each interfaces that framework offers. In this post, I will try explain the possibilities with examples.

Basically, there are two kinds of equality in .NET. Reference Equality and Value Equality. Default is reference equality. To understand reference equality, consider the following class: Continue reading

Disabling a custom control from Visual Studio’s toolbox

When a custom control is created, Visual Studio will show it in the toolbox for dragging and dropping. This can be annoying when you create a composite control which is a combination of several other custom controls where VS will display all custom controls in the toolbox. System.ComponentModel.ToolBoxItemAttribute can be used with the controls that you want to hide from toolbox. Apply this attribute to all the controls that you want to hide from toolbox.

Sample code

public class MyCustomControl : Label

MyCustomControl will not be displayed on the toolbox.

Circular linked list – An implementation using C#

In this post, I will explain about creating a circular doubly linked list using C#. .NET framework provides a doubly linked list implementation in System.Collections.Generic.LinkedList<T> class . But this class is not providing the behavior of a circular linked list and it is very tough to extend for supporting circular linked list requirements.

In a normal doubly linked list, each node will have a link to its previous and next nodes. In a circular doubly linked list, tail node’s next node will be head and head node’s previous node will be tail. Here is an image taken from wikipedia which visualizes circular linked list.

Circular linked list
Continue reading

What Great .NET Developers Ought To Know (More .NET Interview Questions and Answers) – Part1

In this series of posts, I will try to answer the questions posted by Scott Hanselman here. This part we will be seeing the first set of questions he has given under Everyone who writes code heading.

Q) Describe the difference between a Thread and a Process?

A Thread is a small unit of code in execution. A process is an instance of program which will have multiple threads running. A process can host multiple threads. Thread will always belong to a process. Communication between multiple processes are difficult but communication between threads is easy.

Continue reading