I have a simple scenario that may or may not be possible. I have a class that contains an integer, for this purpose I'll make it as simple as possible:
public class Number
{
public int Value {get; set;}
public string Name {get; set;}
}
public static void Print(int print)
{
Console.WriteLine(print);
}
public static string Test()
{
Number num = new Number (9, "Nine");
Print(num); //num "overloads" by passing its integer Value to Print.
}
// Result
// 9
How do I make the Test()
function work as I have coded it? Is this even possible? I think this can be done with the explicit/implicit operator but I can't figure it out.
Try something like this
public static implicit operator int(Number num)
{
return num.Value;
}
class Number
{
public static implicit operator int(Number n)
{
return n.Value;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With