Introduction
Here we will discuss how to overload short-circuit operators in C#. This articles assumes knowledge of operator overloading in C#.
The short-circuit operators are && and ||. They work nearly the same as ampersand (&) and simple pipeline (|) but there are some differences how the short-circuit operators work and the ampersand single pipeline.
bool a=false;
bool b = true;
If(a & b)
//some code
If(a && b)
//some code
For a layman, both expressions will work the same and will generate the same output but for experienced programmers there is a difference between how the short-circuit (&&) and ampersand (&) work.
How a short-circuit works
In C# a short-circuit operator can used in a bool expression only . it will return true and depending on the condition in the control statement.
If the short-circuit finda an operand that can reflect the result of the expression then it will stop checking the remaining operands and execute the condition true or false that is being reflected by the operand.
In the code above in the if statement && will find that a is false and in the AND operator all the operands must be true in order to execute if the statement. In this case a is false so it will not check b and will transfer control to the else statement. The && and || operators do not work with integers or any other type except bool.
Working of ampersand or single pipeline operator
The main difference between a short-circuit and a single pipeline/ampersand operator is the & and | operators can work with integers too. It compares the two operands bitwise and ANDs them. The result can be stores in another integer variable as in the following:
int a =4&5// this will compare 4 and 5 bitwise and AND them in binary form
int b=4|5// this compare 4and 5 bitwise and OR them in binary form
Another difference is that the & and| operator check all the conditions, even if the result of the control statement is being reflected by the current operand.
bool a=false;
bool b = true;
If(a & b) // result of b will also be check event a reflect that the statement is false
//some code
If(b|a)// b reflect result of statement but it will still check a
//some code
Overloading short-circuit operator
C# did not allow overloading the short-circuit operator directly just like +,-,/,|,& and so on.
But there are certain rules if we fulfil them when can use a short-circuit operator for a user-defined data type or in a simple that can overload them. In fact we do not overload them but if the conditions are fulfilled then they are overloaded by the compiler logically.
Rules for overloading the short-circuit operator.
- | and & must be overloaded for the given class.
- | and & must return an object of the class for which they are overloaded.
- | and & must have a reference to an object of the class for which they are overloaded.
- True and false statements must be overloaded.
If the above 4 rules are fulfilled by our program; we can use the short-circuit operator for a user-defined data type in overloaded form without actually overloading them.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace operatorOverLoading
- {
-
- class demension
- {
- private int height;
- private int width;
-
- public demension()
- {
- height = 0;
- width = 0;
- }
-
- public demension(int h, int w)
- {
- height = h;
- width = w;
-
- }
-
- public void show()
- {
- Console.WriteLine("height :" + height);
- Console.WriteLine("width :" + width);
- }
-
- public static demension operator |(demension d1, demension d2)
- {
- if (((d1.width != 0) || (d1.height != 0)) | ((d2.width != 0) || (d2.height != 0)))
- return new demension(1, 1);
- else
- return new demension(0, 0);
-
- }
-
- public static demension operator &(demension d1, demension d2)
- {
- if (((d1.width != 0) && (d1.height != 0)) & ((d2.width != 0) && (d2.height != 0)))
-
- return new demension(1, 1);
-
- else
- return new demension(0, 0);
-
- }
-
-
-
- public static bool operator true(demension op)
- {
- if ((op.width != 0) || (op.height != 0))
- return true;
- else
- return false;
- }
-
- public static bool operator false(demension op)
- {
- if ((op.width == 0) && (op.height == 0))
- return true;
- else
- return false;
- }
-
-
- }
- class Program
- {
- static void Main(string[] args)
- {
- demension a = new demension(5, 6);
- demension b = new demension(10, 10);
- demension c = new demension(0, 0);
- if (b && a) Console.WriteLine("a && c is true.");
- else Console.WriteLine("a && c is false.");
-
- if (a || b) Console.WriteLine("a || b is true.");
- else Console.WriteLine("a || b is false.");
-
- if (a || c) Console.WriteLine("a || c is true.");
- else Console.WriteLine("a || c is false.");
-
- Console.ReadKey();
- }
- }
- }
How the code will work
If (b&&a)
At first control will transfer to the false operator to check whether or not it is false for the operand a. If the false statement returns true then the short-circuit operator will not check b and control will transfer to the else statement.
If the false statement returns false then the & operator will be called for the b operand and checks if the statement inside the & operator is true. If so then two argument constructors will be called with (1,1) and this newly created object is returned to the b operand and the true statement is checked; if satisfied then it will return true and if not then it will return false.
The if statement inside the & operator is false. if so then two argument constructors will be called with (0,0) and this newly created object is returned to the b operand and the true statement is checked. If satisfied then it will return true if not it will return false.
Then depending on that the if or else will be executed in the main.