14

I have been given a sample statement:

MyClass myclass = 3;

How is it possible to make this a valid statement? What code do I need to include in MyClass to support the implicit conversion from an int?

7
  • This actually is a good question, needs edit though Aug 11, 2010 at 11:44
  • How is this related to implicit typing ? you probably mean implicit conversion Aug 11, 2010 at 11:45
  • yes, implicit conversion is better to say here
    – Mike
    Aug 11, 2010 at 11:46
  • 1
    If that were an interview question I'd like to know what the purpose is. Using implicit conversions in such a way is - with few exceptions - a grave abuse of the concept. Sure, if the class were an Integer class or something, the implicit conversion would be nice to have. But I'd flag the sample statement in every code review as soon as I see it.
    – Thorsten79
    Aug 11, 2010 at 11:54
  • @Thorsten79 I don't think there's any harm in testing a theoretical concept. The question is "how to make it a valid statement" - nothing more. It's not saying this is a real world example.
    – MrWhite
    Aug 11, 2010 at 12:07

5 Answers 5

19

You need an implicit conversion operator:

public class MyClass
{
    private readonly int value;
    public MyClass(int value)
    {
        this.value = value;
    }

    public static implicit operator MyClass(int value)
    {
        return new MyClass(value);
    }
}

Personally I'm not a huge fan of implicit conversions most of the time. Occasionally they're useful, but think carefully before putting them in your code. They can be pretty confusing when you're reading code.

On the other hand, when used thoughtfully, they can be amazingly handy - I'm thinking particularly of the conversions from string to XName and XNamespace in LINQ to XML.

3
  • They're confusing for classes for suresies. When used consistently with immutable structs, though, it is in fact not confusing, but very powerful. FeetAndInches length = "10\'-3/16\"" is pretty obvious and leaves no room for question as to how it functions. Aug 11, 2010 at 11:54
  • 1
    @Michael: Actually it does. What does it do if the input string is null? Or empty? Or contains garbage? In each case, does it throw or does it return a nonsensical value? These answers are non-obvious to anyone reading the code, so you probably should not use an implicit operator there. (Of course, an implicit operator going the opposite way would be different, but then there’s already ToString() for that.)
    – Timwi
    Sep 19, 2010 at 1:35
  • @Timwi I think there's room for consistency by convention. I like to make sure that all of my structs have an uninitialized state var feetAndInches = FeetAndInches.Empty. This is also the default value. You can't assign null to a struct, so that's not an issue. If you assign a null string variable or otherwise unparseable string, then throw a parse exception. If you do this consistently, there shouldn't be confusion, and your code becomes much more readable. Sep 20, 2010 at 12:01
6

Here's how:

public class MyClass
{
    public static implicit operator MyClass(int i)
    {
        return new MyClass(i);
    }
}

This uses C# feature called implicit conversion operator.

0

It's possible if MyClass has an implicit conversion from int.

Using Conversion Operators

0
0

Just implement an implicit conversion operator.

-1

You need to overload the implicit constructor operator:

public static implicit operator MyClass (int rhs)
{ 
    MyClass c = new MyClass (rhs);
    return c;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.