Tutorial by Examples

C# allows user-defined types to overload operators by defining static member functions using the operator keyword. The following example illustrates an implementation of the + operator. If we have a Complex class which represents a complex number: public struct Complex { public double Real ...
Equals Checks whether the supplied operands (arguments) are equal "a" == "b" // Returns false. "a" == "a" // Returns true. 1 == 0 // Returns false. 1 == 1 // Returns true. false == true // Returns false. false == false // Return...
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 ...
Returns an int holding the size of a type* in bytes. sizeof(bool) // Returns 1. sizeof(byte) // Returns 1. sizeof(sbyte) // Returns 1. sizeof(char) // Returns 2. sizeof(short) // Returns 2. sizeof(ushort) // Returns 2. sizeof(int) // Returns 4. sizeof(uint) // Returns 4....
Overloading just equality operators is not enough. Under different circumstances, all of the following can be called: object.Equals and object.GetHashCode IEquatable<T>.Equals (optional, allows avoiding boxing) operator == and operator != (optional, allows using operators) When overrid...
var now = DateTime.UtcNow; //accesses member of a class. In this case the UtcNow property.
var zipcode = myEmployee?.Address?.ZipCode; //returns null if the left operand is null. //the above is the equivalent of: var zipcode = (string)null; if (myEmployee != null && myEmployee.Address != null) zipcode = myEmployee.Address.ZipCode;
var age = GetAge(dateOfBirth); //the above calls the function GetAge passing parameter dateOfBirth.
var letters = "letters".ToCharArray(); char letter = letters[1]; Console.WriteLine("Second Letter is {0}",letter); //in the above example we take the second character from the array //by calling letters[1] //NB: Array Indexing starts at 0; i.e. the first letter would be give...
var letters = null; char? letter = letters?[1]; Console.WriteLine("Second Letter is {0}",letter); //in the above example rather than throwing an error because letters is null //letter is assigned the value null
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. true ^ false // Returns true false ^ true // Returns true false ^ false // Returns false true ^ true // Returns false
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 uint value = 15; // 00001111 uint doubled = value << 1; // Resu...
C# allows user-defined types to control assignment and casting through the use of the explicit and implicit keywords. The signature of the method takes the form: public static <implicit/explicit> operator <ResultingType>(<SourceType> myType) The method cannot take any more argu...
C# has several operators that can be combined with an = sign to evaluate the result of the operator and then assign the result to the original variable. Example: x += y is the same as x = x + y Assignment operators: += -= *= /= %= &= |= ^= <<= >>=
Returns one of two values depending on the value of a Boolean expression. Syntax: condition ? expression_if_true : expression_if_false; Example: string name = "Frank"; Console.WriteLine(name == "Frank" ? "The name is Frank" : "The name is not Frank"); ...
Gets System.Type object for a type. System.Type type = typeof(Point) //System.Drawing.Point System.Type type = typeof(IDisposable) //System.IDisposable System.Type type = typeof(Colors) //System.Drawing.Color System.Type type = typeof(List<>) //System.Collections....
Value Type (where T : struct) The built-in primitive data types, such as char, int, and float, as well as user-defined types declared with struct, or enum. Their default value is new T() : default(int) // 0 default(DateTime) // 0001-01-01 12:00:00 AM default(char) // '...
Returns a string that represents the unqualified name of a variable, type, or member. int counter = 10; nameof(counter); // Returns "counter" Client client = new Client(); nameof(client.Address.PostalCode)); // Returns "PostalCode" The nameof operator was introduced in C# ...
6.0 Introduced in C# 6.0, the Null Conditional Operator ?. will immediately return null if the expression on its left-hand side evaluates to null, instead of throwing a NullReferenceException. If its left-hand side evaluates to a non-null value, it is treated just like a normal . operator. Note tha...
Postfix increment X++ will add 1 to x var x = 42; x++; Console.WriteLine(x); // 43 Postfix decrement X-- will subtract one var x = 42 x--; Console.WriteLine(x); // 41 ++x is called prefix increment it increments the value of x and then returns x while x++ returns the value of x and the...

Page 1 of 2