Guest User

If C# used return type in method signatures

a guest
Apr 23rd, 2011
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.62 KB | None | 0 0
  1. abstract class C {
  2.   abstract C SomeOp();
  3. }
  4.  
  5. class A : C {
  6.   C SomeOp() {
  7.     return SomeOp() as A;
  8.   }
  9.  
  10.   A SomeOp() {
  11.     return this;
  12.   }
  13. }
  14.  
  15. class B : C {
  16.   C SomeOp() {
  17.     return SomeOp() as B;
  18.   }
  19.  
  20.   B SomeOp() {
  21.     return this;
  22.   }
  23. }
  24.  
  25. class Foo {
  26.   void Bar() {
  27.     A a = new A();
  28.     B b = new B();
  29.     A otherA = a.SomeOp(); // no cast needed type inferred
  30.     B otherB = b.SomeOp(); // no cast needed type inferred
  31.     C c = a.SomeOp(); // no cast needed, type inferred
  32.     c.SomeOp(); // not ambiguous, only one method with this name
  33.     a.SomeOp() as A; // ambigous without assignment
  34.   }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment