What Is The Deal With C# Operators

C# operators are special symbols that let you perform specific operations on an expression. Some well known examples of operators are below:

Operator Description
+ Addition
* Multiplication
/ Division
== Equality

That is just a few and I am sure you have all of those. But there are many, many more available in C# that you may never have heard of. Also, there are permutations of some that you have used which could be very useful to you.

Before I jump into examples, let me explain some terminology about operations. An operation is essential a phrase of code that gives a result. That result could be a string, integer or boolean value. I am happy to be correct, but I believe that an operation is scalar, in that it will always produce a single result. So X + Y will result in Z (a single value). An operation cannot result in a list or array. There are parts to an operation.

  • Operation – the entire phrase of code eg X + Y = Z
  • Operator – the symbols used in the operation eg + and = are operators in X + Y = Z
  • Operand – these are the “algebraic” parts of the operation (but they could also be integers or strings etc). eg X, 3 and Z are operands in X + 3 = Z

Hopefully that sets the scene for you. Here is a list of some condition operators that you should know about. Editor’s note: I suspect this will be an organic list and I will add more and more as I come across them. Feel free to add more in the comments.

Operator Description
&& Conditional AND. This operator can only be used with bool operands. It also means that each operand must be evaluated as true
& Logical AND. This operator is similar to &&. However, if the first operand does not evaluate to true, all further operands will be disregarded
|| Conditional OR. Only allows bool operands. If the first operand evaluates to true, further operands will not be evaluated. If the first operand evaluates to false, then the second operand will determine if the OR expression results in a true or false
| Logical OR. If the first operand is evaluated as true, all further operands will be disregarded. Therefore, the entire operation is evaluated as true
? Boolean. This operator returns one of two values depending on the result of a condition. For example, x == x ? 5 : 10; This operation says that if x equals x, then return 5, otherwise return 10.
?? Null Coalescing. This operator will return the left-hand operand if the operand is not null. Otherwise, it will return the right-hand operand. This operator has a caveat in that it can return an exception if the types mismatch. For example, consider int x = MethodReturningInt() ?? MethodReturningString(); This will throw an exception if MethodReturningInt() returns a null because the operation will return MethodReturningString() which is a string value. To cater for this, you can instantiate new objects witin the operation. For example, int x = MethodReturningInt() ?? default(int);. This will return the correctly typed result

This is just a few operators that I regularly use and I will continue adding to it. Feel free to add your own experiences to the comments so we can all see how you use various operators.

Til next time …


3 Comments

  • Some Guy says:

    Hate to be negative, but a bunch of your descriptions are wrong… && and || both short circuit, & and | do not (also you’d usually only use & and | for bitwise logic, not boolean conditionals). Your ?? example wouldn’t throw an exception, it wouldn’t even compile due to the different return types. Not to mention that even assuming that was possible, you still couldn’t use an int as the left operand since it can never be null (unless your example method returns a nullable int I suppose).

  • Thanks for sharing such a good thought, piece of writing is nice, thats why i have read it entirely

  • Bobbye says:

    Admiring the commitment you put into your website and detailed information you provide.
    It’s nice to come across a blog every once in a while that isn’t the same outdated rehashed information. Great read!
    I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

Leave a Reply

Your email address will not be published. Required fields are marked *