Escolar Documentos
Profissional Documentos
Cultura Documentos
Type
Represents
Range
Default
Value
bool
Boolean value
True or False
False
byte
0 to 255
char
16-bit Unicode
character
U +0000 to U +ffff
'\0'
decimal
0.0M
double
64-bit double-precision
floating point type
0.0D
float
32-bit single-precision
floating point type
0.0F
int
-2,147,483,648 to 2,147,483,647
long
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
0L
sbyte
-128 to 127
short
-32,768 to 32,767
uint
0 to 4,294,967,295
type
ulong
0 to 18,446,744,073,709,551,615
ushort
0 to 65,535
ToBoolean
Converts a type to a Boolean value, where possible.
ToByte
Converts a type to a byte.
ToChar
Converts a type to a single Unicode character, where possible.
ToDateTime
Converts a type (integer or string type) to date-time structures.
ToDecimal
Converts a floating point or integer type to a decimal type.
ToDouble
Converts a type to a double type.
ToInt16
ToInt32
Converts a type to a 32-bit integer.
ToInt64
Converts a type to a 64-bit integer.
10
ToSbyte
Converts a type to a signed byte type.
11
ToSingle
Converts a type to a small floating point number.
12
ToString
Converts a type to a string.
13
ToType
Converts a type to a specified type.
14
ToUInt16
Converts a type to an unsigned int type.
15
ToUInt32
Converts a type to an unsigned long type.
16
ToUInt64
Converts a type to an unsigned big integer.
Escape sequence
Meaning
\\
\ character
\'
' character
\"
" character
\?
? character
\a
Alert or bell
\b
Backspace
\f
Form feed
\n
Newline
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\ooo
\xhh . . .
Following table shows all the arithmetic operators supported by C#. Assume
variable A holds 10 and variable B holds 20 then:
Show Examples
Operator
Description
Example
A+B=
30
A-B=
-10
A*B=
200
B/A=2
B%A=
0
++
A++ =
11
--
A-- = 9
Following table shows all the relational operators supported by C#. Assume
variable A holds 10 and variable B holds 20, then:
Show Examples
Operator
Description
Example
==
(A == B)
is not
true.
!=
(A != B)
is true.
>
(A > B)
is not
true.
<
(A < B)
is true.
>=
(A >= B)
is not
true.
<=
(A <= B)
is true.
Following table shows all the logical operators supported by C#. Assume
variable A holds Boolean value true and variable B holds Boolean value
false, then:
Show Examples
Operator
Description
Example
&&
(A && B)
is false.
||
(A || B)
is true.
!(A &&
B) is
true.
Bitwise operator works on bits and perform bit by bit operation. The truth
tables for &, |, and ^ are as follows:
p
p&q
p|q
p^q
Assume if A = 60; and B = 13; then in the binary format they are as
follows:
A = 0011 1100
B = 0000 1101
----------------A&B = 0000 1100
A|B = 0011 1101
Description
Example
&
(A & B) =
12, which is
0000 1100
(A | B) =
61, which is
0011 1101
(A ^ B) =
49, which is
0011 0001
(~A ) = 61,
which is
1100 0011
in 2's
complement
due to a
signed
binary
number.
<<
A << 2 =
240, which
is 1111
0000
>>
A >> 2 =
15, which is
operand.
0000 1111
Description
Example
C=A+B
assigns
value of A
+ B into C
+=
C += A is
equivalent
to C = C
+A
-=
C -= A is
equivalent
to C = C A
*=
C *= A is
equivalent
to C = C
*A
/=
C /= A is
equivalent
to C = C /
A
%=
C %= A is
equivalent
to C = C
%A
<<=
C <<= 2
is same
as C = C
<< 2
>>=
C >>= 2
is same
as C = C
>> 2
&=
C &= 2 is
same as C
=C&2
^=
C ^= 2 is
same as C
=C^2
|=
C |= 2 is
same as C
=C|2
There
are
few
other
important
typeof and ? :supported by C#.
operators
including sizeof,
Show Examples
Operator
Description
Example
sizeof()
sizeof(int), returns 4.
typeof()
typeof(StreamReader);
&
Pointer to a variable.
?:
Conditional Expression
If Condition is true ?
Then value X :
Otherwise value Y
is
as
Property
IsFixedSize
Gets a value indicating whether the Array has a fixed size.
IsReadOnly
Gets a value indicating whether the Array is read-only.
Length
Gets a 32-bit integer that represents the total number of elements in all
the dimensions of the Array.
LongLength
Gets a 64-bit integer that represents the total number of elements in all
the dimensions of the Array.
Rank
Gets the rank (number of dimensions) of the Array.
Methods
Clear
Sets a range of elements in the Array to zero, to false, or to null,
depending on the element type.
CopyTo(Array, Int32)
Copies all the elements of the current one-dimensional Array to the
specified one-dimensional Array starting at the specified destination
Array index. The index is specified as a 32-bit integer.
GetLength
Gets a 32-bit integer that represents the number of elements in the
specified dimension of the Array.
GetLongLength
Gets a 64-bit integer that represents the number of elements in the
specified dimension of the Array.
GetLowerBound
Gets the lower bound of the specified dimension in the Array.
GetType
Gets the Type of the current instance. (Inherited from Object.)
GetUpperBound
Gets the upper bound of the specified dimension in the Array.
GetValue(Int32)
Gets the value at the specified position in the one-dimensional Array. The
index is specified as a 32-bit integer.
10
IndexOf(Array, Object)
Searches for the specified object and returns the index of the first
occurrence within the entire one-dimensional Array.
11
Reverse(Array)
Reverses the sequence of the elements in the entire one-dimensional
Array.
12
SetValue(Object, Int32)
Sets a value to the element at the specified position in the onedimensional Array. The index is specified as a 32-bit integer.
13
Sort(Array)
Sorts the elements in an entire one-dimensional Array using the
IComparable implementation of each element of the Array.
14
ToStringk
Returns a string that represents the current object. (Inherited from
Object.)
Methods
10
11
12
13
15
16
17
18
19
20
21
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Preprocessor Directives in C#
The following table lists the preprocessor directives available in C#:
Preprocessor
Directive
Description.
#define
#undef
#if
#else
#elif
#endif
#line
It lets you modify the compiler's line number and (optionally) the
file name output for errors and warnings.
#error
#warning
#region
It lets you specify a block of code that you can expand or collapse
when using the outlining feature of the Visual Studio Code Editor.
#endregion
Exception Classes in C#
C# exceptions are represented by classes. The exception classes in C# are
mainly directly or indirectly derived from the System.Exception class.
Some of the exception classes derived from the System.Exception class are
theSystem.ApplicationException and System.SystemException classes
.
The System.ApplicationException class supports exceptions generated
by application programs. Hence the exceptions defined by the programmers
should derive from this class.
The System.SystemException class is the base class for all predefined
system exception.
The following table provides some of the predefined exception classes
derived from the Sytem.SystemException class:
Exception Class
Description
System.IO.IOException
System.IndexOutOfRangeException
System.ArrayTypeMismatchException
System.NullReferenceException
System.DivideByZeroException
System.InvalidCastException
System.OutOfMemoryException
System.StackOverflowException