Pages

Monday, July 4, 2011

C# and .NET interview questions: Explain in brief an interface?

Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing ‘Idisposable’ interface that means it has a functionality to release unmanaged resources. Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.
  • Single Class can implement multiple interfaces.
  • If a class implements a interface then it has to provide
    implementation to all its methods.

Following code shows that one has the interface definition and other
class implements the interface. Below is the source code “IInterface” is the
interface and “ClsDosomething” implements the “IInterface”. This sample just
displays a simple message box.

Public Interface IInterFace
Sub Do Something ()
End Interface

Public Class ClsDoSomething
Implements IInterFace
Public Sub DoSomething () Implements WindowsInterFace.IInterFace.DoSomething
MsgBox (“Interface implemented”)
End Sub
End Class

After implementing the above code you will get the following output as shown:


Following you can see a simple video on boxing and unboxing:


No comments:

Post a Comment