Why overload true and false instead of defining bool operator?

前端 未结 4 1799
孤城傲影
孤城傲影 2021-02-01 06:31

I\'ve been reading about overloading true and false in C#, and I think I understand the basic difference between this and defining a bool operator. The example I see around is

4条回答
  •  猫巷女王i
    2021-02-01 07:16

    I had no idea these operators existed. That means you can implement the self-negation paradox:

    public class ThisClassIsFalse
    {
        public static bool operator true(ThisClassIsFalse statement)
        {
            return statement ? false : true;
        }
    
        public static bool operator false(ThisClassIsFalse statement)
        {
            return statement ? true : false;
        }
    }
    

    So now we know the true solution to this classic paradox... StackOverflowException.

提交回复
热议问题