简体   繁体   English

重载真假运算符有什么作用?

[英]What does overloading true and false operators do?

I am only a beginner to C#, but am trying to learn everything. 我只是C#的初学者,但我正在努力学习一切。 However, I am stuck on the overloading true and false; 但是,我坚持重载真假; what does this mean? 这是什么意思? Please (try to) give replies as basic as possible (so that even a '13' yr old understands the logic). 请(尽量)尽可能地给出答复(这样即使是'13'岁的人也能理解逻辑)。 (Please make to sure make it as understandable as possible, thank you). (请尽量使其尽可能理解,谢谢)。 If you can, please explain what the outcome would be if the coord had changed to something else (such as: (3,5)). 如果可以的话,请解释如果coord改变为其他东西会产生什么结果(例如:(3,5))。

I have pre-made code right here: (Can you lease explain the different outputs on changing the coordinates) Info: - Program is a console application - C# I have made a class called Coord (standing for co-ordinates). 我在这里预先制作了代码:(你能解释一下改变坐标的不同输出吗)信息: - 程序是一个控制台应用程序 - C#我创建了一个名为Coord的类(代表坐标)。 Inside Class: 内部课程:

class Coord
{
    private int _x, _y;

    public Coord(int x, int y)
    {
        _x = x; 
        _y = y;
    }

    public int x
    {
        get 
        { 
            return _x; 
        }
        set 
        { 
            _x = value; 
        }
    }

    public int y
    {
        get 
        { 
            return _y; 
        }
        set 
        { 
            _y = value; 
        }
    }

    public static bool operator true(Coord coord1)
    {
        if (coord1.x != 0 || coord1.y != 0)
        { 
            return true; 
        }
        else
        { 
            return false; 
        }
    }

    public static bool operator false(Coord coord1)
    {
        if (coord1.x == 0 && coord1.y == 0)
        { 
            return true; 
        }
        else
        { 
            return false; 
        }
    }
}

Inside Main class Program: 内部主课程:

class Program
{
    static void Main(string[] args)
    {
        Coord coord = new Coord(0, 0);

        if (coord)
        {
            Console.WriteLine("True");
        }
        else
        {
            Console.WriteLine("False");
        }

        Console.ReadLine();
    }
}

I am only a beginner to C#, but am trying to learn everything. 我只是C#的初学者,但我正在努力学习一切。

Make sure to allocate a couple of decades minimum. 确保最少分配几十年。 I learn new things about this language quite frequently. 我经常学习这门语言的新知识。

I am stuck on the overloading true and false; 我坚持超载真假; what does this mean? 这是什么意思?

They are seldom used; 它们很少使用; they are primarily needed to make user-defined && and || 它们主要用于制作用户定义的&&|| operators be short-circuiting. 运营商是短路的。

Read my series of articles on this subject; 阅读我关于这个主题的系列文章; you will be particularly interested in part three but you should read the whole thing. 你会对第三部分特别感兴趣,但你应该阅读整篇文章。

https://ericlippert.com/2012/03/26/null-is-not-false-part-one/ https://ericlippert.com/2012/03/26/null-is-not-false-part-one/

If you can, please explain what the outcome would be if the coord had changed to something else (such as: (3,5)). 如果可以的话,请解释如果coord改变为其他东西会产生什么结果(例如:(3,5))。

If you want to know what a program does when you change something, change it and run it and soon you will know! 如果你想知道改变某些程序时程序的作用,请更改并运行它 ,很快就会知道!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM