Operators & Variables In C#

C# has a number of standard operators, taken from C, C++ and Java. Most of these should be quite familiar to programmers; the less common ones are enclosed elsewhere.
 
Standard operators list are given below. Note that when we are writing any classes classes it is possible to change the default behaviors of some of these operators (i.e. to ‘overload’ the operator), although this should only be done where the resultant semantics makes sense. The diagram indicates which of the operators are overloadable.
 
Category Name Syntax Example Overloadable?
Primary Grouping (a+b) No
Member A.B No
Struct pointer member access A->B No
Method call f(x) No
Post increment c++ Yes
Post decrement c– Yes
Constructor call c = new Coord(); No
Array stack allocation int* c = stackalloc int[10] No
Struct size retrieval sizeof (int) No
Arithmetic check on checked {byte c = (byte) d;} No
Arithmetic check off unchecked {byte c = (byte) d;} No
Unary Positive value +10 Yes
Negative value -10 Yes
Not !(c==d) Yes
Bitwise complement ~(int x) Yes
Pre increment ++c Yes
Pre decrement –c Yes
Type cast (myType)c No
Value at address int* c = d; No
Address value of int* c = &d; No
Type operators Type equality / compatibility a is String No
Type retrieval typeof (int) No
Arithmetic Multiplication c*d Yes
Division c/d Yes
Remainder c%d Yes
Addition c+d Yes
Subtraction c-d Yes
Shift bits right c>>3 Yes
Shift bits left c<<3 Yes
Relational and Logical Less than c<d Yes
Greater than c>d Yes
Less than or equal to c<=d Yes
Greater than or equal to c>=d Yes
Equality c==d Yes
Inequality c!=d Yes
Bitwise and c&d Yes
Bitwise or c|d Yes
Logical and c&&d No
Logical or c||d No
Conditional int c=(d<10) ? 5:15 No
 
Overloading operators
 
To overload an operator in a class, one defines a method using the ‘operator’ keyword. For instance, the following code overloads the equality operator.
 
public static bool operator == (Value a, Value b) {return a.Int == b.Int}
 
Where an operator is one of a logical pair, both operators should be overwritten if any one is. These pairs are the following:
 
== and !=
< and >
<= and >=
 
Variables
 
A Variable is a named location that stores a value. Although variables are physically stored in the RAM, their actual memory address is not known to the programmer. We can use the variables via the name we give them. These are the rules for naming a variable:-
 
• The name must begin with a letter or an underscore & can be followed by a sequence of letters (A-Z), digits (0-9) or underscore (_)

• Special Characters such as ? – + * / \ ! @ # $ % ^ ( ) [ ] { } , ; : . can’t be used in the variable name.

• A variable name must not be the same as a reserved keyword such as using, public, etc.

• The name can contain any number of the allowed characters

• Variables with the same scope cannot have the same name

 
Note: C# being a case-sensitive language, two variables such as var1 and Var1 would be different.
 
Any Variable has a type associated with it, which basically restricts the type of value it can store. The table below shows the Data types available in C#, the size they occupy in the memory and the values they can contain.
 
Type Size Range of Variables
char 2 0 and 65536
int 4 -2,147,483,648 and 2,147,483,647
float 4 For negative values -3.402823E + 38 and -1.401298E -45For positive values
1.401298E -45 and 3.402823E +38
double 8 For negative values-1.79769313486232E308 and 4.94065645841247E-324
For positive values
4.94065645841247E-324 and 1.79769313486232E308
bool 1 true or false
string Depends on length of the string 0 – 2 million Unicode Characters
 
Variable Declaration
 
Before we can use a variable, we need to declare it. Declaration of variables is done using the syntax of variable declaration is:-
 
<data type> <variable name>;
 
For Example
 
int Age;
char Sex;
string Name;
bool IsMarried;
 
Variables of the same data type can also be declared using the syntax:-
 
<data type> <variable 1>,<variable 2>, .. and so on;
 
Example
 
int Age, RollNum;
 
Initialization
 
While declaring a variable, it is possible to assign an initial value to it. This operation is called Initialization and it has the following syntax:-
 
<data type> <variable name> = <value>;
 
Example
 
int Age = 20;
char Sex = ‘M’;
string Name = ‘Max Steel’;
bool IsMarried = false;
 
Multiple variables of the same data type can also be initialized in a single line of code using the syntax:-
 
<data type> <variable 1> = <value>, <variable 2> = <value>;
 
Example
 
int Age = 20, RollNum = 69;
 
Variable types
 
C# is a type-safe language. Variables are declared as being of a particular type, and each variable is constrained to hold only values of its declared type. Variables can hold either value types or reference types, or they can be pointers.
 
Here’s we can summarizes the difference between value types and reference types:
 
– where a variable v contains a value type, it directly contains an object with some value. No other variable v’ can directly contain the object contained by v (although v’ might contain an object with the same value).
 
– where a variable v contains a reference type, what it directly contains is something which refers to an object. Another variable v’ can contain a reference to the same object referred to by v.
 
Value Types
 
It is possible in C# to define your own value types by declaring enumerations or structs. These user-defined types are mostly treated in exactly the same way as C#’s predefined value types, although compilers are optimized for the latter.
 
The following table lists, and gives information about, the predefined value types. Because in C# all of the apparently fundamental value types are in fact built up from the (actually fundamental) object type, the list also indicates which System types in the .Net framework correspond to these pre-defined types.
 
C# Type .Net Framework (System) type Signed? Bytes Occupied Possible Values
sbyte System.Sbyte Yes 1 -128 to 127
short System.Int16 Yes 2 -32768 to 32767
int System.Int32 Yes 4 -2147483648 to 2147483647
long System.Int64 Yes 8 -9223372036854775808 to 9223372036854775807
byte System.Byte No 1 0 to 255
ushort System.Uint16 No 2 0 to 65535
uint System.UInt32 No 4 0 to 4294967295
ulong System.Uint64 No 8 0 to 18446744073709551615
float System.Single Yes 4 Approximately ±1.5 x 10-45 to ±3.4 x 1038 with 7 significant figures
double System.Double Yes 8 Approximately ±5.0 x 10-324 to ±1.7 x 10308 with 15 or 16 significant figures
decimal System.Decimal Yes 12 Approximately ±1.0 x 10-28 to ±7.9 x 1028 with 28 or 29 significant figures
char System.Char N/A 2 Any Unicode character (16 bit)
bool System.Boolean N/A 1 / 2 true or false
 
In the following lines of code, two variables are declared and set with integer values.
 
int x = 10;
int y = x; y = 20; //
after this statement x holds value 10 and y holds value 20
 
Reference Types
 
The pre-defined reference types are object and string, where object (as we have mentioned above) is the ultimate base class of all other types. New reference types can be defined using ‘class’, ‘interface’, and ‘delegate’ declarations.
 
Reference types actually hold the value of a memory address occupied by the object they reference. Consider the following piece of code, in which two variables are given a reference to the same object (for the sake of the example, this object is taken to contain the numeric property ‘myRefValue’).
 
object x = new object();
x.myRefValue = 10;
object y = x; y.myRefValue = 20;
// after this statement both x.myRefValue and y.myRefValue equal 20
 
This code illustrates how changing a property of an object using a particular reference to it is reflected in all other references to it. Note, however, that although strings are reference types, they work rather more like value types. When one string is set to the value of another, for example
 
string s1 = “hello”;
string s2 = s1;
 
Then s2 does at this point reference the same string object as s1. However, when the value of s1 is changed, for instance with
 
s1 = “goodbye”;
 
Here a new string object of s1 is created.. Hence, above code, s1 equals “goodbye”, whereas s2 still equals “hello”.
 
The reason for this behavior is that string objects are ‘absolute’. That is, the properties of these objects can’t themselves change. So in order to change what a string variable references, a new string object must be created.