|
Listing 12-13 extracted from chapter
Inheritance, polymorphism and abstraction
Listing 12-12< > Listing 12-14
This listing can be compiled with the command line: csc.exe /target:exe Example_12_13.cs Errors: 0 Warnings: 0
Example_12_13.cs
interface I {
void SetState(int i);
int GetState();
}
struct Struct : I {
private int i;
public void SetState(int i) { this.i = i; }
public int GetState() { return i; }
}
class Program {
static void Main() {
Struct s = new Struct();
// Here, there is an implicit boxing of the instance 's'.
I i = (I) s;
s.SetState(10);
i.SetState(20);
System.Console.WriteLine( "s.GetState() returned:" + s.GetState() );
System.Console.WriteLine( "i.GetState() returned:" + i.GetState() );
}
}
Copyright Patrick Smacchia 2006 2007
|