SoFunction
Updated on 2025-03-07

Detailed explanation of C# user-defined type conversion

C# user-defined type conversion

• Used for custom classes and structures to perform implicit conversion and display conversion. For example: converting a custom class type into integer, floating point, etc., and vice versa.

C# provides implicit and explicit conversions

•Implicit conversion: The compiler automatically performs the conversion
•Explanatory conversion: The compiler only performs conversion when using the explicit conversion operator
The syntax for declaring implicit conversions is as follows. Note: All user-defined conversions must use public and static modifiers.

Copy the codeThe code is as follows:

public static implicit operator TargetType(SourceType Identifier)
{
       ...
      return ObjectOfTargetType;
}

TargetType: Target type

Parameter Identitfier: Source data

For example, the following code makes an int literal implicitly convert to a LimitedInt object, and in turn, LimitedInt can also be implicitly converted to an int

Copy the codeThe code is as follows:

class LimitedInt
    {
        const int MaxValue = 100;
        const int MinValue = 0;

        private int _theValue = 0;
//Properties
        public int TheValue
        {
            get { return _theValue; }
            set
            {
                if (value < MinValue)
                {
                    _theValue = 0;
                }
                else
                {
                    _theValue = value > MaxValue ? MaxValue : value;
                }
            }
        }

//Implicit conversion: Convert the LimitedInt object to an integer
        public static implicit operator int(LimitedInt li)
        {
            return ;
        }

//Implicit conversion: Convert an integer to a LimitedInt object
        public static implicit operator LimitedInt(int x)
        {
            LimitedInt li = new LimitedInt();
            = x;
            return li;
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
LimitedInt li = 500; //Convert 500 to LimitedInt

int value = li; //Convert LimitedInt to int

            ("li:{0},value:{1}", , value);

            ();
        }


The output result of changing the code: li:100, value:100

Show conversion and cast operators

All I mentioned above are implicit conversions. If you change the operator implcit to explicit, you will have to display the conversion operator when implementing the conversion.

The code snippet is as follows:

Copy the codeThe code is as follows:

//Show conversion: Convert LimitedInt object to integer
        public static explicit operator int(LimitedInt li)
        {
            return ;
        }

//Display conversion: Convert integers to LimitedInt object
        public static explicit operator LimitedInt(int x)
        {
            LimitedInt li = new LimitedInt();
            = x;
            return li;
        }

        static void Main(string[] args)
        {
LimitedInt li = (LimitedInt)500; //Captively convert 500 to LimitedInt

int value = (int)li; //Capt the LimitedInt to int

            ("li:{0},value:{1}", , value);

            ();
        }