.NET and c# Interview Question and answers: – If we want to update interface with new methods, what is the best practice?

This is an awesome c# interview question. Kudos to the interviewer who has asked this c# question and double kudos to the candidate if he answers this c# question.

The biggest use of interface is to ensure that strict CONTRACT is followed between clients and components. So that when changes happen on the components clients do not have to change too much.  In real world CONTRACTS between two parties do not change which implies that interfaces should also not change.
So if you want to add new methods or change methods in interfaces the best practice is to create new interfaces by inheriting. With this approach your older client who are using the interface stay happy and the new clients who want those new or changed methods get the benefit of the new functionality.

Let’s consider you have a simple interface called as “IView” which helps you to view data , below is the code for the same. Let’s consider that this interface is consumed by many clients.

interface Iview

{

public void View();

}

Over a period of time some users demand “Edit” functionality as well but the rest of the users are happy with the ‘View” functionality and they do not want to get affect by these changes. Now if you go and change this interface ( “IView”) a.k.a you will be disturbing everyone.

So the best option would be to add a new interface i.e. “IEdit” which inherits from “IView”. So the “IEdit”  will have the functionality for “Edit” as well as “View” because it’s also inheriting from “IView”.  And the clients who are consuming “IView” do not need to update as “IView’ is not changed at all.

interface IEdit : Iview

{

public void Edit();

}

Answering this c# interview question in simple sentence.

Interface once fixed should not be changed. If we ever have to add new functions, new interfaces should be created so that we do not break compatibility with old clients.

Recently one of our friends was asked an awesome c# interview question, how can we debug c# threads?. Click on this video to see how we can debug threads

Taken from the best selling c# and .NET interview question book, you can see more about the book by clicking on interview questions and answers for .NETHere’s our site for interview questions and answers for .NET

and  SQL Server interview question book.

Regards,

Click to view more from author’s on C# and interview questions and answers for Dotnet

 

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s