Information Technology Reference
In-Depth Information
For example, the following code reiterates and uses the two type conversion operators
defined previously. In Main , an int literal is converted into a LimitedInt object, and in the next
line, a LimitedInt object is converted into an int .
class LimitedInt
{
const int MaxValue = 100;
const int MinValue = 0;
public static implicit operator int(LimitedInt li) // Convert type
{
return li.TheValue;
}
public static implicit operator LimitedInt(int x) // Convert type
{
LimitedInt li = new LimitedInt();
li.TheValue = x;
return li;
}
private int _TheValue = 0;
public int TheValue // Property
{
get { return _TheValue; }
set
{
if (value < MinValue)
_TheValue = 0;
else
_TheValue = value > MaxValue
? MaxValue
: value;
}
}
}
class Program
{
static void Main() // Main
{
LimitedInt li = 5; // Convert 5 to LimitedInt
int Five = li; // Convert LimitedInt to int
Console.WriteLine("li: {0}, Five: {1}", li.TheValue, Five);
}
}
Search WWH ::




Custom Search