Skip to content

Instantly share code, notes, and snippets.

@JorTurFer
Last active March 26, 2019 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JorTurFer/a69129746db10ef2007aaded5ab510f9 to your computer and use it in GitHub Desktop.
Save JorTurFer/a69129746db10ef2007aaded5ab510f9 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace Implicit_Explicit
{
class Program
{
static void CasoGrados()
{
//Después de un ciclo completo, no hay perdida de informacion
//Sentido 1
{
Celsius gradoCelsius = new Celsius(21.1f);
Fahrenheit gradoFahrenheit = gradoCelsius;
Kelvin gradoKelvin = gradoFahrenheit;
Celsius gradoCelsius2 = gradoKelvin;
Console.WriteLine($"Sentido 1: Celsius ({gradoCelsius.ToString()}) -> Fahrenheit -> Kelvin -> Celsius={gradoCelsius2.ToString()}");
}
//Sentido 2
{
Celsius gradoCelsius = new Celsius(21.1f);
Kelvin gradoKelvin = gradoCelsius;
Fahrenheit gradoFahrenheit = gradoKelvin;
Celsius gradoCelsius2 = gradoFahrenheit;
Console.WriteLine($"Sentido 2: Celsius ({gradoCelsius.ToString()}) -> Kelvin -> Fahrenheit -> Celsius={gradoCelsius2.ToString()}");
}
}
private static void CasoPersonas()
{
//Después de un ciclo completo, hay perdida de informacion
Profesor teacher = new Profesor { Name = "Jorge", Facultad = "Ingenieros", IdContrato = 10 };
Alumno student = (Alumno)teacher;
Profesor teacher2 = (Profesor)student;
Console.WriteLine(teacher.ToString());
Console.WriteLine(teacher2.ToString());
}
static void Main(string[] args)
{
Console.WriteLine("==================Conversion implícita==================");
CasoGrados();
Console.WriteLine("==================Conversion explícita==================");
CasoPersonas();
Console.Read();
}
class Grado
{
public float Grados { get; set; }
}
class Celsius : Grado
{
public Celsius(float temp)
{
Grados = temp;
}
public static implicit operator Fahrenheit(Celsius c)
{
return new Fahrenheit((9.0f / 5.0f) * c.Grados + 32);
}
public static implicit operator Kelvin(Celsius c)
{
return new Kelvin(c.Grados + 273.15f);
}
public override string ToString()
{
return Grados.ToString("0.00");
}
}
class Kelvin: Grado
{
public Kelvin(float temp)
{
Grados = temp;
}
public static implicit operator Fahrenheit(Kelvin k)
{
return new Fahrenheit((k.Grados - 273.15f) * 9 / 5 + 32);
}
public static implicit operator Celsius(Kelvin k)
{
return new Celsius(k.Grados - 273.15f);
}
}
class Fahrenheit: Grado
{
public Fahrenheit(float temp)
{
Grados = temp;
}
public static implicit operator Celsius(Fahrenheit fahr)
{
return new Celsius((5.0f / 9.0f) * (fahr.Grados - 32));
}
public static implicit operator Kelvin(Fahrenheit fahr)
{
return new Kelvin((fahr.Grados - 32) * 5 / 9 + 273.15f);
}
}
public class Persona
{
public string Name { get; set; }
}
public class Alumno : Persona
{
public string Facultad { get; set; }
public List<int> IdsCursos { get; set; }
public static explicit operator Profesor(Alumno alum)
{
return new Profesor { Name = alum.Name, Facultad = alum.Facultad, IdContrato = -1 };
}
}
public class Profesor : Persona
{
public string Facultad { get; set; }
public int IdContrato { get; set; }
public static explicit operator Alumno(Profesor prof)
{
return new Alumno { Name = prof.Name, Facultad = prof.Facultad, IdsCursos = new List<int>() };
}
public override string ToString()
{
return $"Nombre:{Name}\tFacultad:{Facultad}\tIdContrato:{IdContrato}";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment