I was experimenting on this thing and I found that C# actually allows you to have non-static, non-abstract method definitions inside an interface, which made totally no sense to me as it is not according to the rules of Object Oriented paradigm. Here's the code you may test out in your systems which actually compiles and runs successfully the way you would expect it to.
Interface Test : -
public interface Test
{
public void Display() {
Console.WriteLine("Display called from interface");
}
}
Class Derived that implements test: -
public class Derived : Test
{
public void SomeTestMethod()
{
Console.WriteLine("Just a method");
}
}
Program.cs : -
class Program
{
static void Main(string[] args)
{
Test instance = new Derived();
instance.Display();
}
}
This code would run perfectly without any issues producing the output "Display called from interface" in the console. This makes absolutely no sense to me as to why is this even allowed. Won't this just re-introduce the diamond problem or the issue of having ambiguity in methods defined in two interfaces when both of them are implemented by a single class. I am not sure if this is something that has been introduced in newer versions of C# or is it a .Net Core thing. If anyone knows the reason please let me know. Thanks in advance.