Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Eric Lippert mean by "you need to know what the base class is to determine what the base class is"?

I just read this interesting article by Eric Lippert, Top 10 Worst C# Features. Near the end he states:

The rules for resolving names after the aforementioned colon are not well founded; you can end up in situations where you need to know what the base class is in order to determine what the base class is.

By colon he is referring to the inheritance operator (e.g. Dog : Animal).

What situation is Eric referring to? Can anyone provide a code sample?

like image 924
Ian Newson Avatar asked Aug 19 '15 14:08

Ian Newson


1 Answers

This can happen in convoluted scenarios with generics, inheritance, and nested classes:

class Base<T> {     public class Inner {} }  class Derived : Base<Derived.Inner2> {     public class Inner2 : Inner {} } 

Result

  • To determine Derived's base class, we need to bind Derived.Inner2.
  • To bind Derived.Inner2, we need to resolve the Inner symbol.
  • The Inner symbol is inherited from its containing scope's base class, so we need to determine Derived's base class again.
like image 66
SLaks Avatar answered Sep 28 '22 06:09

SLaks