| Start | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Overloadable Operators C# allows user-defined types to overload operators by defining static member functions using the If we have a And we want to add the option to use the We will need to overload the Operators such as Comparison operators have to be overloaded in pairs (e.g. if A full list of overloadable operators (as well as non-overloadable operators and the restrictions placed on some overloadable operators) can be seen at MSDN - Overloadable Operators (C# Programming Guide) (opens new window). overloading of Given a type An overloadable which can be used like this (The example is taken from the Roslyn Pattern Matching Documentation (opens new window)) |
||||||||||||
| Overloading equality operators Overloading just equality operators is not enough. Under different circumstances, all of the following can be called:
When overriding When NOT overridden Generally equality operation must obey the following rules:
|
||||||||||||
| Relational Operators Equals Checks whether the supplied operands (arguments) are equal Unlike Java, the equality comparison operator works natively with strings. The equality comparison operator will work with operands of differing types if an implicit cast exists from one to the other. If no suitable implicit cast exists, you may call an explicit cast or use a method to convert to a compatible type. Unlike Visual Basic.NET, the equality comparison operator is not the same as the equality assignment operator. Not to be confused with the assignment operator ( For value types, the operator returns Not Equals Checks whether the supplied operands are not equal. This operator effectively returns the opposite result to that of the equals ( Greater Than Checks whether the first operand is greater than the second operand. Less Than Checks whether the first operand is less than the second operand. Greater Than Equal To Checks whether the first operand is greater than equal to the second operand. Less Than Equal To Checks whether the first operand is less than equal to the second operand. |
||||||||||||
| Implicit Cast and Explicit Cast Operators C# allows user-defined types to control assignment and casting through the use of the The method cannot take any more arguments, nor can it be an instance method. It can, however, access any private members of type it is defined within. An example of both an Allowing the following cast syntax: The cast operators can work both ways, going from your type and going to your type: Finally, the It will generate a compilation error. |
||||||||||||
| Short-circuiting Operators By definition, the short-circuiting boolean operators will only evaluate the second operand if the first operand can not determine the overall result of the expression. It means that, if you are using && operator as firstCondition && secondCondition it will evaluate secondCondition only when firstCondition is true and ofcource the overall result will be true only if both of firstOperand and secondOperand are evaluated to true. This is useful in many scenarios, for example imagine that you want to check whereas your list has more than three elements but you also have to check if list has been initialized to not run into NullReferenceException. You can achieve this as below: mList.Count > 3 will not be checked untill myList != null is met. Logical AND
Logical OR
Example usage |
||||||||||||
| ? : Ternary Operator Returns one of two values depending on the value of a Boolean expression. Syntax: Example: The ternary operator is right-associative which allows for compound ternary expressions to be used. This is done by adding additional ternary equations in either the true or false position of a parent ternary equation. Care should be taken to ensure readability, but this can be useful shorthand in some circumstances. In this example, a compound ternary operation evaluates a Ternary operators can also be nested, such as: When writing compound ternary statements, it's common to use parenthesis or indentation to improve readability. The types of expression_if_true and expression_if_false must be identical or there must be an implicit conversion from one to the other. The type and conversion requirements apply to your own classes too. |
||||||||||||
| ?. (Null Conditional Operator) Introduced in C# 6.0 (opens new window), the Null Conditional Operator This comes handy when firing events. Normally you would have to wrap the event call in an if statement checking for |
||||||||||||
| sizeof Returns an *Only supports certain primitive types in safe context. In an unsafe context, |
||||||||||||
Class Member Operators: Null Conditional Member Access |
||||||||||||
Class Member Operators: Null Conditional Indexing |
||||||||||||
| "Exclusive or" Operator The operator for an "exclusive or" (for short XOR) is: ^ This operator returns true when one, but only one, of the supplied bools are true. |
||||||||||||
| Bit-Shifting Operators The shift operators allow programmers to adjust an integer by shifting all of its bits to the left or the right. The following diagram shows the affect of shifting a value to the left by one digit. Left-Shift Right-Shift |
||||||||||||
default Operator Value Type (where T : struct)The built-in primitive data types, such as Reference Type (where T : class)Any |
||||||||||||
| Postfix and Prefix increment and decrement Postfix increment Postfix decrement
while both are commonly used in for loop |
||||||||||||
| => Lambda operator The It is used to declare lambda expressions and also it is widely used with LINQ Queries (opens new window): When used in LINQ extensions or queries the type of the objects can usually be skipped as it is inferred by the compiler: The general form of lambda operator is the following: The parameters of the lambda expression are specified before This operator can be used to easily define delegates, without writing an explicit method: instead of |
||||||||||||
| Assignment operator '=' The assignment operator |
||||||||||||
| ?? Null-Coalescing Operator The Null-Coalescing operator The |
||||||||||||
Class Member Operators: Member Access |
||||||||||||
Class Member Operators: Function Invocation |
||||||||||||
Class Member Operators: Aggregate Object Indexing |
||||||||||||
| Binary operators with assignment C# has several operators that can be combined with an Example: is the same as Assignment operators:
|
||||||||||||
| typeof Gets To get the run-time type, use Operator |
||||||||||||
| nameof Operator Returns a string that represents the unqualified name of a The Syntax
Parameters
RemarksAll operators are defined as Operator PrecedenceAll operators have a particular "precedence" depending on which group the operator falls in (operators of the same group have equal precedence). Meaning some operators will be applied before others. What follows is a list of groups (containing their respective operators) ordered by precedence (highest first):
Unary Operators Multiplicative Operators Additive Operators Bitwise Shift Operators Relational/Type-testing Operators Equality Operators Logical AND Operator Logical XOR Operator Logical OR Operator Conditional AND Operator Conditional OR Operator Null-coalescing Operator Conditional Operator Related Content |
Read Article
https://develop.horje.com/learn/1434/reference