Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the "as" operator not use an implicit conversion operator in C#?

I have defined implicit string conversion from/to a certain type in C# (dummy code):

public class MyType
{
    public string Value { get; set; }

    public static implicit operator MyType(string fromString)
    {
        return new MyType { Value = fromString };
    }

    public static implicit operator string(MyType myType)
    {
        return myType.Value;
    }
}

Somewhere in external library code, an instance of MyType is passed to a method, as an object parameter. Part of that method looks like something along these lines:

private void Foo(object value)
{
    // ... code omitted
    var bar = value as string // note that value is an instance of MyType at runtime
    if(bar != null) // false, cast fails
    {
       // ... code omitted
    }
}

Why does the cast not use the implicit converter? I thought the whole point of these was to make casting and transparent usage possible?

Would this work if MyType had an explicit converter instead? If so, (how) can I have both?

By the way, the cast definitely works if the type is known at compile-time. Is this because operators are static? Is there something like non-static conversion operators?

P.S. I'm actually most interested in the differences between compile-time behaviour and runtime behaviour, so I've a follow-up question: Why are implicit type conversion operators not dynamically usable at runtime in C#?

like image 545
MarioDS Avatar asked Feb 19 '16 14:02

MarioDS


People also ask

Does C allow implicit conversion?

Implicit type conversion in C language is the conversion of one data type into another datatype by the compiler during the execution of the program. It is also called automatic type conversion.

Is implicit type conversion bad?

Implicit conversions allow the compiler to treat values of a type as values of another type. There's at least one set of scenarios in which this is unambiguously bad: non-total conversions. That is, converting an A to a B when there exists A s for which this conversion is impossible.

Which operator can be used to do type conversions C#?

In C#, we can use the Convert class to perform type conversion. The Convert class provides various methods to convert one type to another.

When can an implicit conversion occur between classes?

Implicit Type Conversion is also known as 'automatic type conversion'. It is done by the compiler on its own, without any external trigger from the user. It generally takes place when in an expression more than one data type is present.


1 Answers

Why does the soft cast not use the implicit converter?

Well, that's the way the language is specified, basically. From the C# 5 specification section 7.10.11:

If the compile-time type of E is not dynamic, the operation E as T produces the same result as

E is T ? (T)(E) : (T)null

except that E is only evaluated once.

[...]

Note that some conversions, such as user defined conversions, are not possible with the as operator and should instead be performed using cast expressions.

like image 161
Jon Skeet Avatar answered Sep 27 '22 18:09

Jon Skeet