Predefined Interface exposes some method,we are using that method but interface doent have definition part ,then how its working in c#? -
interface exposes method,we using method in class interface doent have definition part ,then how working in c#. example
class productnamecomparer : icomparer { public int compare(object x, object y) { product first = (product)x; product second = (product)y; return first.name.compareto(second.name); } }
here icomparer
exposes compareto()
method, icomparer
doesn't have compareto()
method definition part, how working?
interface
contract declares method rather having implementation part....class
implements particular interface
should have implemenation detail.
for instance, have ianimal
interface , dog
class implements ianimal
interface,
public interface ianimal { void walk(); // declares method, not implemenation } //class implementing interface //must define method specified in interface class dog : ianimal { public void walk() { console.writeline("dog can walk"); } }
in example, misunderstood icomparer
interface.
icomparer
defines interface compare()
method
whereas
compareto()
declared icomparable
interface.
in above code, have implemented icomparer interface so, defining compare()
method details in productnamecomparer
class.