C# and .
Net Framework Unit - 2
============================== DATA TYPES IN C# =============================
Every variable in C# is associated with a data type.
Data types specify the size and type of values that can be stored. C# is
a language rich in its data types.
The types in C# are primarily divided into two categories:
Value types
Reference types
Value types
Value types (which are of fixed length) are stored on the stack,
and when a value of a variable is assigned to another variable, the
value is actually copied. This means that two identical copies of the
value are available in memory.
Reference types
Reference types (which are of variable length) are stored on the
heap, and when an assignment between two reference variables
occurs, only the reference is copied; the actual value remains in the
same memory location. This means that there are two references to
a single value.
================================= VALUE TYPE ================================
The value types of C# can be grouped into two categories (as shown in
Fig. 4.2), namely,
User-defined types (or complex
types) and
Predefined types (or simple
types)
We can define our own complex types
known as user-defined value types
which include struct types and
enumerations.
Predefined value types which are also
known as simple types (or primitive
types) are further subdivided into:
Numeric types,
Boolean types, and
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 1
C# and .Net Framework Unit - 2
Character types.
Numeric types include integral types,
floating-point types and decimal types.
These are shown diagrammatically in
Fig. 4.3.
Integer Type:
Integral types can hold whole numbers such as 123, -96 and 5639. The
size of the values that can be stored depends on the integral data type we
choose. C# supports the concept of unsigned types and therefore it
supports eight types of integers as shown in Figs 4.4 and 4.5.
Signed Integer Type:
Signed integer types can hold both positive and negative numbers. Table
4.3 shows the memory size and range of all the four signed integer data
types. (Note that one byte is equal to eight bits).
It should be remembered that wider data types require more time for
manipulation and therefore it is advisable to use smaller data types
wherever possible. For example, instead of storing a number like 50 in an
int type variable, we must use a sbyte variable to handle this number.
This will improve the speed of execution of the program.
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 2
C# and .Net Framework Unit - 2
Unsigned Integer Type:
We can increase the size of the positive value stored in an integer type by
making it 'unsigned'. For example, a 16-bit short integer can store values
in the range -32,768 to 32,767. However, by making it ushort, it can
handle values in the range 0 to 65,535. Table 4.4 shows the size and
range of all the four unsigned integer data types.
All integers are by default int type. In order to specify which of the other
integer types the values must take, we must append the characters U, L
or UL as shown below:
123 U (for uint type)
123 L (for long type)
123 UL (for ulong type)
We may also use lower case u and I, although the letter I is confused with
the number 1.
Floating Point Type:
Integer types can hold only whole numbers and therefore we use another
type known as floating-point type to hold numbers containing fractional
parts such as 27.59 and -1.375. There are two kinds of floating point
storage in C# as shown in Fig. 4.6.
The float type values are single-precision numbers with a precision of
seven digits. The double types represent double-precision numbers with a
precision of 15/16 digits. Table 4.5 gives the size and range of these two
types.
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 3
C# and .Net Framework Unit - 2
Floating-point numbers, by default, as double-precision quantities. To
force them to be in single- precision mode, we must append for F to the
numbers.
Example: 1.23f 7.56923e5f
Double-precision types are used when we need greater precision in
storage of floating-point numbers.
Decimal Type:
The decimal type is a high precision, 128-bit data type that is designed for
use in financial and monetary calculations. It can store values in the range
1.0 x 10-28 to 7.9 x 1028 with 28 significant digits. Note that the precision
is given in digits, not in decimal places.
To specify a number to be decimal type, we must append the character M
(or m) to the value, like 123.45M. If we omit M, the value will be treated as
double.
Character Type:
In order to store single characters in memory, C# provides a character
data type called char. The char type assumes a size of two bytes but, in
fact it can hold only a single character. It has been designed to hold a 16-
bit Unicode character, in which the 8-bit ASCII code is a subset.
Boolean Type:
Boolean type is used when we want to test a particular condition during
the execution of the program. There are only two values that a Boolean
type can take: true or false. Remember, both these words have been
declared as keywords. Boolean type is denoted by the keyword bool and
uses only one bit of storage. In contrast to C and C++, in C#, we cannot
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 4
C# and .Net Framework Unit - 2
use zero for false and non-zero for true. No conversion between bool type
and other integer types is possible.
============== Reference Type =================
As with value types, the reference types can also be divided into two
groups: User-defined (or complex) types
Predefined (or simple) types User-defined reference types refer to those
types which we define using predefined types. They include:
Classes, Interfaces, Delegates and Arrays
These complex types will be discussed in later chapters when we take up
these topics individually. Predefined reference types include two data
types:
Object type and String type
Operators in C#: Arithmetic Operators, Prefix and Postfix
notation, Assignment Operators, Relational Operators, Other
Operators, Operators precedence.
Operator in C#:
=================== Introduction
==================
An operator is a symbol that tells the computer to perform certain
mathematical or logical manipulations. Operators are used in programs to
manipulate data and variables. They usually form a part of mathematical
or logical expressions.
C# supports a rich set of operators. C# operators can be classified
into a number of related categories as below:
1. Arithmetic operators 2. Relational
operators
3. Logical operators 4. Assignment
operators
5. Increment and decrement operators 6. Conditional
operators.
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 5
C# and .Net Framework Unit - 2
7. Bitwise operators 8. Special operators
Arithmetic Operators
Arithmetic operators are used to perform common Arithmetic (mathematical)
operations:
Operator Name Description Example
+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
// C# program to demonstrate the Arithmetic Operators
using System;
namespace Arithmetic
{
class Arithmetic_Operators
{
// Main Function
public static void Main(string[] args)
{
int result;
int x = 10, y = 5;
// Addition
result = (x + y);
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 6
C# and .Net Framework Unit - 2
Console.WriteLine("Addition Operator: " + result);
// Subtraction
result = (x - y);
Console.WriteLine("Subtraction Operator: " + result);
// Multiplication
result = (x * y);
Console.WriteLine("Multiplication Operator: "+ result);
// Division
result = (x / y);
Console.WriteLine("Division Operator: " + result);
// Modulo
result = (x % y);
Console.WriteLine("Modulo Operator: " + result);
}
}
}
OUTPUT:
Addition Operator: 15
Subtraction Operator: 5
Multiplication Operator: 50
Division Operator: 2
Modulo Operator: 0
INCREMENT AND DECREMENT OPERATORS:
Increment and Decrement operators:
The operators ++ and -- is known as Increment and Decrement operators
in C# or other language.
++x --> Preincrement
x++ --> PostIncrement
--x --> PreDecrement
x-- --> PostDecrement
Preincrement(++x): Whenever you find this statement(++x) in the
program ,then after just this statement in the program value of x will be
incremented immediately by one.
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 7
C# and .Net Framework Unit - 2
PostIncrement(x++): After just this statement(x++) in the program
value of x will not be incremented immediately by one.
PreDecrement(--x): After just this statement (--x) value of x will be
decremented immediately b y one.
PostDecrement(x--): After just this statement(x--) value of x will not be
Decremented immediately by one.
Ex.
using System;
namespace increment_and_Decrement
{
class Program
{
static void Main(string[] args)
{
int x=10,y=20;
x = ++x;
Console.WriteLine("value of x is:" + x);
x = x++;
Console.WriteLine("value of x is:" + x);
x = --x;
Console.WriteLine("value of x is:" + x);
x = x--;
Console.WriteLine("value of x is:" + x);
y = ++x + x++ + --x + x--;
Console.WriteLine("value of y is:" + y);
Console.ReadLine();
}
}
}
output:
Relational operators:
When we want to compare two or more quantities then we use relational
operators.
Relational operators are more useful to establish the relation between two
or more operands.
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 8
C# and .Net Framework Unit - 2
Ex.
Let an expression is:
x <= 20 or x > 30
If x = 10 then the expression:
x <= 20 is true.
x > 30 is false.
If x =25 then the expression:
x <= 20 is false.
x > 30 is false.
Note:- The value of Relational Expression is either true or false.Simple
relational expression
contains only one operators but complex relation expression contains
more than one operators.
There are some relational operators which are shown below table:
C# Program on Relational operators:-
using System;
namespace Relational
{
class Program
{
static void Main(string[] args)
{
int x = 40, y = 80;
float z = 15.0F;
Console.WriteLine("x<y is" + (x < y));
Console.WriteLine("x>y is" + (x > y));
Console.WriteLine("x<=y is" + (x <= y));
Console.WriteLine("x!=y is" + (x != y));
Console.WriteLine("x>=y is" + (x >= y));
Console.WriteLine("x==y is" + (x == y));
Console.WriteLine("z is" + z);
Console.ReadLine();
}
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 9
C# and .Net Framework Unit - 2
}
}
Output:-
Logical operator:
The Logical operators are used when we want to form compound conditions by
combining two or more Operators(relations).
Ex.
If (x>50 && y<10)
{
i++;
}
Note:-If both x and y value are true according to the expression the i value will be
incremented.
There are some Logical operators which are shown below table:
Truth table of Logical Operators:-
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 10
C# and .Net Framework Unit - 2
Program:
using System;
namespace logical
{
class Program
{
static void Main(string[] args)
{
int x = 60, y = 500;
int z = 1000;
if (x > 30 && y < 1000 || z >= 1000)
{ x++;
y--;
z--;
x = x-- + ++z;
Console.WriteLine("value of x is :"+x);
Console.WriteLine("value of y is :" + y);
Console.WriteLine("value of z is :" + z);
Console.ReadLine();
}
}
}
}
OUTPUT:
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 11
C# and .Net Framework Unit - 2
Conditional Operators:-
The character pair ? : is a ternary operator in C#. This operator is used to
make conditional Expression in C#.
Ex. 1
exp1 ? exp2 : exp3
Where exp1 ,exp2 and exp3 are expressions.
The operator ? : works as follow:
exp1 is evaluated first, if it is true, then exp2 is evaluated and
becomes the value of the conditional expression.
If exp1 is false then exp3 is evaluated and becomes the value of the
expression.
Ex. 2
Let x=20 and y=30
and
Expression z =x > y ? x : y ;
Output: Z = 30
Description : x>y is false so expression third ( y) is evaluated and
becomes the value of this conditional expression.
Bitwise operators:
The Bitwise operators are used to manipulation of data at bit level. These
are basically used for testing the bits or shifting the bit to right or left.
Floating point Number are not supported Bitwise Operators.
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 12
C# and .Net Framework Unit - 2
C# Program on Bitwise operators:
using System;
namespace bitwise
{
class Program
{
static void Main(string[] args)
{
byte bAnd, bOR, bXOR;
int bwright, bwleft, bwcomp;
bAnd = 13 & 12;
bOR = 17 | 3;
bXOR = 10 ^ 4;
bwright = 20 >> 5;
bwleft = 50 << 6;
bwcomp = ~ 65;
Console.WriteLine("bAnd result is:" + bAnd);
Console.WriteLine("bOR result is:" + bOR);
Console.WriteLine("bXOR result is:" + bXOR);
Console.WriteLine("bwright result is:" + bwright);
Console.WriteLine("bwleft result is:" + bwleft);
Console.WriteLine("bwcomp result is:" + bwcomp);
Console.ReadLine();
}
}
}
Output:
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 13
C# and .Net Framework Unit - 2
Conditional Operator:
The character pair?: is a ternary operator available in C#. This operator
is used to construct conditional expressions of the form
exp1 ? exp2: exp3
where exp1, exp2 and exp3 are expressions.
The operator? : works as follows: exp1 is evaluated first. If it is true, then
the expression exp2 is evaluated and becomes the value of the
conditional expression. If exp1 is false, exp3 is evaluated and its value
becomes the value of the conditional expression.
Note that only one of the expressions (either exp2 or exp3) is evaluated.
For example, consider the following statements:
a = 10;
b = 15;
x = (a>b)? a: b;
In this example, x will be assigned the value of b. This can be achieved
using the if...else statement as follows:
if(a> b)
x = a;
else
x = b;
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 14
C# and .Net Framework Unit - 2
OPERATOR PRECEDENCE AND ASSOCIATIVITY:
Each operator in C# has a precedence associated with it. This
precedence is used to determine how an expression involving more than
one operator is evaluated. There are distinct levels of precedence and an
operator may belong to one of the levels. The operators at the higher
level of precedence are evaluated first. The operators of the same
precedence are evaluated either from left to right or from right to left,
depending on the level.
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 15
C# and .Net Framework Unit - 2
Example:
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 16
C# and .Net Framework Unit - 2
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 17
C# and .Net Framework Unit - 2
Flow Control and Conditional Statements:
Prof. Raju Vathari Tungal Degree BCA and BSc College, Jamkhandi Page 18