Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
ISampleInterface a = new A();
ISampleInterface b = new B();
a.SampleMethod();
b.SampleMethod();
//Console.WriteLine(a.myValue); // can't do it!! a is not A
Console.WriteLine(a.GetType()); // uhm...
}
}
interface ISampleInterface
{
void SampleMethod();
}
class A : ISampleInterface
{
public double myValue = 10.0;
public void SampleMethod() {
Console.WriteLine("A");
}
}
class B : ISampleInterface
{
public double myValue = 20.0;
public void SampleMethod() {
Console.WriteLine("B");
}
}
}
I init a class by interface (implemented by the class).
Obviously, can't access to a.myValue
, because correctly Rextester.ISampleInterface
doesn't contain such a definition.
But if I ask to compiler which type is a
, it outputs Rextester.A
(which is not, I believe).
Why? And more important, which kind of class is a
? A sort of hybrid-sliced class limited by its interface? Not sure how I would define it...