Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are implicity/explicit conversion methods inherited in C#?

Tags:

c#

inheritance

I'm not sure what I'm doing wrong here. I have a generic class, which is basically a glorified integer, with a few methods for certain string formatting, as well as into/from string and int conversions:

public class Base
{
    protected int m_value;
    ...
    // From int
    public static implicit operator Base(int Value)
    {
        return new Base(Value);
    }
    ...
    // To string
    public static explicit operator string(Base Value)
    {
        return String.Format("${0:X6}", (int)Value);
    }
}

And it functions fine. I can successfully use implicit and explicit conversions:

Base b = 1;
Console.WriteLine((string)b);  // Outputs "$000001", as expected.

Then I derive from this class, different child classes, which turn on/off different named bits in m_value. For example:

public class Derived : Base
{

}

And then I cannot use my implicit to/from int conversions:

Derived d = 3;
// Cannot implicitly convert type 'int' to 'Derived'. An explicit conversion exists (are you missing a cast?)

Even this gives the same error:

Derived d = (int)3;

Are the implicit/explicit conversions not inherited in the derived class? This will require a lot of code copying if not.

RESPONSE Thank you so much for the quick responses! You all deserve the 'answer' checkmark, they're all very good answers. The key is to think about the types on both sides of the equal sign. Now that I think about it like that, it makes perfect sense.

I obviously only have to re-write my "to Derived" conversions. The "to Int32, String, etc" conversions still apply.

like image 405
Jonathon Reinhart Avatar asked Jun 09 '09 00:06

Jonathon Reinhart


People also ask

What is difference between implicit and explicit variable in Visual Basic?

An implicit conversion does not require any special syntax in the source code. In the following example, Visual Basic implicitly converts the value of k to a single-precision floating-point value before assigning it to q . An explicit conversion uses a type conversion keyword.

Why are implicit type conversions safe?

Implicit Conversions There is no special syntax for this type of conversion, this is the safest type of casting. No data is lost, for example, when converting from smaller to larger integral types or derived classes to base classes.

Which of the following conversions can be performed implicitly?

In C#, you can perform the following kinds of conversions: Implicit conversions: No special syntax is required because the conversion always succeeds and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.

What is the need for conversion of data type in C sharp?

Type conversion happens when we assign the value of one data type to another. If the data types are compatible, then C# does Automatic Type Conversion. If not comparable, then they need to be converted explicitly which is known as Explicit Type conversion. For example, assigning an int value to a long variable.


2 Answers

The reason

Derived d = (int)3;

does not work is because the type Derived does not exactly match the return value of the operator Base as is required to invoke this operator. Notice that you haven't provided any conversion operators that contain the code new Derived(...), so it is not surprising that you can't make new Derived instances this way.

Note, however, that the opposite conversion

Derived v = ...;
string s = (string)v;

will work fine (as if it were "inherited", although this is not really inheritance due to the static keyword).

like image 175
Jason Kresowaty Avatar answered Sep 21 '22 01:09

Jason Kresowaty


No, it will not work that way. The compiler will not implicitly downcast from a base to a derived for you. Basically, you can't do ...

D d = new B();

You will get your base class implmentations from your string cast, because it will do the implicit upcasting for you.

You could do a work around if you didn't want to copy your methods to your derived class with an extension function on integers, like (assuming your derived class is called D) ...

public static class Ext
{
    public static D IntAsD(this int val)
    {
        return new D(val);
    }
}

Then, you could do what you want with ...

D d1 = 5.IntAsD();

Granted, it's not perfect, but it might fit your needs.

like image 35
JP Alioto Avatar answered Sep 20 '22 01:09

JP Alioto