Professional Documents
Culture Documents
37 Interface
37 Interface
com/
ketkiacharya.net@gmail.com
Ketki Acharya
From: SM VITA ATC of CDAC
ketkiacharya.net@gmail.com
• Interfaces declared directly within a namespace can be declared as public or internal and, just like classes
and structs, interfaces default to internal access.
• Interface members are always public because the purpose of an interface is to enable other types to access
a class or struct. No access modifiers can be applied to interface members.
interface I1
{
void A();
}
interface I2
{
void A();
}
class C : I1, I2
{
public void chk()
{
I1 idemo = new C();
idemo.A();
}
public void A() //observe it is public and member of class
{
Console.WriteLine("C.A()");
}
}
public class program
{
public static void Main()
{
C c = new C();
I1 i1 = c; //even if do not type cast will it work???? yes
I2 i2 = c;
}
}
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Public and private implementation of interface
• One challenge with interfaces is that they may include methods that have the same signatures as existing class
members or members of other interfaces. Explicit interface implementations can be used to disambiguate
class and interface methods that would otherwise conflict. Explicit interfaces can also be used to hide the
details of an interface that the class developer considers private.
class C : I1,I2
{ void I1.A()
{ Console.WriteLine("I1.A()");
In this case we would get a compile error saying that C fails to implement I2.A(). We could fix this error by changing the first line to
class C : I1
but we'd get another compile error when trying to invoke A() as a member of C:
C c = new C();
c.A();
This time the compiler would report that class C does not contain a definition for method A(). We get the error because the explicit implementation of I1.A()
hides A() from the class. The only way to call I1.A() now is through C's I1 interface:
C c = new C();
I1 i1 = c;
i1.A();
return this.MemberwiseClone();
public PointDescription()
} {
}
PetName = "No-name";
}
USM’s Shriram Mantri Vidyanidhi Info Tech
} Academy
How to achieve deep copy
Point p4 = (Point)p3.Clone();
{ employee
if (temp
temp = obj as employee;
!= null)
}
employee[] p=new employee [5]; {
p[0] = new employee() { Name = "Raj", Salary = //if (this.Salary > temp.Salary)
50000 }; // return 1;
//if (this.Salary < temp.Salary)
p[1] = new employee() {Name="Anita",Salary =80000 }; // return -1;
// else
p[2] = new employee() {Name="Mona",Salary =30000 };
// return 0;
p[3] = new employee() { Name = "Geeta", Salary = return this.Salary.CompareTo(temp.Salary);
90000 }; }
else
p[4] = new employee() { Name = "Ravi", Salary = 70000 }; throw new ArgumentException("Parameter is not a employee!");
// throw new NotImplementedException();
Array.Sort(p);
foreach (employee e in p) }
cp
e 0 1 2 3 4
Name = "Raj",
Salary = 50000
Name = Name =
"Anita", "Mona",
Salary = Salary =
80000 30000
REmoveProduct
class Program
{
static void Main(string[] args)
{
company cp = new company();