Showing posts with label c# interview question with answers. Show all posts
Showing posts with label c# interview question with answers. Show all posts

Saturday, July 27, 2013

C# interview question with answers :- What is Fragile parent class problem in c# ?

Inheritance is a great power by which developers can increase reusability in parent-child relationship. But with great powers comes greater responsibility and also problems. One of the problems which creep’s in is the “Fragile parent class problem”.


Take a simple scenario (below is the image) where we have a simple parent child class relationship. Assume that this child class is used in lot of projects and installed in lot of places. Now assume after the child class has been deployed across locations, after some months there are some changes in the parent class.

These changes can have cascading and unexpected behavior in child class as well. This unexpected behavior on child classes is termed as “Fragile class problem”.


Consider the below scenario where we have a parent class called as “DbParent” with a virtual method “Insert”.

class DbParent
{
     
        public virtual void Insert()
        {
            Console.WriteLine("Parent insert");
        }

}

Below is a simple child class with his own “Insert” implementation which is installed across various locations.

class DbChildClass : DbParent
{
       public void  Insert()
        {
            Console.WriteLine("Child inserts");
        }
}

Now let’s say after some months of deployment parent class developers without understanding the impact on child classes go and add a new method “Add”. This “Add” method calls the “Insert” method internally (below is the code snippet for the same).

class DbParent
{
public  void Add() // Adds method with out consulting
{
      this.Insert();
}

public virtual void Insert()
{
            Console.WriteLine("Parent insert");
}
}

Now client programs who invoke the “Add” method by creating child class objects expect that the “Child” class “Insert” implementation should be called.

DbChildClass o = new DbChildClass();
o.Add();
Console.Read();

But whoaaaa, if you run the below code you will see the parent class “Insert” is called which is not EXPECTED.









So the next question is what is the proper way of solving this issue?. First thing to my understanding (I can be wrong) there are no standard and common ways of solving the problem.

One of the thought which goes here is, if the base class is very fragile (will change time to time) then inheritance should not be allowed further. So by marking the classes as “sealed” or the fragile method as “sealed” this problem can be controlled.

Second way would be avoid calling virtual methods in the parent class in other methods.

I am myself hunting for proper standard solutions so if you have some better suggestions, please do write down the same in the comments below.

Also I would like to point out that .NET framework handles fragile parent class issues to a certain extent by itself.  For instance in the below code even though the child class method “HandleNumerics” has been passed an integer value, it is still invoking the “HandleNumerics” with double data type.

So the framework assumes that the child class method should be given preference rather than parent class.

class Parent
    {
        public virtual void HandleNumerics(int d)
        {
            Console.WriteLine("Parent numeric ");
        }

    }

    class child : Parent
    {

        public  void  HandleNumerics(double d)
        {
            Console.WriteLine("Child numeric");
        }
        
    }

    class Client
    {

        public static void Main()
        {
            child o = new child();
            int i = 100;
            o.HandleNumerics(i);
        }
    }

The above code will display “Child Numeric” rather than displaying “Parent Numeric”.

Below is a simple youtube video which demonstrates the above auto fragile handling methodology by .NET framework. Many c# developers think that this is a defect but it’s actually done “By DESIGN” to solve the “Fragile base class” problem.
  




Saturday, April 21, 2012

ASP.NET interview questions: - How will you explain differences between ASP and ASP.NET?

This is the most practical oriented ASP.NET Interview Questions which may be asked during the Interview by the Interviewer.

ASP.NET new feature supports are as follows:-

Better Language Support
  • New ADO.NET Concepts have been implemented.
  • ASP.NET supports full language (C#, VB.NET, C++) and not simple scripting like VBSCRIPT...
Better controls than ASP
  • ASP.NET covers large set’s of HTML controls..
  • Better Display grid like Data grid, Repeater and datalist.Many of the display grid havpaging support.
Controls have events support
  • All ASP.NET controls support events.
  • Load, Click, and Change events handled by code makes coding much simpler and much better organized.
Compiled Code
ASP.NET supports forms-based user authentication, including cookie management and automatic redirecting of unauthorized logins. (You can still do your custom login page and custom user checking).

Better Authentication Support
ASP.NET supports forms-based user authentication, including cookie management and automatic redirecting of unauthorized logins. (You can still do your custom login page and custom user checking).

User Accounts and Roles
ASP.NET allows for user accounts and roles, to give each user (with a given role) access to different server code and executables.

High Scalability
  • Much has been done with ASP.NET to provide greater scalability.
  • Server to server communication has been greatly enhanced, making it possible to scale an application over several servers. One example of this is the ability to run XML parsers, XSL transformations, and even resource hungry session objects on other servers.
Easy Configuration
  • Configuration of ASP.NET is done with plain text files.
  • Configuration files can be uploaded or changed while the application is running. No need to restart the server. No more metabase or registry puzzle.
Easy Deployment
No more server restart to deploy or replace compiled code. ASP.NET simply redirects all new requests to the new code.

See the following video on ASP.NET for importance of session and view state: -



Click for more ASP.NET interview questions

Regards,

Visit for more Authors’ blog on ASP.NET interview questions

Monday, January 9, 2012

C# interview question: - Explain partial methods in partial classes of C# and .NET?

Partial methods are important components of partial classes. Read and click on the following link in case you are new to partial classes.

Partial methods helps us to define a method definition in one of the physical files and we can implement that method in the other physical files as shown in the below figure.

In the below figure you can see we have defined “Validate” method in “Customer1.cs” and this validate method is implemented in “Customer2.cs”. Please note the partial keywords attached to both of these methods.

Figure: -
Also see the following video on Partial methods: -



Click for more learning stuffs on c# interview questions and answers

Regards,

Click it for author’s more training stuffs on Most asked c# interview questions

Thursday, August 11, 2011

C# and .NET interview question: - What are Named and Optional Arguments in C# 2010?

Visual C# 2010 introduces two interesting features called as Named and Optional Arguments.
Named Argument: - Named Argumentallows you to explicitly name an argument you
are passing to a method, constructors and properties with indexes – instead of
just identifying it by argument position.

For example: - suppose we have created a method named as “Multiply” with two
parameters namely “i” and “j”, just like below code snippet.

staticint Multiply(int i = 0, int j = 0)

{

return i * j;

}
Now, let’s see how we can call the above method and pass the value to the parameters using “Named Argument”.
You can call the method in one of the following ways: -

//Note that the position of parameters doesn’t matters while passing the values.
Console.WriteLine(Multiply(i: 10));
Console.WriteLine(Multiply(j : 5));

Console.WriteLine(Multiply(i : 10 , j : 5));

Console.WriteLine(Multiply(j : 5 , i : 10));
Because both parameters are optional, in cases where only one (or zero) parameters are specified then the default value for any non-specified arguments is passed.
Optional Argument: -Optional arguments enable you to omit arguments for some
parameters.

Parameters are optional when a default value is specified to a particular
argument.

For Example: -suppose we have created a method named as “Multiply” with two
parameters namely “i” and “j” and have set the value j = 5, just like below code
snippet.

staticint Multiply(int i = 0 , int j = 5)

{

return i * j;

}

Now, let’s see how we can call the above method and pass the value to the parameters using “Optional Argument”.
Note that VS 2010’s Intellisense indicates when a parameter is optional, as well as what its default value is when statement completion is displayed: -





In the above diagram you can see that the VS 2010 Intellisense indicates the parameter in square bracket which is optional with its default value.

Now, let’s demonstrate a small example on both the features to get a better idea.

Let first demonstrate an example for “Named Argument”.

Step1: - create a new project go to File > New > Project > Windows > Console
Application.







Step2: - Now, just create a method with two parameters like below code snippet.

staticint Add(int i , int j)

{

return i + j;

}

Just call the method in the main class like code snippet.

staticvoid Main(string[] args)

{

Console.WriteLine("*****Begin Test*****");

Console.WriteLine("");

Console.WriteLine("Example of Named Arguments");

Console.WriteLine("");

//Here the Named Argument has been Used.

Console.WriteLine("The Addition of two Numbers:"+ Add(j : 2 , i : 10));

Console.ReadLine();

}

The below diagram will show how exactly the VS 2010 Intellicense act while passing the value to the method parameters.






Now, let’s demonstrate an example for “Optional Argument”.

Step1:- Follow the same as we have done for the “Named Argument”.

Step2: - Create a method with two parameter namely “i” and “j” and also set
default value to j = 5 like below code snippet.

staticint Add(int i , int j = 5)

{

return i + j;

}

Just call the method in the main class like code snippet.
staticvoid Main(string[] args)

{

Console.WriteLine("*****Begin Test*****");

Console.WriteLine("");

Console.WriteLine("Example of Optional Paramater Arguments");

Console.WriteLine("");

//In the above code i have just passed the value for i only but

the value for j will be 5 by default.

Console.WriteLine("The Addtion of two Numbers:" + Add(10));

Console.ReadLine();

}

The below diagram will show how exactly the VS 2010 Intellicense act while
passing the value to the method parameters.





Related to interview, view the following video on mock of C# and .NET interview



Get more tutorials for C# and Dotnet interview questions 

Regards,

Also view author’s blog for Most asked Dotnet interview questions