Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a base-class method return this, even in a derived class?

I'd like to be able to have a method in a C# base class, callable on objects of several derived classes, that returns the object itself, and have the CLR know what type the object really is - i.e., the appropriate derived type. Can someone suggest a way to do it? Other, of course, than return type covariance, which C# doesn't have.

Something like this, except that Method()'s return type should be the type of the derived class, not the base:

public abstract class Base { 
    public Base Method() { return this; }
}

public class Derived1: Base { ... }

public class Derived2: Base { ... }

public class Main {
    public static int Main() {
        Derived1 d1 = new Derived1();
        Derived1 x = d1.Method();
        Derived2 d2 = new Derived2();
        Derived2 y = d2.Method();
    }
}

I can only think of two ways to make this work, and I don't like either of them:

  1. Cast the result of Method() to the expected type (e.g., Derived1 x = (Derived) d1.Method();). But casts are the tool of the Devil, and besides, the intent of the method is to return a Derived1 or Derived2 or ..., not a Base.

  2. Declare Method() as abstract in the base and implement it separately in each derived class. But that runs exactly counter to the idea of factoring out common methods. The Method() would be identical in each case except for its return type.

like image 541
Ross Patterson Avatar asked Oct 17 '11 19:10

Ross Patterson


People also ask

Can base class access methods of derived class?

base (C# Reference)The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.

Can a class be both a base class and a derived class?

A derived class can have only one direct base class.

Can we assign base class instance to derived class?

No, that's not possible since assigning it to a derived class reference would be like saying "Base class is a fully capable substitute for derived class, it can do everything the derived class can do", which is not true since derived classes in general offer more functionality than their base class (at least, that's ...

What can be inherited by a derived class from a base class?

Which among the following is inherited by a derived class from base class? Explanation: The class inheriting another class, inherits all the data members and member functions that are not private. This is done to ensure the security features with maximum flexibility. 3.


1 Answers

I believe you can use the dynamic keyword on C# 4.0:

public abstract class Base { 
    public dynamic Method() { return this; }
}

public class Derived1: Base { ... }

public class Derived2: Base { ... }

public class Main {
    public static int Main() {
        Derived1 d1 = new Derived1();
        Derived1 x = d1.Method();
        Console.WriteLine(x.GetType()); // outputs Derived1
        Derived2 d2 = new Derived2();
        Derived2 y = d2.Method();
        Console.WriteLine(y.GetType()); // outputs Derived2
    }
}
like image 193
smoak Avatar answered Oct 07 '22 04:10

smoak