Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A
dotnetinter

Can you explain change existing code without touching it?( .NET Interview questions)

If you looking for last minute .NET interview revision start from this



Let’s understand how we normally work first?

  1. We create class library with some functionality and reuse it at multiple places.

  Example:

code 1

Note: Above code will be used by more than one client code.

     2.    Test our code
     3.    Deploy the application

Now sometime in the future we came across a situation where one of the client wants some changes in the MyMethod function and we start putting if condition inside the MyMethod function.

  • Now because I have made changes in MyMethod function I have to test all my existing clients along with one who was seeking a change in class library.

  • It also violates OCP – Open closed principle – Software modules should be closed for modifications but open for extensions.

Now the question is then what will be the best solution?
Answer is polymorphism.
At the time of creation of the class we should have anticipated such issue and we should have decorated our MyFunction method with virtual modifier.

code 2

Now whenever changes will be asked we can go and create new child class and MyClass and override and MyMethod function. Client Code which was seeking a change in the functionality will use new class.
Example:

code 3

Old Clients
MyClass m=new MyClass();
m.MyMethod1();
m.MyMethod2();
m.MyMethod();

Client seeking change
MyClass m=new NewMyClass();
m.MyMethod1();
m.MyMethod2();
m.MyMethod();

Recently one of our friends in a .NET interview was asked about the importance of yield keyword , below goes the explanation for the same.



Hope you understood the concept and enjoyed reading it. Don’t forget to subscribe us on facebook.

If you are looking for some technical training on any Microsoft technologies likeWCF,MVC,WCF,BI or any practices like Design Patterns, UML, DDD etc visit www.sukesh-Marla.com or contact SukeshMarla@Gmail.com
Tags: .net interview questions, .net yield interview questions, c# interview questions, csharp interview questions, it interview questions with answers, programming interview questions
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments