implicit (C# Reference)

The implicit keyword is used to declare an implicit user-defined type conversion operator. Use it to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data.


namespace implicitExample
    class Digit
        public Digit(double d) { val = d; }
        public double val;
        // ...other members 

        // User-defined conversion from Digit to double 
        public static implicit operator double(Digit d)
            return d.val;
        //  User-defined conversion from double to Digit 
        public static implicit operator Digit(double d)
            return new Digit(d);
    class Program
        static void Main(string[] args)
            Digit dig = new Digit(7);
            //This call invokes the implicit "double" operator 
            double num = dig;
            //This call invokes the implicit "Digit" operator
            Digit dig2 = 12;
            Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);

By eliminating unnecessary casts, implicit conversions can improve source code readability. However, because implicit conversions do not require programmers to explicitly cast from one type to the other, care must be taken to prevent unexpected results. In general, implicit conversion operators should never throw exceptions and never lose information so that they can be used safely without the programmer's awareness. If a conversion operator cannot meet those criteria, it should be marked explicit. For more information, see Using Conversion Operators.

C# Language Specification

For more information, see the following section in the C# Language Specification:

  • 6.1 Implicit conversions

  • 10.9.3 Conversion Operators

See Also


How to: Implement User-Defined Conversions Between Structs (C# Programming Guide)


C# Programming Guide


C# Keywords

explicit (C# Reference)

operator (C# Reference)

Other Resources

C# Reference