0

Have the following code which works fine.

MyType convertedItem = (MyType)item;

However I get a compiler error from

var convertedItem = item as MyType;

Cannot convert type 'OtherType' to 'MyType' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion.

Can anyone explain why \ when this occurs. An explicit cast works fine but AS wont even compile.

** How do I get 'AS' functionality in this situation. Namely I need to do a trycast and would prefer not to invoke the exceptionhandler to accomplish it. **

David
  • 1,131
  • 9
  • 22
  • 2
    Possible duplicate of [Direct casting vs 'as' operator?](https://stackoverflow.com/questions/132445/direct-casting-vs-as-operator) – nilsK Jun 04 '19 at 11:48
  • 2
    Is your type a struct? – Haytam Jun 04 '19 at 11:52
  • 2
    @nilsk I know HOW to cast what I dont and your "Duplicate" doesnt explain is when AS Wont work even though an explicit cast does.. – David Jun 04 '19 at 11:53
  • 2
    `as` does not work for `struct`. – MakePeaceGreatAgain Jun 04 '19 at 11:53
  • 1
    Did you search for other answers before posting this ? I found something similar here : [link](https://stackoverflow.com/questions/30888325/cannot-convert-type-via-a-reference-conversion-boxing-conversion-unboxing-conv) – NewBie1234 Jun 04 '19 at 11:55
  • 1
    Please post `MyType` and `item`. Without those this question is impossible to answer. – Reinstate Monica Cellio Jun 04 '19 at 11:56
  • Thanks Haytam and Him that was it. If one of you wants to post that Ill accept it as an Answer. – David Jun 04 '19 at 11:57
  • Possible duplicate of [What is the difference between casting and using "as" in C#?](https://stackoverflow.com/questions/702234/what-is-the-difference-between-casting-and-using-as-in-c) – Ian Jun 04 '19 at 12:06

2 Answers2

2

as doesn't work with anything that is a struct. Logically we can understand this because a struct is non nullable by default. The suggestions of casting to object work by cheating and boxing the struct

Jlalonde
  • 483
  • 4
  • 13
  • as object as mytype always returns null so though it will compile the result isn't of use. – David Jun 04 '19 at 12:04
  • Sure, just an extra tidbit why it's silly. The best case if you're unsure if the cast is valid is wrapping in a try catch, but if you fail to convert it's up to you to know what to do. – Jlalonde Jun 04 '19 at 12:06
  • didnt want the overhead of the exception handler plus what I actually need is IEnumerable.OfType(MyType) so as always its a lot messier than the simple example but the fact that its a struct is the root of the issue. – David Jun 04 '19 at 12:08
  • In fairness, behind the scenes there has to be some additional overhead. I think we forgot the language keywords always play by similar rules to us as users. So I don't think it's a big stretch to guess that `as` is likely an explicit cast in a try catch. I'd have to peek at corefx to be sure – Jlalonde Jun 04 '19 at 12:11
-1

For example, the following types would give CS0039:

class MyType
{
}

class MyOtherType
{
}

MyOtherType item = new MyOtherType();
var convertedItem = item as MyType;

In the above example, the compiler has determined that given the types participating in the cast, it's impossible to perform the requested conversion.

Here providing conversion operators would solve the problem.

EDIT: working around this error with casting to Object is not recommended, as it defeats the purpose of the type system

felix-b
  • 8,178
  • 1
  • 26
  • 36
  • Converting to object is kind of cheating. As doesn't allow us to convert objects outside if it's inheritance tree but erasing it to object removes that constraint because everything inherits from object. It's not that this is wrong, I just think most of the time this breaks some of the rules and guard rails of the language. – Jlalonde Jun 04 '19 at 12:14
  • Thank you for receiving my feedback so well. It's not always common on SO and I appreciate it. – Jlalonde Jun 04 '19 at 12:33