static void Main(string[] args) { BaseFoo derivedBarAsBaseFoo = new DerivedBar(); BaseFoo derivedFooAsBaseFoo = new DerivedFoo(); DerivedFoo derivedFoo = new DerivedFoo(); derivedFooAsBaseFoo.DoSomething(); //Prints "Foo" when you might expect "Bar" derivedBarAsBaseFoo.DoSomething(); //Prints "FooBar" derivedFoo.DoSomething(); //Prints "Bar" }
public No27_ExpandFunctionOfInterfaceRestrictedMinimumByExtensionMethod() { //目的: //①インタフェースの利便性を高める //概要: //-------------------------------------------------------------------------------------- //インタフェースは定義したメンバ変数やメソッドを実装することを強制する //そのため、あれば便利といったメソッドをインタフェース内に定義できない //そこで拡張メソッッドを用いて、インタフェースに対してあったら便利な機能を付与し、機能拡張する //利用時 var a = 1; var b = 2; var result = a.GreaterThan(b); //false //注意 var foo = new DerivedFoo(); foo.UpdateMarker(); }