BasicKnowledgeCSharp/LessonsAndTasks/Lesson 82 - boxing and unboxing. Упаковка и распаковка значимых(value)типов/Program.cs
Dvurechensky 058c8f2679 1.0
Main
2024-10-05 09:59:53 +03:00

33 lines
953 B
C#

using System;
/*
* boxing and unboxing (значимые в стеке, ссылочные в куче)(лучше избегать таких ситуаций)
*/
interface IPrintable
{
void Print();
}
struct Point : IPrintable
{
public int X { get; set; }
public int Y { get; set; }
public void Print() => Console.WriteLine($"X:{X}; Y:{Y};");
}
class Program
{
static void Print(IPrintable printable) => printable.Print();
public static void Main()
{
int a = 1;
object b = a; // boxing
int c = (int)b; // unboxing
// decimal d = (decimal)b; // InvalidCastException
var point = new Point { X = 3, Y = 3 }; // boxing (ссылку содержит на данные в куче)
Print(point);
int d = 0; d.GetType(); // boxing
Console.ReadKey();
}
}