-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
83 lines (55 loc) · 2.12 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// User-defined explicit and implicit conversion operators
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators
// Type-testing operators and cast expressions - is, as, typeof and casts
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast#cast-expression
Product product01 = new Product() { Id = 1, Name = "Same product", Price = 12};
ProductDto productDto01 = product01;
var productDto02 = (ProductDto)product01;
Console.WriteLine(productDto01);
Console.WriteLine(productDto02);
Celsius cel = new Celsius(45);
Fahrenheit far = cel;
float temperature = cel;
Console.WriteLine($"Celsius: {temperature}");
float Fahrenheit = far;
Console.WriteLine($"Fahrenheit: {Fahrenheit}");
Console.WriteLine(far);
Celsius cel2 = far;
Console.WriteLine(cel2);
class Temperature
{
public float Degrees { get; set; }
public static implicit operator float(Temperature temperature) => temperature.Degrees;
}
class Celsius : Temperature
{
public Celsius(float degrees) => Degrees = degrees;
public static implicit operator Fahrenheit(Celsius c) => new Fahrenheit((9.0f / 5.0f) * c.Degrees + 32);
public override string ToString() => $"{Degrees} °C";
}
class Fahrenheit : Temperature
{
public Fahrenheit(float degrees) => Degrees = degrees;
public static implicit operator Celsius(Fahrenheit fahrenheit) => new Celsius((5.0f / 9.0f) * (fahrenheit.Degrees - 32));
public override string ToString() => $"{Degrees} °F";
}
class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public static implicit operator ProductDto(Product product) => new ProductDto
{
Id = product.Id,
Name = product.Name,
Price = product.Price,
};
}
class ProductDto
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public override string ToString() => $"Id: {Id} Name: {Name} Price: {Price}";
}