C# interview questions and answers: – Explain the use of Icomparable in c#?

This question is taken from the book .NET interview questions written by Shivprasadkoirala and published by BPB publications.

“IComparable” interface helps to implement custom sorting for collections in c#. Let us try to understand the same with a simple c# example. For instance let’s say you have a list collection of people names as shown in the below code.

List 1

Now if you run the above program you will see that we have not applied sort on the collection. It displays data as they were  inserted.

List 2

But now if you call “Peoples.Sort()” method on the collection  you will get the following sorted output on the “name”  value in ascending position.

List 3

But now let’s say that we need the person’s age also to be added in the list with the person name value. So logically you will create a “Person” class with “name” and “age” property as shown in the code below.

List 4

Once you create the class you would create an object of the person class and add it to the list collection as shown in the below code snippet.

List changes 1

But if you now try to call the “Sort” method on the List , he gets confused ?. On which value should he sort on “Name” or “Age” ?.

List 7

If you run the application it will throw up the below exception text. The exception text says that he is confused and he needs specific direction on how the sorting should work. This direction can be provided by using “IComparable” interface.

List 8

So in order to show a proper direction for the sort logic we need to implement “IComparable” interface as shown in the below. The logic of “Sort” needs to be defined in the “CompareTo” method.

List changes 2

You can see in the “CompareTo” method we have defined the logic saying if the “age” value is same then sort by using the “name” value or else use “age” value for sorting. If you run the application you would get the following output. You can see for “40” and “30” value he has sorted on the basis “Age” but for the remaining people the age is same so he sorted on the “Name” value.

List 11

So the use of “IComparable” interface is to define custom sorting logic on a collection.

For further c# interview preparation you can go through the following articles

Huge collection of c# interview question videos: – c# interview question videos .

Read this article on how to prepare for c# interviews: – c# interview questions and answers

Huge collection of c# and .NET interview question videos: – c# and .NET interview question videos.

Number one site for c# and .NET interview questions :-http://www.dotnetinterviewquestions.in/



About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s