相关疑难解决方法(0)

C#postfix和前缀增量/减量重载差异

大多数消息来源说,在c#中重载++和 - 运算符导致一次性重载postfix和prefix.但看起来他们的行为仍然不同.

class Counter
{
    public Counter(int v = 0)
    {
        this.v = v;
    }
    public Counter(Counter c)
    {
        v = c.v;
    }
    public int GetValue() { return v; }
    public static Counter operator ++(Counter c)
    {
        c.v++;
        return new Counter(c);
    }
    private int v;
}


class Program
{
    public static void Main()
    {
        Counter c1 = new Counter(1);

        Counter c2 = c1++;

        Counter c3 = ++c1;

        c3++;

        System.Console.WriteLine("c1 = {0}", c1.GetValue());
        System.Console.WriteLine("c2 = {0}", c2.GetValue());
        System.Console.WriteLine("c3 = {0}", …
Run Code Online (Sandbox Code Playgroud)

c# operator-overloading

2
推荐指数
1
解决办法
2113
查看次数

标签 统计

c# ×1

operator-overloading ×1