C Sharp > interface struct и пример упаковки
08.02.2018 16:33:32
Наиболее часто встречающиеся слова в статье:
Статья:
public interface IMath {
void AddOne();
}
struct Star : IMath {
public int num;
public void AddOne() { ++num; }
}
class Program {
static void Main(string[] args)
Star star = new Star(); // num=0
star.AddOne(); // num=l
Console.WriteLine(star.num);
{
IMath math=star;
math.AddOne();
Console. Write Line (star. num); //num=l
}
}
Упаковка происходит, тк Star это struct, который в стеке , math=star - это ссылка на объект в данном
случае он будет равен 2, star это переменная в стеке и её в конце печатаем и она равна 1.
В случае замены struct Star на class Star будет напечатана 2.