Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
School/C# - Beginner (Denis)/Lesson 36/lesson_36.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
79 lines (67 sloc)
1.97 KB
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
int x = 50; | |
byte y = (byte)x; // явное преобразование от int к byte | |
int z = y; // неявное преобразование от byte к int | |
public static implicit|explicit operator Тип_в_который_надо_преобразовать(исходный_тип param) | |
{ | |
// логика преобразования | |
} | |
class Counter | |
{ | |
public int Seconds { get; set; } | |
public static implicit operator Counter(int x) | |
{ | |
return new Counter { Seconds = x }; | |
} | |
public static explicit operator int(Counter counter) | |
{ | |
return counter.Seconds; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
Counter counter1 = new Counter { Seconds = 23 }; | |
int x = (int)counter1; | |
Console.WriteLine(x); // 23 | |
Counter counter2 = x; | |
Console.WriteLine(counter2.Seconds); // 23 | |
} | |
class Timer | |
{ | |
public int Hours { get; set; } | |
public int Minutes { get; set; } | |
public int Seconds { get; set; } | |
} | |
class Counter | |
{ | |
public int Seconds { get; set; } | |
public static implicit operator Counter(int x) | |
{ | |
return new Counter { Seconds = x }; | |
} | |
public static explicit operator int(Counter counter) | |
{ | |
return counter.Seconds; | |
} | |
public static explicit operator Counter(Timer timer) | |
{ | |
int h = timer.Hours * 3600; | |
int m = timer.Minutes * 60; | |
return new Counter { Seconds = h + m + timer.Seconds }; | |
} | |
public static implicit operator Timer(Counter counter) | |
{ | |
int h = counter.Seconds / 3600; | |
int m = (counter.Seconds % 3600) / 60; | |
int s = counter.Seconds % 60; | |
return new Timer { Hours = h, Minutes = m, Seconds = s }; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
Counter counter1 = new Counter { Seconds = 115 }; | |
Timer timer = counter1; | |
Console.WriteLine($"{timer.Hours}:{timer.Minutes}:{timer.Seconds}"); // 0:1:55 | |
Counter counter2 = (Counter)timer; | |
Console.WriteLine(counter2.Seconds); //115 | |
Console.ReadKey(); | |
} |