Compartir a través de


implicit (C# Reference) 

The implicit keyword is used to declare an implicit user-defined type conversion operator.

static implicit operator target_type { source_type identifier }

Parameters

  • target_type
    A reference type
  • source_type
    A reference type.
  • identifier
    Something.

Remarks

By eliminating unnecessary casts, implicit conversions can improve source code readability. However, because implicit conversions can occur without the programmer's specifying them, care must be taken to prevent unpleasant surprises. 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.

class MyType 
{
    public static implicit operator int(MyType m) 
    {
        // code to convert from MyType to int
    }
}

Implicit conversion operators can be called implicitly, without being specified by explicit casts in the source code.

MyType x;
// implicitly call MyType's MyType-to-int conversion operator
int i = x;

C# Language Specification

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

  • 6.1 Implicit conversions

See Also

Tasks

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

Reference

C# Keywords
explicit (C# Reference)
operator (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference