c# add castable type code example
Example 1: c# type conversion
float varFloat = 12.7f;
double varDouble = varFloat;
double varDouble = 2.3;
int varInt = (int) varDouble;
int varInt = 19;
string varString = varInt.ToString();
string varString = "89";
int varInt = int.Parse(varString);
double varDouble = double.Parse(varString);
int varInt = Convert.ToInt32(varString);
double varDouble = Convert.ToDouble(varString);
Example 2: how to make a class implicitly convertible C#
public class ExampleClass
{
private static int storedInt = 10;
public int Integer
{
get => storedInt;
set => storedInt = value;
}
public ExampleClass(int storedNumber)
{
storedInt = storedNumber;
}
public static implicit operator ExampleClass(int value)
{
ExampleClass x = new ExampleClass(value);
x.Integer = value;
return x;
}
}