1

Why I can't implicitly convert Generic<T> to GenericSetted : Generic<T> ?

This driving me insane:

Declaration:

class GenericParam<T>
{ }

class WithGenericParamAndGeneric<G, T>
    where G : GenericParam<T>
{ }

Implementation:

class Generic : GenericParam<string>
{ }

class Inherited : WithGenericParamAndGeneric<Generic, string>
{ }

Trouble:

void Assign()
{
    WithGenericParamAndGeneric<GenericParam<string>, string> b = new Inherited(); //(!)
}

Error:

Cannot implicitly convert type 'Inherited' to 'WithGenericParamAndGeneric<GenericParam<string>, string>'

I do not know the reasons in which case CLR can't process this case.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • 1
    Why do u expect it to work ? – loneshark99 May 05 '17 at 06:01
  • Nice variable name. – r41n May 05 '17 at 06:04
  • @r41n i know. thanks. @loneshark99 `Generic` is the same as `GenericParam` –  May 05 '17 at 06:06
  • Your question is the same as every other "why don't generic type parameters follow the same inheritance rules as regular types?" question on Stack Overflow. See marked duplicate for one of many such discussions. In your example, if the compiler let you do what you want, then you'd have variable `b` you could use to put some `GenericParam` into your `Inherited` object that _wasn't_ actually a `Generic` object. – Peter Duniho May 05 '17 at 06:09
  • @PeterDuniho it's **not** the same as you linked. Just post answer how i can resolve this. –  May 05 '17 at 06:09
  • 1
    Great job posting a [MCVE](https://stackoverflow.com/help/mcve)! – C. McCoy IV May 05 '17 at 06:10
  • @anete.anetes: it is _exactly_ the same as the question you are asking. And you can't resolve it, because you're trying to write code that is broken. The compiler is helping you avoid hurting yourself. – Peter Duniho May 05 '17 at 06:11
  • @PeterDuniho in your link, i can cast List to List by `implicitly` operator. **in my case** i can't do it because **user-defined conversions to or from a base class are not allowed**. Or may be you post answer which will explain **why** `IL` and `CLR` can't do this cast? –  May 05 '17 at 06:14
  • 1
    _"i can cast List to List"_ -- no you can't. The two types are not compatible. And the reason they are incompatible is the exact same reason the types in your question are incompatible. As a programmer, you need to develop the ability to be able to generalize and abstract from a known fact to another scenario that is the same. I suggest you use this opportunity to exercise those skills. – Peter Duniho May 05 '17 at 06:18
  • _"i can't do it because user-defined conversions to or from a base class are not allowed"_ -- no, that's not why you can't do it. You can't do it because to allow you to do it would create a not-type-safe situation. _"why IL and CLR can't do this cast?"_ -- the error you are getting is from the compiler, not the runtime. The runtime also won't let you do the cast, but your code fails to compile because the compiler won't let you, not because the runtime won't let you. – Peter Duniho May 05 '17 at 06:19
  • @PeterDuniho i see your problem. You are just not trying to help, you try to explain your point of view. Can you answer **how** i can **avoid** it? Please **try to help me**, **not to explain what I'm wrong about** –  May 05 '17 at 06:22
  • 1
    _"how i can avoid it?"_ -- you can't avoid it. What you are trying to do is **dangerous**. Your first step is to understand that, and to understand why. Once you know that, then you can review the literally hundreds of similar questions already on Stack Overflow discussing possible work-arounds. Please do some research, and post a question only when you have something _new_ to ask. – Peter Duniho May 05 '17 at 06:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143461/discussion-between-anete-anetes-and-peter-duniho). –  May 05 '17 at 06:45
  • 1
    _"because of you i can't add answer"_ -- we don't need you to add an answer. Whatever your work-around, it's already been written numerous times on this site already. So, the fact that you can't is not a problem. – Peter Duniho May 05 '17 at 06:48

0 Answers0