Explicit interfaces implementation in C#
Forums:
interface IText {
string AsText();
}
interface IHtml {
string AsText();
}
class UID : IText, IHtml {
public int Id { get; set; }
string IText.AsText() {
return Id.ToString();
}
string IHtml.AsText() {
return String.Format("<span>{0}</span>", Id);
}
}
var uid = new UID {Id = 314};
var text = ((IText) uid).AsText();
//text is "314"
var html = ((IHtml)uid).AsText();
//html is "<span>314</span>"
- Log in to post comments