TL;DR
It doesnât have to be there. Itâs a way for us to âhideâ interface members.
What we are doing here is creating âExplicit Implementationsâ. When we implement an interface, all the members on that interface must be public but we donât necessarily want that to be the case. We donât really want to expose CaptureState
and RestoreState
to the rest of the system so, to do that, we implement these interface members as âexplicitâ.
But what does that mean? Well, it means that these methods will not be visible on the class. The only way these methods can be called is through an âinterface referenceâ (I think this is my own terminology). The saving system gets all ISaveable
classes. In some sense it doesnât get class references, it gets the âinterface referencesâ. So, it gets the classes but it knows nothing about them other than the fact that they implement the interface. The only methods you will see on any of the classes at this point is the interface methods (and perhaps some intrinsic methods and extension methods for the interface). Because it is an âinterface referenceâ and we defined these methods as âexplicitâ, these two methods are now public and visible and can be called from outside the class.
A small bit of code may explain it a little better
interface ISample
{
void Test();
void AnotherTest();
}
class Sample : ISample
{
void ISample.Test() // notice there's no access modifier here
{
// test stuff
}
public void AnotherTest() // normal way of implementing the interface
{
// more test stuff
}
public void ClassTest() // This is only on the class, not in the interface
{
// class test stuff
}
}
class Consumer
{
public void ConsumeClass(Sample classRef)
{
classRef.Test(); // will fail because there is no 'Test' in the class reference
classRef.AnotherTest(); // will succeed because interface members must be public
classRef.ClassTest(); // will succeed because it's on the class and public
}
public void ConsumeInterface(ISample interfaceRef)
{
interfaceRef.Test(); // will succeed because there is a 'Test' in the interface reference
interfaceRef.AnotherTest(); // will succeed because it's on the interface
interfaceRef.ClassTest(); // will fail because we don't know anything about the class
}
}
I hope this makes sense. I tried to explain it as simply as possible. Feel free to ask if thereâs anything I need to clarify