{love to code?}

October 16, 2009

How overriding Object.ToString() helps

Filed under: .NET — navaneethkn @ 7:37 AM
Tags: ,

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. (more…)

October 15, 2009

Understanding equality and object comparison in .NET framework

Filed under: .NET — navaneethkn @ 4:27 PM
Tags: ,

.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: (more…)

August 18, 2009

Circular linked list – An implementation using C#

Filed under: .NET, Algorithms — navaneethkn @ 9:27 AM
Tags: ,

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
(more…)

August 2, 2009

Thought process for writing recursive methods

Filed under: Algorithms, Back to school days, Beginner — navaneethkn @ 10:42 AM
Tags: , , ,

I was interviewing one candidate the other day and found that he is facing difficulties in writing a recursive method. This is not the first time I am seeing people facing problems with recursion. In this post, I will try to explain the thought process to write recursive methods.

This post is intended for beginners who don’t have any idea about recursion. If you are an intermediate or above level, you don’t find this as useful. (more…)

July 21, 2009

Single click builds for different environments using Visual Studio

Filed under: C# — navaneethkn @ 8:39 AM
Tags: , , ,

A neat build system is vital for all projects. Good build system should be capable to work without *any* user intervention. Here is what Joel Spolsky says on his post

Can you make a build in one step?

By this I mean: how many steps does it take to make a shipping build from the latest source snapshot? On good teams, there’s a single script you can run that does a full checkout from scratch, rebuilds every line of code, makes the EXEs, in all their various versions, languages, and #ifdef combinations, creates the installation package, and creates the final media — CDROM layout, download website, whatever.

Ask yourself the same question – Can you build your application in one step? (more…)

July 11, 2009

ADO.NET best practices – Reading data from data reader

Filed under: C# — navaneethkn @ 5:45 PM
Tags: , , ,

I have seen many people using DataReader incorrectly. In this post, I will try to explain some good practices that can be followed when reading from a data reader. Consider the following problematic code,

SqlDataReader reader = /* ... */;
while (reader.Read())
{
    string userName = reader["user_name"].ToString();
    int age = int.Parse( reader["age"].ToString() );
    /* ... */
}
reader.Close();

How many problems can you figure out from the above code? There are many problems with this code, (more…)

September 19, 2007

Writing a long string in multiple lines – c#

Filed under: C# — navaneethkn @ 1:41 PM
Tags: ,

Writing a big string will always produce scrollbars in the editor. So it’s better to split it to multiple lines which is more easy to read. This post discusses the various methods that can be used to split big lines into multiple lines.

Check this big line.

string BigLine = "bla bla  bla bla  bla bla  bla bla
bla bla  bla bla  bla bla  bla bla ";

Writing a string as shown above will produce error. This can be rewritten like

string BigLine = @"bla bla  bla bla  bla bla  bla bla
bla bla  bla bla  bla bla  bla bla ";

Or

string</font> BigLine = "bla bla  bla bla  bla bla  bla bla " +
"bla bla  bla bla  bla bla  bla bla ";

I always prefer the second one. Because first one will not move the next line to the starting of first line. And more over it won’t be possible to write first method for the following type of string.

string BigLine = @"bla bla  bla bla  bla bla  bla bla
bla bla  bla bla  bla bla\"test\"  bla bla ";

In this I am escaping " by specifying \. But .NET editor will produce an error.

Blog at WordPress.com.