Wednesday, August 24, 2011

.Net Interview Question:- Can we declare Interfaces as private?

Sometimes I am surprised with some questions which come up during .NET interview and this one is one of them. I really do not understand why people ask such questions and how useful it is practically.
Most of the developers jump to the conclusion and say “NO”.

Let me first prove that how the developer are also right by saying that
Interfaces cannot be declared as private and later we will see that how they are
also wrong(incorrect).

Now, let first declare an interface as private and try to compile it and see
that what the compiler says about it.

private interface I1

{



}




In the above diagram you can see that the compiler throws an error saying that Elements defined in a namespace cannot be explicitly declared as private.

Now, let’s prove the second point that interfaces can also be declared as
private.

In the below code snippet you will see that I have declared the interface as
private and try to build the solution, will see that what the compiler has to
say about it.

class MyClass

{

private interface I1

{

}

}


In above diagram you can see that now the compiler has successfully build
solution without throwing the error this means that we can declare Interfaces as
private also but the interfaces declared as private should be declared inside
the class.
Note: - To declare Interfaces as Private you need to declare it inside the class.

Following is the video which shows that how the questions are asked C# and .NET interviews.



Please click here to see more interview questions and answers for .NET

Regards,


Visit Authors blog for more Most asked Dotnet interview questions

No comments: