Operators, Variables and Data Types
VARIABLES
Variable values can be of various types (e.g, integer, decimal, or character) in C#. The type-safe feature of the language causes the compiler to confirm a correct data type for a variable's value. The compiler also rejects inappropriate operations on variables such as addition with a boolean variable. C# allows users to change variables.
The syntax of a variable declaration follows:
DATATYPE NAME = VALUE
Review an example below:
int IDnumber = 7895436;
Variables can also be declared using the var keyword, which tells the compiler to infer its data type:
var maximumAmount = 1,000,000;
When declared, a variable cannot be declared with a new data type or assigned an incompatible value, however, its type can be converted through lossless polymorphism.
C# variables are classified as static, instance, or local variables. Static variables, which use the static keyword, are global variables. Instance variables only exist on the creation of a new class instance. Local variables only exist within their block
DATA TYPES
C# is a strongly typed language;thus, every constant, expression, and variable must have a data type. All method signatures must specify the data type of parameters and return values. C# offers a set of built-in numeric types and a broader set of more complex types covering constructs like network connections, arrays, and dates.
Two important aspects of types in the .NET framework follow:
- It supports inheritance. Types can come from parent types (i.e., “base” types).
- Each type falls in the category of either a value type or a reference type. Types defined through the struct keyword are value types. All native numeric types are structs. Types defined through the class keyword are reference types. Different rules are associated with each category.
Certain types have distinction:
- Object type – This reference type serves as the ultimate and incredibly versatile base class for every data type within C#.
- String type – This reference type allows for all string values to be assigned to a variable.
- Dynamic type - This reference type stores virtually any kind of value.
- Pointer type – Variables of this type hold the memory address of a type, and offer the same capabilities of C and C++ pointers.
OPERATORS
Operators offered by C# include the following types:
Primary Operators | |
---|---|
x.y | Allows access to members |
x?.y | Provides conditional member access |
f(x) a?[x] |
Allows array and indexer access Allows conditional array and indexer access |
x++ | post-increment |
x-- | post-decrement |
new T(..) new T(..){..} new{..} new T[..] |
Creates objects and delegates Creates objects with an initializer Provides an anonymous initializer Creates an array |
typeof(T) | Gets the System.Type object for T |
checked(x) | Performs a checked evaluation |
unchecked(x) | Performs an unchecked evaluation |
default(T) | Gets the default value of type T |
delegate{} | An anonymous function (and method) |
Unary Operators | |
---|---|
+x | Identity |
-x | Negation |
!x |
Logical negation |
~x | Bitwise negation |
++x | pre-increment |
--x |
pre-decrement |
(T)x | Converts x explicitly to type T |
Multiplicative Operators | |
---|---|
* | Multiplication |
/ | Division |
% | Remainder |
Additive Operators | |
---|---|
x + y | Subtraction, removal of delegates |
Shift Operators | |
x<<y | Left shift |
x>>y | Right shift |
Type and Relational Operators | |
---|---|
x<y | Less than |
x>y | Greater than |
x <= y | Less than or equal |
x >= y | Greater than or equal |
x is T | If x is a T, return true, otherwise false |
x as T | Return x as type T, or null if x is not T type |
Equality Operators | |
---|---|
x == y | Equal |
x != y | Not equal |
Logical, Conditional, and Null Operators | ||
---|---|---|
AND (logical) | x & y | Boolean AND, Integer bitwise AND |
XOR (logical) | x ^ y | Boolean XOR, Integer bitwise XOR |
OR (logical) | x | y | Boolean OR, Integer bitwise OR |
AND (conditional) | x && y | If x is true, it evaluates y |
OR (conditional) | x || y | If x is false, it evaluates y |
Null coalescing | x ?? y | If x is null, it evaluates to y, else x |
Conditional | x ? y:z | If x is true, it evaluates to y, else z |
Assignment and Anonymous Operators | |
---|---|
= | Assignment |
x operator = y | Compound Assignment (+=, *=, etc.) |
(T x) => y | Lambda expression (anonymous function) |
Expressions evaluate from left to right, but parentheses change the order dictated by operator precedence. Note that parentheses do not override a right associative operator such as “=”.