What is interface casting for in C#? -
i understand how writing interface works in c#, example described here: codeguru explanation
interface iintelligence { bool intelligent_behavior(); } class human: iintelligence { public human() { //............. } /// interface method definition in class implements public bool intelligent_behavior() { console.writeline("........"); return true } }
i confused following process of interface casting:
human human = new human(); // human object casted interface type iintelligence humaniq = (iintelligence)human; humaniq.intelligent_behavior();
what sense of having class (human in case) implement interface, , casting instance human interface? question not how works, why done.
.net offers 2 types of interface implementations implicit implementation , explicit implementation.
when using implicit implementation , become part of type interface example if have iperson interface :
public interface iperson { string name{get;} }
and implement follows :
public class person:iperson { public string name{get; ....} }
you can access (implicitly):
aperson.name;
but if implement (explicitly) :
public class person:iperson { string iperson.name{get; ....} // notice there's no need include access modifier. }
then can accessed using iperson interface:
((iperson)aperson).name;
update:
actually ,explicit interface implementation allow implement different interfaces members have same name.(as shown in this tutorial)
Comments
Post a Comment