Created
June 2, 2019 12:25
-
-
Save CristianoRC/f5901f81a7cbf933409dc5700bbfa7ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace explicitCast | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var real = new Real(459); | |
var dolar = (Dolar)real; | |
var realConvertido = (Real)dolar; | |
Console.WriteLine($"{real.Valor.ToString("N2")} {real.Codigo} = {dolar.Valor.ToString("N2")} {dolar.Codigo}"); | |
Console.WriteLine($"{realConvertido.Valor.ToString("N2")} {realConvertido.Codigo} = {dolar.Valor.ToString("N2")} {dolar.Codigo}"); | |
} | |
} | |
public class Real | |
{ | |
public Real(decimal valor) | |
{ | |
Valor = valor; | |
Nome = "Real brasileiro"; | |
Codigo = "BRL"; | |
} | |
public string Nome { get; private set; } | |
public string Codigo { get; private set; } | |
public decimal Valor { get; private set; } | |
public static explicit operator Dolar(Real real) | |
{ | |
var cotacaoDolar = 0.25477m; | |
var conversao = real.Valor * cotacaoDolar; | |
return new Dolar(conversao); | |
} | |
} | |
public class Dolar | |
{ | |
public Dolar(decimal valor) | |
{ | |
Valor = valor; | |
Nome = "United States dollar"; | |
Codigo = "USD"; | |
} | |
public string Nome { get; private set; } | |
public string Codigo { get; private set; } | |
public decimal Valor { get; private set; } | |
public static explicit operator Real(Dolar dolar) | |
{ | |
var cotacaoDolar = 0.25477m; | |
var conversao = dolar.Valor / cotacaoDolar; | |
return new Real(conversao); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment