Question: Summary
-------

Let's say I have two C# 4.0 classes, one inheriting from the other:

```
class ParentKey {}
class ChildKey : ParentKey {}

```

I want the compiler to issue an error if I try this:

```
ChildKey c = new ChildKey();
ParentKey p = c; // I want compiler error here!

```

Essentially, I want to use inheritance **for reusability purposes**, but I want to **avoid polymorphic behavior** (or more specifically, assign compatibility) that normally comes with it. Similar to C++ private inheritance.

---

Example
-------

Specifically, I'd like to avoid accidentally mixing ParentKey and ChildKey when used as keys of some container (since their implementations of GetHashCode() or Equals() might be incompatible). For example:

```
Dictionary<ParentKey, object> d = new Dictionary<ParentKey, object>();
d.Add(new ChildKey(), new object()); // I want compiler error here!

```

---

What I Tried
------------

Now, I know I can use composition to avoid the inheritance altogether, but I'd like to avoid the verbosity that comes with this solution (my ParentKey can be quite complex, and there may be many levels of inheritance hierarchy).

Another solution is to always use tailor-made IEqualityComparer, or to explicitly create new ParentKey based on the ChildKey prior passing to the container, but both of these are easy to forget, and may be comparatively hard to diagnose at run-time.

Attempting to make the conversion explicit...

```
class ChildKey : ParentKey {
    public static explicit operator ParentKey(ChildKey c) {
        // ...
    }
}

```

...yielded compiler error CS0553: *user-defined conversions to or from a base class are not allowed.*

Struct inheritance would be ideal here (so the "end" portion of ChildKey is "cut-off" when passed to something that is declared as ParentKey), but this is not supported in C# either.

Am I missing something obvious here? Any ideas? Thanks.

Answer: 
How about:

```
class BaseKey
{
    // all functionality here
}

class ParentKey : BaseKey
{}

class ChildKey : BaseKey
{}

```

?
```
public struct Exclusive<T>
{
   public Exclusive(T item)
   {
     if (c.GetType () != typeof(ParentKey))
       throw new Exception (); // I want compiler error here!
     Item = item;
   }
   public T Item{get; private set;}
   // todo: add implicit cast to T
   // todo: add forcing non-null to get_Item
}

```