Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6066943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:33:17+00:00 2026-05-23T09:33:17+00:00

I’ve been searching for actual working code where an overloaded false operator actually gets

  • 0

I’ve been searching for actual working code where an overloaded false operator actually gets executed.

This question (What’s the false operator in C# good for?) is somewhat the same, but the accepted answer links to an url which is returning a 404 error. I’ve also looked at How does operator overloading of true and false work? and some other questions.

What I’ve found in almost all answers is that false only gets executed when you use a short circuited and like x && y. This is evaluated as T.false(x) ? x : T.&(x, y).

Ok, so I have the following code. The struct contains an int and considers itself true if the int is greater than zero.:

public struct MyStruct {
    private int _i;

    public MyStruct(int i) {
        _i = i;
    }

    public static bool operator true(MyStruct ms) {
        return ms._i > 0;
    }

    public static bool operator false(MyStruct ms) {
        return ms._i <= 0;
    }

    public override string ToString() {
        return this._i.ToString();
    }
}

Now I would hope the following program will execute and use the overloaded false operator.

class Program {
    private static void Main() {
        MyStruct b1 = new MyStruct(1); // to be considered true
        MyStruct b2 = new MyStruct(-1); // to be considered false

        Console.WriteLine(b1 && b2);
        Console.WriteLine(b2 && b1);
    }
}

However, it does not even compile. It says it cannot apply operator ‘&&’ to operands of type ‘MyStruct’ and ‘MyStruct’.

I know I can implement an overload of the & operator. So let’s do that. The & must return a MyStruct, so I can not make it return a bool.

public static MyStruct operator &(MyStruct lhs, MyStruct rhs) {
    return new MyStruct(lhs._i & rhs._i);
}

Now the code does compile. Its output is 1 and -1. So the result of b1 && b2 is not the same as that of b2 && b1.

If I debug the code, I see that b1 && b2 first executes the false operator on b1, which returns false. Then it performs the & operator on b1 and b2, which performs a bitwise and on 1 and -1, resulting in 1. So it indeed is first checking if b1 is false.

The second expression, b2 && b1 first executes the false operator on b2, which returns true. Combined with the fact I’m using short circuiting, it doesn’t do anything with b1 and just prints out the value of b2.

So yes, the false operator is executed when you use short circuiting. However, it does not execute the true or false operator on the second argument, but instead executes the overloaded & operator on the operands.

When can this ever be useful? Or how can I make my type so that it can check if the two variables both are true?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:33:18+00:00Added an answer on May 23, 2026 at 9:33 am

    EDIT-

    Reading the linked article I was able to get the following output which uses both the true and false operators:

    op false on 1
    op & on 1 -1
    op true on 1
    op true on -1
    FALSE
    op false on -1
    op true on -1
    FALSE
    op true on 1
    op true on 1
    TRUE
    op true on -1
    op & on -1 1
    op true on -1
    op true on 1
    TRUE
    

    With the code:

    class Program
    {
        static void Main(string[] args)
        {
            MyStruct b1 = new MyStruct(1); // to be considered true
            MyStruct b2 = new MyStruct(-1); // to be considered false
    
            Console.WriteLine((b1 && b2) ? "TRUE" : "FALSE");
            Console.WriteLine((b2 && b1) ? "TRUE" : "FALSE");
    
            Console.WriteLine((b1 || b2) ? "TRUE" : "FALSE");
            Console.WriteLine((b2 || b1) ? "TRUE" : "FALSE");
    
            Console.ReadLine();
        }
    }
    
    public struct MyStruct
    {
        private int _i;
    
        public MyStruct(int i)
        {
            _i = i;
        }
    
        public static bool operator true(MyStruct ms)
        {
            Console.WriteLine("op true on {0}", ms);
            return ms._i > 0;
        }
    
        public static bool operator false(MyStruct ms)
        {
            Console.WriteLine("op false on {0}", ms);
            return ms._i <= 0;
        }
    
        public static MyStruct operator &(MyStruct lhs, MyStruct rhs)
        {
            Console.WriteLine("op & on {0} {1}", lhs, rhs);
    
            if (lhs)
            {
                return rhs;
            }
            else
            {
                return new MyStruct(-1); //-1 is false
            }
        }
    
        public static MyStruct operator |(MyStruct lhs, MyStruct rhs)
        {
            Console.WriteLine("op & on {0} {1}", lhs, rhs);
    
            if (lhs)
            {
                return lhs;
            }
            else
            {
                return rhs;
            }
        }
    
        public override string ToString()
        {
            return this._i.ToString();
        }
    }
    

    I’m not sure what you mean when you say the first code can’t compile, although it doesnt use the operator true/false, I ran the following code in 2010 express and got the output:

    op bool on 1
    op bool on -1
    False
    op bool on -1
    False
    op bool on -1
    op bool on 1
    True
    op bool on 1
    True
    

    Code:

    class Program
    {
        static void Main(string[] args)
        {
            MyStruct b1 = new MyStruct(1); // to be considered true
            MyStruct b2 = new MyStruct(-1); // to be considered false
    
            Console.WriteLine(b1 && b2);
            Console.WriteLine(b2 && b1);
    
            Console.WriteLine(b2 || b1);
            Console.WriteLine(b1 || b2);
    
            Console.ReadLine();
        }
    }
    
    public struct MyStruct
    {
        private int _i;
    
        public MyStruct(int i)
        {
            _i = i;
        }
    
        public static bool operator true(MyStruct ms)
        {
            Console.WriteLine("op true on {0}", ms);
            return ms._i > 0;
        }
    
        public static bool operator false(MyStruct ms)
        {
            Console.WriteLine("op false on {0}", ms);
            return ms._i <= 0;
        }
    
        public static implicit operator bool(MyStruct ms)
        {
            Console.WriteLine("op bool on {0}", ms);
            return ms._i > 0;
        }
    
        public override string ToString()
        {
            return this._i.ToString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Does anyone know how can I replace this 2 symbol below from the string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.