Interface and operator overloading Questions and Answers

By
1.) What is an interface in c#?                                                                                           
An interface is a reference type in c#.It is basically a kind of class with some differences,which are given below:-
  • All the members of an interface are implicitly public and abstract.
  • We can not declare the interface members as  static.
  • An interface can not contain constructors, destructors and constant fields.
  • An interface can inherit multiple interfaces.
 2.) Can interface contain one or more methods?                                                          
Yes, But none of them are implemented in the interface itself.
3.) How to define an interface in c#?                                                                                
Syntax:-
interface interface-name
{
member declarations;
}
 4.) Can we use Nested interface in c#?                                                                           
Yes.

5.) How can control the interface members in c#?                                                       
We can control the interface members by the Access-specifier or modifier in c#.
Ex.
  • public
  • protected
  • internal
  • private
  • New (for Nested interfaces)
6.) Why c# does not support multiple inheritance?                                                     
C# does not support multiple inheritance due to name collision.
More Details... (See the comment part)

7.) When we implement interfaces from different sources the Name collision create problem how?
Yes.
Ex.
Interface L1
{
void display();
}
interface L2
{
void Display();
}
class cls : L1,L2
{
public void display()
{
}
}

Description:- In above example ,we can call Display()  method with three technique.
  1. cls.Display()
  2. L1.Display()
  3. L2.Display()
Compiler will confuse ,which display method call due to name collision,compiler gives error message.
8.) How can implement interfaces in abstract methods?                                          
Example:
Interface L1
{
void Method();
}

abstract class cls : L1
{
....................
.....................
Public abstract void method ();
}

Note:- In above Example class cls does not implement the interface method.

9.) What is operator overloading?                                                                                    
C# has the ability to provide the operators with a special meaning for a data type.This special
meaning to an operator is known as operator overloading.  

10.) What are the Number of operators in c# that can be overloaded?                   
  • Binary arithmetic
  • Unary arithmetic
  • Binary Bitwise
  • Unary Bitwise
  • Logical Operators

11.What are the operators that is used in c# ,can not overloaded?                           
  • Conditional operators
  • compound assignment
  • Others([], (),=,?:,->,new ,sizeof, typeof,as etc.)
More Details...

12.How to define operator method in c#?                                                                       
Ex.
public static void operator operator-name(argument-list)
{
........................
Method body;
}

13.What are the features of operator methods in c#?                                                   
  • It must be defined as public and static.
  • It must be defined return type as void ,int ,others.
  • Number of  argument should be one for the unary operators and two for the binary operators.

14.What are the steps involved in overloading processing in c#?                              
  • Create a class or struct that defines the data type that is to be used in the overloading operations.
  • Declare operator methods (public or static)
  • Define the body of the operator method.
15.How to overload binary operator in c#?                                                                     
More Details...

16.How to overload Unary operator in c#?                                                                     
More Details...

17.How many argument are used in binary operator method?                                  
Two or More

18.How to overload Equal operator(==) in c#?                                                               
More Details...

19.What are the difference between overloading comparison operators and overloading arithmetic operators?
Comparison operators return a bool type value but arithmetic operators are not return bool type value.Apart from these differences,all other things are same in comparison and arithmetic operators

20.How many argument are used in Unary operator method?                                  
one

2 comments:

Powered by Blogger.