8/24/2020
C# LANGUAGE BASICS
INTRODUCTION C#
C# is a modern, general-purpose, object-oriented
programming language developed by Microsoft and
approved by Ecma and ISO.
C# was developed by Anders Hejlsberg and his team
during the development of .Net Framework.
C# is designed for Common Language Infrastructure
(CLI), which consists of the executable code and runtime
environment that allows use of various high-level
languages to be used on different computer platforms
and architectures.
1
8/24/2020
DATA TYPES
In C#, variables are categorized into the following
types:
Value types
Reference types
2
8/24/2020
VALUE TYPES
Value type variables can be assigned a value directly. They are derived from the
classSystem.ValueType.
The value types consist of two main categories:
Structs
Enumerations
Structs fall into these categories:
Numeric types
Integral types
Floating-point types
decimal
bool
User defined structs.
BOOL
You can assign a Boolean value to a bool variable. You can also
assign an expression that evaluates to bool to a bool variable.
The bool keyword is an alias of System.Boolean. It is used to declare
variables to store the Boolean values, true and false.
bool b = true; or bool b = false;
3
8/24/2020
BYTE
CHAR
4
8/24/2020
DECIMAL
DOUBLE
5
8/24/2020
ENUM
The enum keyword is used to declare an enumeration, a distinct
type that consists of a set of named constants called the
enumerator list.
Usually it is best to define an enum directly within a namespace so
that all classes in the namespace can access it with equal
convenience. However, an enum can also be nested within a class
or struct.
By default, the first enumerator has the value 0, and the value of
each successive enumerator is increased by 1. For example, in the
following enumeration, Sat is 0,Sun is 1, Mon is 2, and so forth.
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
FLOAT
6
8/24/2020
INT
LONG
7
8/24/2020
SBYTE
SHORT
8
8/24/2020
STRUCT
UINT
9
8/24/2020
ULONG
USHORT
10
8/24/2020
REFERENCE TYPES
Variables of
reference types store references to
the actual data. This section introduces the
following keywords used to declare reference
types:
class
interface
delegate
Thissection also introduces the following built-
in reference types:
dynamic
object
string
DYNAMIC TYPE
You can store any type of value in the dynamic
data type variable. Type checking for these
types of variables takes place at run-time.
Syntax for declaring a dynamic type is:
dynamic <variable_name> = value; For example,
dynamic d = 20; Dynamic types are similar to
object types except that type checking for
object type variables takes place at compile
time, whereas that for the dynamic type
variables take place at run time.
11
8/24/2020
OBJECT TYPE
The Object Type is the ultimate base class for all data
types in C# Common Type System (CTS). Object is an
alias for System.Object class. So object types can be
assigned values of any other types, value types,
reference types, predefined or user-defined types.
However, before assigning values, it needs type
conversion.
When a value type is converted to object type, it is
called boxing and on the other hand, when an object
type is converted to a value type, it is called unboxing.
object obj; obj = 100; // this is boxing
STRING TYPE
The String Type allows you to assign any string values to
a variable. The string type is an alias for the
System.String class. It is derived from object type. The
value for a string type can be assigned using string
literals in two forms: quoted and @quoted.
For example,
String str = "Tutorials Point";
12
8/24/2020
BOXING
Boxing is process of converting a value type to the
reference type.
Example :
Int32 x = 10;
object o = x; // Implicit boxing
Console.WriteLine("The Object o = {0}", o); // prints out 10
Int32 x = 10;
object o = (object)x; // Explicit Boxing
Console.WriteLine("The object o = {0}", o); // prints out 10
UNBOXING
Unboxing is process of a reference type to value
type.
Example :
Int32 x = 5;
object o = x; // Implicit Boxing
x = o; // Implicit UnBoxing
Int32 x = 5;
object o = x; // Implicit Boxing
x = (Int32)o; // Explicit UnBoxing
13
8/24/2020
C# OPERATORS
OPERATOR
An operator is a symbol that tells the compiler to
perform specific mathematical or logical manipulations.
C# is rich in built-in operators and provides the following
type of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
14
8/24/2020
ARITHMETIC OPERATORS
RELATIONAL OPERATORS
15
8/24/2020
LOGICAL OPERATORS
BITWISE OPERATORS
16
8/24/2020
ASSIGNMENT OPERATORS
MISCELLANEOUS OPERATORS
17
8/24/2020
OPERATOR PRECEDENCE
C# DECISION MAKING & LOOPS
18
8/24/2020
DECISION MAKING
IF STATEMENT
19
8/24/2020
IF ..ELSE STATEMENT
NESTED IF STATEMENTS
20
8/24/2020
SWITCH STATEMENT
NESTED SWITCH
21
8/24/2020
? : OPERATOR
We have covered conditional operator ? : in previous
chapter which can be used to
replace if...elsestatements. It has the following general
form:
Exp1 ? Exp2 : Exp3;Where Exp1, Exp2, and Exp3 are
expressions. Notice the use and placement of the colon.
The value of a ? expression is determined like this: Exp1
is evaluated. If it is true, then Exp2 is evaluated and
becomes the value of the entire ? expression. If Exp1 is
false, then Exp3 is evaluated and its value becomes the
value of the expression.
C# LOOPS
22
8/24/2020
WHILE :
DO..WHILE
23
8/24/2020
FOR LOOP :
24
8/24/2020
BREAK :
CONTINUE
25