I've found a behaviour in c# and I would like to know if it's in the specs (and can be expected to work on all platforms and new versions of the .NET runtime) or if it's undefined behaviour that just happens to work but may stop compiling at any time.
So, let's say I want to take existing classes, like these:
public class HtmlTextBox
{
public string Text {get; set;}
}
public class HtmlDiv
{
public string Text {get; set;}
}
now I would really like them to implement a common IText interface, like this one:
public interface IText
{
string Text {get; }
}
but I can't change the classes directly because they are part of an external library. Now there are various ways to do this, through inheritance or with a decorator. But I was surprised to find out that doing simply this compiles and works on .NET 4.5 (windows 7 64 bits).
public class HtmlTextBox2 : HtmlTextBox, IText {}
public class HtmlDiv2 : HtmlDiv, IText {}
That's it. This gets me drop-in replacements for HtmlTextBox
and HtmlDiv
that use their existing Text
property as implementation for IText
.
I was half-expecting the compiler to yell at me, asking me to provide an explicit re-implementation of Text
, but on .NET 4.5 this just works:
IText h2 = new HtmlTextBox2{Text="Hello World"};
Console.WriteLine(h2.Text); //OUTPUT: hello world
In fact,I've tried the same on mono (whatever version ideone.com is using) and mono does not yell at me either
So I guess I'm good to go, but before trying this on serious code I wanted to check if I've misunderstood what is really happening here or if I can't rely on this to work.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Yes, this is expected behavior. The implementation of an interface's method needs not to be done in the class where the interface is actually applied; it can be in any ancestor class.
The C# Language Specification 5.0 documents this in section 13.4.4; excerpt of the rule:
The implementation of a particular interface member
I.M
, whereI
is the interface in which the memberM
is declared, is determined by examining each class or structS
, starting withC
and repeating for each successive base class ofC
, until a match is located
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With