Wow - I am struggling here:
Okay: what is an interface and why do we us it
An interface defines a "contract". What is a contract A contract is a collection of Type members such as simple predefined types (bool, string, integer, etc), Methods (Create, DoSomething(), HandleSomething(), etc.), etc.
We define contracts so that we will have verifications AT COMPILE TIME that defined contract members are implemented. An interface NEVER gaurantees that ANY implementation is correct (does IsValid properly validate and return 'true' when valid - the interface does not know that - hence, unit tests). All we know when we implement an interface is that ALL contract members are included and that the "Type" retuened is the correct "Type".
This class has a static member and this works just fine in C#:
public class SomeClass {
public static bool Foo{};
}
What I cannot do now is define any static members in an interface and here is the "chain" that is used as a reason:
public interface IMyInterface {
static bool IsValid {get;}
}
public class MyClass1: IMyInterface {
static bool IsValid {
get { return true; }
}
}
public class MyClass2: IMyInterface {
static bool IsValid {
get { return false; }
}
}
static class Test {
static void Run() {
MyClass1 obj1 = new MyClass1();
IMyInterface myInterface = CreateInstance(obj1);
}
public static IMyInterface CreateInstance(IMyInterface myInterface) {
return myInterface.IsValid();
}
}
The question posed: What should be returned
Well, its easy! Is it not
MyClass1 was instantiated and MyClass1 returns 'true". We then cast obj1 to IMyInterface as 'myInterface' and then we have the equivalent of a class within a class when we try to create a duplicate static instance (which would be retarded by the way and is simply used as a to answer the question: what happens in this scenario )
So, what "should happen "
Well, we should have a reference in memory to the instance of MyClass1.IsValid which is what is returned within the "duplicate" member (CreateInstance). So, obviously it "should" return 'true'.
Of course this would be rediculous and I see only the worst o programmers ever wanting to do such a thing. Let us consider this: it is possible now if we dont use the iterface, but the actual instance as the defined Type (I.e. MyClass1 versus IMyInterface). However, and VERY IMPORTANTLY - do not use the incorrect defintion of static as mentioned previously: that MyClass1 must have the same static reference as MyClass2: they are separate Types and static members are associated to a Type. For some odd reason I keep getting told that static members should be the same between different types and this is simply false!
So, again static members are defined in actual Types just fine, and they cast just fine when we don;t use an interface.
An interface should define a "contract" and simply enforce that ALL members of the contract are, in fact, implemented.
The other benefit with an interface is that referencing the interface allows for access to interface members and only interface members are passed around. Think of a Winform: the actual instance has many members, but implementing an interface allows passing instances of that interface and ONLY those interface members which is great for performance and memory allocation. So, interfaces also allow for gaurantees of contracts AND allows for ONLY the contract to be passed around.
Lastly, we get the compiler checking that proper implemntations are enforced and we get compiler errors if we forget to include a particular member, a member changes, or the members returns an incorrect type. This is where the problem exists! The compiler is too limited with interfaces and does not allow us to require static implementations in an interface and I want to know WHY
I am still waiting for a reply that is not easily resolved. The benefits are normous, so where is a reason that cannot be resolved