Introduction to USER defined data type conversions for c

  • 2020-06-03 08:06:50
  • OfStack

c# allows users to perform two defined data type conversions, explicit and implicit, which require explicit tag conversions in code by writing the target data type in parentheses.

For predefined data types, when data type conversion may fail or lose some data, explicit conversion is required,

1 When converting int values to short, as short may not be large enough to contain the converted values.

Convert signed data to unsigned data. If the signed variable contains 1 negative value, the result is incorrect.

When converting a floating-point number to an integer data type, the decimal portion of the number is lost.

You should explicitly type the data in your code, telling it that you know there is a risk of losing data, so write your code to take this possibility into account.

c# allows you to define your own data types, which means you need some tools to support data conversion between your own data types. The method is to define the cast as a member operator of the relevant class, and the cast must be declared implicitly or explicitly to show how it is used.

Note: If the source data value causes the data conversion to fail, or an exception may be thrown, you should define the data type conversion as explicit.

The syntax for defining data type conversions is similar to operator overloading.

For example: Implicit type conversion:


public static implicit operator float(Current current)
{
}

Like operator overloading, data type conversions must be declared as public and static.

Note:

When a data type conversion is declared implicit, the compiler can either explicitly or implicitly call a data type conversion.

When a data type conversion is declared explicitly, the compiler can only explicitly call a type conversion.

Here's a small example:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace  Type conversion 
{
struct Current
{
public uint Dollars;
public ushort Cents;
public Current(uint dollars, ushort cents)
{
this.Dollars = dollars;
this.Cents = cents;
}
public override string ToString()
{
return string.Format("{0}.{1,-2:00}",Dollars,Cents);
}
public static implicit operator float(Current value)
{
return value.Dollars+(value.Cents/100.0f);
}
public static explicit operator Current(float f)
{
uint dollars = (uint)f;
ushort cents = (ushort)((f - dollars) * 100);
return new Current(dollars,cents);
}
}
class Program
{
static void Main(string[] args)
{
try
{
Current balance = new Current(50, 35);
Console.WriteLine(balance);
Console.WriteLine("balance using tostring(): "+balance.ToString());
float balance2 = balance;
Console.WriteLine("After converting to float,= " + balance2);
balance = (Current)balance2;
Console.WriteLine("After converting to Current,= " + balance);
float t = 45.63f;
Current c = (Current)t;
Console.WriteLine(c.ToString());
checked
{
balance = (Current)(-50.5);
Console.WriteLine("Result is:" + balance.ToString());
}
}
catch (System.Exception ex)
{
Console.WriteLine("Exception occurred:" + ex.Message);
}
Console.ReadKey();
}
}
}

Two questions will be asked:

1 Conversion from float to Current got an error result of 50.34, not 50.35. A truncation problem occurs.

A: If the float value is converted to the uint value, the computer truncates the extra number rather than rounding it up. Data in computers is stored in base 2, not base 10, and decimal 0.35 cannot be stored in base 2. Because 1 part is omitted, the actual converted data should be less than 0.35, that is, the value that can be stored in base 2 form, and then multiplied by 100, to get the number less than 35 34. Sometimes this stage is very dangerous, avoid this wrong way to make sure to perform intelligent rounding during the conversion process.

Microsoft wrote a class System.Covert to accomplish this task. System.Covert contains a number of static methods to perform various number conversions, and we are going to use Convert.ToUInt16 (). Note that using the System.Covert method incurs an additional performance penalty, so use it only when needed.

Note: the ES61en. Covert methods also perform their own overflow checks, so


Convert.ToUInt16((f - dollars) * 100);

You can leave it out of checked.

2 No exception occurred while attempting to convert values out of range. The main reason: The overflow is not in the Main routine at all -- it is in the code for the transformation operator, which is called in the Main() method, which is not marked checked. Its solution:


 public static explicit operator Current(float f)
{
checked
{
uint dollars = (uint)f;
ushort cents = Convert.ToUInt16((f - dollars) * 100);
return new Current(dollars, cents);
}
}<SPAN style="FONT-FAMILY: Arial, Verdana, sans-serif">
</SPAN>


Related articles: