Переопределение инкремента - C#
Формулировка задачи:
Здравствуйте!
Подскажите почему эти два кода работают одинаково
1)
2)
Разница только в операторе инкремента
Предполагал что первый пример будет работать так же как и второй, если написать с=с++
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace first
{
class Cord
{
private int x;
public Cord(int _x = 0)
{
x = _x;
}
public void Show()
{
Console.WriteLine(x);
}
public static bool operator true(Cord op1)
{
if (op1.x != 0)
return true;
return false;
}
public static bool operator false(Cord op1)
{
if (op1.x == 0)
return true;
return false;
}
public static Cord operator ++(Cord op1)
{
Cord res = new Cord(op1.x);
res.x++;
return res;
}
}
class Program
{
static void Main(string[] args)
{
Cord c = new first.Cord(-3);
while(c)
{
c.Show();
c++;
}
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace first
{
class Cord
{
private int x;
public Cord(int _x = 0)
{
x = _x;
}
public void Show()
{
Console.WriteLine(x);
}
public static bool operator true(Cord op1)
{
if (op1.x != 0)
return true;
return false;
}
public static bool operator false(Cord op1)
{
if (op1.x == 0)
return true;
return false;
}
public static Cord operator ++(Cord op1)
{
op1.x++;
return op1;
}
}
class Program
{
static void Main(string[] args)
{
Cord c = new first.Cord(-3);
while(c)
{
c.Show();
c++;
}
}
}
}Решение задачи: «Переопределение инкремента»
textual
Листинг программы
Cord a = new Cord(-3); Cord b = a++; // Здесь я ожидаю, что a == -2 и b == -3 a.Show(); // -2, OK b.Show(); // -2, Dafuq?!