Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# behavior on interface implementation

Tags:

c#

.net

mono

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.

like image 971
Paolo Falabella Avatar asked Dec 26 '13 13:12

Paolo Falabella


People also ask

What C is used for?

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 ...

Is C language easy?

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.

What is the full name of C?

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.


1 Answers

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, where I is the interface in which the member M is declared, is determined by examining each class or struct S, starting with C and repeating for each successive base class of C, until a match is located

like image 183
Ondrej Tucny Avatar answered Sep 23 '22 00:09

Ondrej Tucny