This project is read-only.

inherited base on object creation

Topics: C# Language Design
May 4, 2014 at 1:38 PM
Edited May 4, 2014 at 1:39 PM
Sorry, may the C# possibility for insert heritable class as a new base for object?

For example:
class a{ };
class b : a{ }
class c : a{ }
c x = new c():b();
and x.base is b
May 4, 2014 at 5:18 PM
Something with a b as a base cannot be a c. Are you seeking multiple inheritance?

I could see some usefulness for a limited form of multiple inheritance where the splitting-and-rejoining branches of the tree would be required to be very similar. For example, being able to specify that a family of classes should be identical except for the selection of interfaces which they implement, and being able to derive from such a family to produce another without having to derive from each class individually. Such an arrangement would facilitate the creation of parallel mutable and immutable types (e.g. have one mass of code for ReadableFoo, MutableFoo and ImmutableFoo, and then have another mass of code for EnhancedFoobase, MutableEnhancedFoo, and ImmutableEnhancedFoo. Any such thing woudl certainly have to be done at the class level, rather than at the time of instantiation.
May 5, 2014 at 3:20 PM
This is not multiple inheritance: x have one base class.
For example: we have closed external library. But if we add middle chield, we add new possibility.
Except: class is ceiled.

Thanks for understanding of a problem.
May 5, 2014 at 5:09 PM
The type of an object's base is a property of the object's type. Thus, if any instances of type c have a as their base type, all must do so. It might be useful to have a means of saying "Define a class c2 which is just like c, but inheriting from b rather than a, but c2 would be a different type from c. Alternatively, if code has suitable permissions, it might be possible to patch the class loader before c is loaded, and have the class loader modify the type to inherit from b, but I'm not sure the CLR would expose any mechanism to do that.

In any case, there's certainly no mechanism for changing base on a per-instance basis.
May 5, 2014 at 6:54 PM
Edited May 5, 2014 at 6:55 PM
supercat wrote:
The type of an object's base is a property of the object's type.
from base documentation:
A base class access is permitted only in a constructor, an instance method, or an instance property accessor.
It is an error to use the base keyword from within a static method.
May be, base is instance property?

Sorry again.
May 5, 2014 at 7:02 PM
If Foo derives from Bar, then within Foo, the keyword base essentially means (Bar)this, but it bypasses virtual method dispatch and is usable in some contexts where the latter syntax would not be accepted. It basically tells the compiler "regard a reference to me as a reference to an object of some type known only as a derivative of Bar, and use whatever member would be used if I was an instance of Bar".
May 5, 2014 at 7:15 PM
Thanks. (but (a) this is authorized in dinamic chain for c and for b )