C# 03 - Operators and Converting Data Types
C# 03 - Operators and Converting Data Types
Ateik Alzehla
Content
Nullable
Operators
Operators Overloading
Casting, Boxing and Unboxing
Nullable
Variables of reference types (classes) can be null while variables of value types
(structs) cannot.
int x=null;
Why Java Solution has important disadvantage?
C# has a solution for this: nullable types.
A nullable type is a ValueType that can be null. You just have to put the ? after the
type
int? x=null;
int? x = 3;
Assignment problem. int y = x; int? x = 3; int? x = 3;
int y = (int)x; int y = 0;
Value And HasValue
if (x.HasValue)
y = x.Value;
Why Nullable is Important?
Operators
What is an operator? int? x = 3;
if(++x == 4) { }
Evaluation if(x++ == 4) { }
Conditional Operator ?: int? x = 3;
var y = x.HasValue ? x.Value : 0;
condition ? true_value: false_value
The is Operator
allows you to check whether an object is compatible with a specific type.
if( x is string) {… }
if(x is Person) { } if(x is Person p)
{
if(x is null) { } p.FirstName = "“;
}
Operators (Continued)
The as Operator:
used to perform explicit type conversions of reference types
if the types are incompatible, the as operator returns the value null.
object o1 = "Some String";
object o2 = 5;
string s1 = o1 as string; // s1 = "Some String"
string s2 = o2 as string; // s2 = null
The sizeof Operator:
Determine the size (in bytes) required on the stack by a value type
cannot use sizeof with classes
Operators (Continued)
The typeoff Operator:
The typeof operator returns a System.Type object representing a specified
type
For example, typeof(string) returns a Type objectrepresenting the
System.String type
var theType = typeof(string); // returns a Type object representing the System.String type.
The nameof Operator:
accepts a symbol, property, or method and returns the name
The Null Coalescing Operator ?? :
provides a shorthand mechanism to cater to the possibility of null values
when working with nullable and reference types
The operator is placed between two operands—the first operand must be a
nullable type or reference type, and the second operand must be of the
same type as the first or of a type that is implicitly convertible to the type of
the first operand.
int? a = null;
int b;
b = a ?? 10; // b has the value 10
a = 3;
b = a ?? 10; // b has the value 3
Operators (Continued)
The Null-Conditional Operator ?. && ?[] :
To reduce the number of code lines.
It checks the variablevarwhether null= or
phoneNumber not, before accessing its members.
school?.Students?[0].Parent?.PhoneNumber;
Example: Imagine you are designing multi-org school system and you want
to get the phone number of parent of student:
var phoneNumber= school.Students[0].Parent.PhoneNumber;
Custom Index Operator []:
public Person this[int index]
{
get => _people[index]; public struct Point
set => _people[index] = value; {
} public Point(int x, int y)
{
Operator Overloading: X = x;
Y = y;
public void Main(string[] args) }
{ public int X { get; set; }
Point p1 = new Point(3, 5); public int Y { get; set; }
Point p2 = new Point(3, 5); public static Point operator +(Point p1, Point p2)
Point p3 = p1 + p2; {
} return new Point(p1.X + p2.X, p1.Y + p2.Y);
}
}
Casting:
Casting:
Converting a type to another.
Implicit Conversions=> see the Table (page 324)
int myIntNumber = 20;
long myLongNumber = myIntNumber; // Implicit cast
Boxing and Unboxing
all types—both the simple predefined types, such as int and char, and the
complex types, such as classes and structs—derive from the object type
Boxing: in fact, It is a casting of types(Value or Reference) to object type
Unboxing the Boxing counterpart( casting object to Type)