Adobe Scan 19 Jun 2024

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

UNIT I

CHAPTER

Introducing .Net

Syllabus
Introducing .NET : The NET Framework, C#, VB, and the NET, Languages, The
Common Language Runtime, The .NET Class Library.
The C# Language: C# Language Basics, Variables and Data Types, Variable
Operations, Object-Based Manipulation, Conditional Logic, Loops, Methods.

Syllabus Topic :.Net Frame Work and C#, VB

1.1 .Net Frame Work Architecture

It is the platform created by Microsoft for developing applications.


It consists of primarily a gigantic library of code that is used in the client languages (such
as C#) using object-oriented programming (OOP) techniques.
It also helps in building and running the advanced applications. It has two major
components :
1. The common language runtime and
2. The NET framework class library

Sylabus Topic : The .NET Language and The Common Language Run Time(CLR)
1.1.1 The Common Language Run Time (CLR)
It is the foundation of the .NET Framework. It helps to manage the code during execution.
Considered as the heart of .net frame work.
It manages memory, thread execution, code execution, code safety verification.
compilation and system services.
Adv. Web Prog. (MU B.Sc. IT- Sem-V) 1-2 Introducing .Ng

It is used to climinate many common software issues and also


accelerate the developer
productivity.
Syllabus Toplc : The .Net Class Library
1.1.2 The .Net Class Library
enumerated
ltcontains a huge library of reusable types, classes, interfaces, structures, and
values, which are collectively called types.
The other components include :
Components of Common
Language RunTime

a. Just In Time (JIT) Compiler

b. Common Language Specification

c. Garbage Collector

Fig. 1.1.1

a. Just In Tme (JIT) Compiler


When code that uses NET frame work is compiled, operating-system-specific native code
language
is not immediately created. The code is compiled into common -intermediate
(CIL) code.
the job of the JITers to
This code is not specific to any OS or is not specific to C#. It is
code only once. The same
compile CIL code to native code.IL code is converted to native
can be used repeatedly.
’ b. Common Language Specification
work.
CLS helps to make.NET languages compliant to NET frame
meet to qualify as
There are a set of rules issued by Microsoft that each language should
language to
.NET Complaint.IL being a very rich language, it is not necessary for a
qualify as
implement all the IL functionality,rather it meets the small subset of it,CLS to
a .NET compliant.
C. Garbage Collector
framework for memory
This is one of the most important tools provided by .NET
management.
A We Py MVRSIT Sn V)
l fns s the itetyy usal by an ayylcat is fnt uy mletely when the
isIN ger n use.
aylnatim
rixtval insywton ot the eNY of the vmputer is kne annt remves any thutg fhvm
it chat is lnger ntt

V8NET JSorpe NET Mve NET Layuys

Canm Languaye Siatn (cLS)

Comnon Type System (CTS)

NET Framewort Ciass LDrary (FCL)

ASP.NET
Web Foms, XML Web Services Windows Foms Console

ADO.NET NET Remoting

Common Language Runtime


(Just-in-Time Compilers, Garbage Collector, Security Manager, and so on)

Gommon Language Infrastructurs (CLI)

Operating System

Fig. 1.1.2

Metadata and Assemblles

The binary information describing the program, which is cither stored in a portable
executable file (PE)or in the memory.
Assembly is a logical unit consisting of the assembly manifest, type mctadata, lL code
and a set of resources like image files.
Adv. Web Prog. (MU B.Sc. IT- Sem-V) 1-4 Introducing Net Introducing .Net
Adv. Web Prog. (MU B.Sc. IT -Sem-V) 1-5

G Windows Forms Syllabus Topic : Varlables


displayed in the
Windows forms contain the graphical representation of any window 1.2.1 Varlables
application.
These are identifiers which can change during program execution.
ADO.NET numeric variable and string
Based on the values stored in the variable we can have either
provides access to data
It is the technology used for working with data and databases. It variable.
sources like SQL server, OLE DB. XML etc.
It is always necessary to declare a variable before using it in a program.
The AD0.NET allows connection to data sources for retrieving, manipulating, and Values are assigned in a variable using = (assignment operator).
updating data. Syntax
o Windows Workflow Foundation (WF) datatype variablename;
It helps in building workflow-based applications in Windows. It contains activities, Example
workflow runtime, workflow designer, and a rules engine. int x=25:

Syllabus Topic : C# Language Basics G Key Words

Introduction to C# predefined reserved words that have special meaning to the compiler.
1.2
They cannot be used as identifiers in programs unless they are prefixed with @.
Simple, modern, object-oriented and type-safe language. Contains new features like Ex: @for can be used as an identifier.
delegate, indexers, properties etc.
lobany
It is part of a suite of products called Visual Studio .NET.
G Constants & Variables
for- cannot be used as an identifier since it is a reserved word

Syllabus Topic : Data Types

1.2.2 Data Types


Constants are values that do not change during program execution. It is divided into two
types numeric and character data type. The data type tells a C# compiler what kind of value a variable can hold.
E.g. 123,12.56,'a',"ASP C# includes many in-built data types for different kinds of data, e.g., Sring, number,
float, decimal, etc.
G How to declare a constant
G The following are the data types supported by c#
A constant is declared using the keyword constant.
Syntax NET Type Size Range (values)
type (bits)
constant data type variablename=value:
Example : Byte Unsigned integer 0 to 255

SByte Signed integer 8 128 to 127


constant int b=100:
Int32 Signed integer 32 -2,147,483,648 to 2,147, 483,647
Ulnt32 Unsigned integer 32 0 to 4294967295
Atv. Web P (MU RS IT-Sen V) 16 Introxhucing N Adv. Wab Prog (MU B. Sc. IT- Sem-V) 1-7 Introducing Not
Sle Range (values)
NET Type Syllabus Toplc : Varlable Operatlons
type (bits)
Intlo
32,768 to 32,767 1.3 Operators
Signed integer
These are special characters which perform some action on values or operands associated.
Ulntio 16 0 to 65,535
'nsigned integer
lnt64 Signnd integer 64 9,223,372.036,854,775,808 Based on the operation performed they care classified into following types:
9,223,372,036,854,775,807 Arithmetic operator Operators

Ulnto4 U'nsigned integer 64 O


to 18,446,744,073,709,55 1,61S 2. Comparison or Relational
1. Arithmetc operator
-3.402823e38 to 3.402823e38 3. Logical
Single Single precision floating point 32
2. Comparison or Relational
type 4. Assignment operator.
Double Double-precision floating point 64 -1.79769313486232e308 to 5. Conditional or ternary operator 3. Logical
type 1.797693 13486232e308 4. Assignment operator
Char Asingle Unicode character 16 Unicode symbols used in text 5. Condibonal or termary operator

Boolcan Logical boolcan type 8 True of False


Fig. 1.3.1
Object Base type of all other types
Asequence of characters
Note : Operators are also classified based on the number of operands like unary, binary or
String temary.
DecimalPrecise fractional or integral type 128 (+ or-) 1.0 x 10e-28 to 7.9 x 10e28 1, Arithmetic Operators
that can represent decimal
This operator basically used to calculate and perform action on numeric values. It is
numbers with 29 significant digits classified under the binary category as it always takes two operands.
Date Represent date and time 0:00:00am 1/1/01 to The following table shows the list of operators:
Time
11:59:59 pm 12/31/9999 Operator Description Example
Boolean Logical Boolean type 8 True or False Addition a+b

Object Base type of all other types 10+20=30

String Asequence of characters Subtraction X-y


20-5=15
Decimal Precise fractional or integral type 128 (+ or-) 1.0x 10e- 28 to 7.9 x 10e28
that can represent decimal Multiplication s*p
numbers with 29 significant digits 12*3=4
Date Represents date and time Division n/m
0:00:00 am 1/1/01 to
Time 12/3=4 (Gives the quotient)
11:59:59 pm 12/31/9999
% Modulus a%b
12%4=0(gives the remainder)
Adv. Web Prog. (MU B.Sc. IT- Som-V) 1-8 Introducing N,
Adv. Web Prog. (MU B.Sc.IT - Sem-V) 1-9 Introducing .Net
but categorized under unary operat
The following are also called as arithmetic operator
Operators Description Example
since it takes only one operand.
AND A && B
Example &&
Operator Description Returns true if both the operands are true otherwise false
X++
44 Increment the value by | OR AIIB
back
Increase the value of x by Iand stores it Returns true if any one or both the operands is true other
in x wise false

NOT !A
Decrements the value by
Returns false it operand is true or true otherwise.
decreases the value by I and stores it back in a
+ 2. Comparison or Relational Operator Note: "Unary operator
Itis used to compare values. It returns a Boolean value either True or false, this operato 4. Assignment operator
also comes under the binary opcrators category. It is classified into two types :
The following table describes the list of comparison operators: (i) Simple and Assignment
Operators Deseription Example (ü) Compound assignment operator.
Equal to A==b
Returns true if both the values are same, false otherwise. The main function of this operators is used to assign values.
(0) Simple Assignment
Not equal to A!=b
"=" isthe operator which is used to assign value to a variable.
Returns true if both the values are different, false other
wise Example
V Less than S<b
c=a+b;
(i) Compound Assignment
Returns true if the value of S is less compared o b
This operator is basically a combination of simple assignment operator and the othe
Greater than S>m
operators. We can use this operator only when the variable used in left hand side is also ther
Returns true if s>m clse false. in the right hand side.
Less than cqualto g<=y Operator Deseription Example
Returns true if g is less than or cqual to y t=,=,"=J=,%= Performs addition and a+=5 ie a=a+5
Greater than equal k>p assignment respectively. Increase the value of a
Returns true if k is bigger or equal to p otherwise false Same with other operators by 5 and stores back in a.
’ 3. Loglcal operators ’ 5. Conditlonal operator

This operator too is used with conditional statements. It returns This operator comes under the category of ternary operator, since it takes three operan
either true or false. The
lollowing are the list of operators : It is represcnted as ?: the expression is evaluated to true or false depending on
value any of the given two statements is executed.
Adv. Web Prog. (MU B.SC. IT - Sem-V) 1-10 Introducing Adv. Wetb Prog. (MU B.Sc. IT - Sem-V) 1-11 Introducing .Net

Example To
FROM
c={ a>b ?a:b): long, float, double, decimal
Int
In the abOve statement c is assigncd a if a>b otherwise b. Uint long, ulong, float, double, decimal
1.3.1 Bitwise Operator Long float, double, decimal

Allow evaluation and manipulation of specific bits within an integer. ulong float, double, decimal
Bitwise operation operates on one or more bit patterns or binary numerals at the Float double

individual bit level. Char ushort, int, uint, long, ulong. float, double, decimal
Primitive action directly supported by the processor.
& - bitwise AND
(ii) Explicit Conversion
Converting from a larger data type to smaller data type is called Explicit type conversion.
I- bitwise OR
A- bitwise exclusive OR For example converting from float to int, double to float etc. In this conversion there is a
chance of loss in data.
>>- shift ight
Explicit type conversion is done in c# by using methods defined in Convert class.
<<- shift left
Command Result
-- one's compliment
1.3.2 Type Casting Convert.ToBoolean(x) Xis converted to Boolean
It is necessary to convert data from one type to another can be achieved through type Convert.ToByte X converted to byte
casting. Convert.ToChar(x) X converted to char
There could be a possibility of loss of data when larger data types are cast onto Convert.ToDecimal(x)X converted to decimal
smaller
data types.
Convert.ToDouble(x) Xconverted to double
(i) Implicit Conversion
(ü) Explicit Conversion Convert.Tolnt16(x) X converted to short

e.g.
’ () Implicit conversions
int n=Convert. Tolnt16("12");
Conversions where there is no loss of data.
Will convert string to int.
Conversion always from smaller data types to larger data types.
Ex. :a short can be implicitly
converted to int- short is a Syllabus Toplc :0bect Based Manipulation
subset of int.
FROM To 1.4 Object Based Manipulation
Sbyte Short, int, long, float, double, decimal
Byte .NET is object-oriented to the core. Common data types have the built-in smarts to handle
Short, ushort, int, uint, long, ulong, float,
Short double, decimal basic operations (such as counting the number of characters in a string).
int, long, float, double, decimal
Ushort We can manipulate strings, dates, and numbers in the same way in C#.
int, uint, long, ulong, float,
double, decimal
’ Members
String o String 1.

specificExtract These The 2. This substring. portion
Split)- IndexOf)StartsWith) Replace)-
Remove)- Insert()specify. PadLeft) as Trim0,
(or uppercase
ToUpper()Length- The s= s s=strinExample g with Tone Adv.
DateTime The returns many both) =s. S
s.ToUpper);
above ubstring(0,
s.Trim): s strings.
presentation a
class Divides of -Inserts TrimEnd), = of Web
part DateTime times ends Returns the
members only and Replaces
and string
aRemoves and
lowercase
or and statements "Asp.Net Prog.
and LastindexOf()- PadRight(0- of // We best
of a
string the EndsWith)- another necessary as
a ToLower) the / = 3); OP uase
(MUB.SC.
a
TimeSpan of string. and
DateTime allow
(such
and first the a a number use "ASP" "ASp. //Net C# exampl e
specified OP functions
TimeSpan into match specified
specified string TrimStart()- characters.
-Returns built-in Programming IT-
as you an Adds
to of "Asp" of Sem-V)
the (for todata array and FindsDetermines inside make C#
number the
characters
methods,such as for how
total
example, perform types
Types of can the
length
substring
a specified the Removes a Programming" manipulation "; class
number string copy
also substrings start total
zero-based whether the at stringsof 1-12
just
three have
at with at length of thein members
of the specified a character spaces the
days the usefulbuilt-in delimited end another specified string
string of
a from Substring(),
ToUpper/)
Trim), can
year) position string of or strings.
or tasks: or a th e (as
total methods beginning. location specified
(zero-based)
to some with
an replace
or by starts string. string the
number a of appropriate other integer).
all
convert
and
specific or
a Substring)- (as equal th e built-in
substring ends aposition. characters characters
of properties.
a new index to
minules) substring with th e side functions
TimeSpan in
string).
a Extractsi position. number of from
Introducing
a specitia chanos
suly a
to wstri eii

Clear)- of Array
GetUpperBound)
array.Length
an GetLength) findArrays TotalMinutes, 3.
TotalDays, members
Add) Days, Time Add) Now-Givesmembers
span integer. Year,Today-Gives Tlme
date DateTime.Now;
e.AddDays(100); Determine Easily Adv.
s= e= Examplewhether
stringDateTimee=
GetLowerBound), n=int]
out Array TypeThe Web
membersmyArray.Length; myAray and Hours, Date, and perform
clears Returns
- the also Second, the Prog.
method, sizebehave TotalHours- Minutes,
Subtract) Subtract)- e.Year.ToString0;
Month, the dat e the
TotalSeconds, current
datandecurrent current date (MU
the = and
an method {1,2 , of occurs
anray B.3c.
GetUpperBound) integer
// both alike Combines Millisecond Day, Adds calculations.
ofp 3,4, of objects Seconds- indate
one-dimensional date
IT-
to Returns Hour, a
that find =5 5); which number or and leap and Sem-V)
subtracts sets
represents in t in TimeSpan Returns Minute- time
year). time
the tim e
retun
n; the of the and
-and highest
new days, a to
thearray, totalobjects one Returns
TimeSpan00:00:00 other 1-13
Determines the world
index total component
hours,
total you value information
number number of together. one
number can NET. minutes, from
the of part
use of
dimensions
of in of the the of the (such
an clements the For and DateTime.
the
elements array. example, current current
Length so DateTime as
on. the
of in TimeSpan.
an in property
an TimeSpan day
array. all if
aray you object of Introducing
.Net
dimensions the
theorwant as as weck
to a an
or
Adv. Web Prog. (MU B.Sc. IT- Sem-V)

Sort()- sorts the array


1-14
Introducing
Reverse()- reverse the array.
Syllabus Toplc : Conditional Loglc and Loops
1.5 Control Flow Statement

Generally a program is executed sequentially one after another but there might L
situations where the statements need to be executed only based on some condition.
Java script supports two types of statements : Control Flow Statement
1. Selection statements.
2. Looping statements. 1. Selection statements

a. if statement
’ 1. Selection statements
b. if-else statement
The statements are executed only if the conditionis
satisfied. We have three types of selection c. if-elseif statement

statements :
d. switch case
(a) if statement
2. Loopingg statements
(b) if-else statement
a. while
(c) if-elseif statement.
(d) switch case. b. do -while

’ (a) If statement c. for


This is also called as conditional statement. The
d. for each
flowchart below depicts the working of the
statement. Fig. 1.5.1
Yes
statements
nol

Fig. 1.5.2

The statements are executed only when the condition is true.


Adv. Web Prog. (MU B,Se. IT - Sem-V) 1-15 Introducing Net
Syntax
illoondn)

Statement,

Console Write("aitive"
+ (b) I-clse statenent
This statenent is similar to if statement but the only differene is different set of
Ntatenments are ereouted based on the condition whether it is true or false.
The lowchart clescribes its working :

Ya
ondltlon Statement
|No
Satement

8yntax
expreaion)

Satenenta)
elae

Satementa
Statement-3;
executed. statements
bewil
case the omitted
al statement
if , case switch the fromterminate statement
wil break elseif(expresion3)
statement(s): default:
} Statement-2;}
break; (expression2) elseif
statement(s); condition
n: case

Statement-l;
break;
iflexpressionl){
statement(s); condition
2: case
break; Syntax
statement(s); condition
l: case 1.5.4 Fig.
End)
(expression) switch Statement
Syntax No
used. be
condition
wil default matches,a nothing found.
If Statement Condition
match
is expression
untail the of
value thagainst
e case each checksinterpreter The
expression. the value
of the on
based execute No
atements
to different several andevaluate expression
to an
give statement
to is Switch Condition
Statement
case switch (d) ’ Yes
>"): Write("cis Response. below The working. tdepicts
he figure
used, multiple check tohave When
we
else statement
becan ifconditions
if-else
>"); Write("bis Response. statement if-elseif (c) ’
(b>a)&&(b>c)) Elseif
Wite("Negative"; Response.
>"); Write("a
is Response.
&&{a>c)) (a>b) if
else
Example r
rie"Psitive": Response.
Statement-4;

Introducing
.Net Sem-V) (MUB.Sc.
-IT Prog. Web Adv.
else \ntoducing Example s

1-17 Acv.
(MU Prog. Web
Sem-V) IT- B.Sc.
1-16
1-20
B.SC. IT- Sem-V)
Adv.Web Prog. (MU
Aow chart
\Introdudry Adv. Web Prog. (MU B.Sc. IT -Sem-V) 1-21
Introducing .Net

Initialization Flow chart

No
statements <condition
Yes Yes
no staternents
condition
End)
(End
Fig. 1.5.6
Fig. 1.5.7
o Syntax
Syntax
initislization;
lor(initialization; condition; increment/deerement)

ateme
statements;

hile(condiion);
Example
Exmple
for(i=1;i<=10;i++)
i=l;

Response. Write(i);

Response. ite): (d) for each


it+:
The for each statement is used to loop through arrays. Each value from the array element
hlei<=10; is assigned to Svalue and the array pointer is moved by one and the next element will be
’ (c) for processed.
One of the simplest loop as it is easy to Syntax
or
decrement is defined on the same implement. incremed

statement. Initialization,
condition and
foreach (array as value)
code to be executed;

Example
int[ arrl= {1,2, 3,4, 5}:
foreach (int x in arrl)
.Net
Introducing at syntax another typereturn be and the no required
has cannot
datathe pass of contain
program The
number
from to the
the then It may
structure. sensitive.usedand complete
Method method is values,
C# type method
Every are order,
its return to
ato a any case parameters
C# of or type,a needed
task. in Parameters 1.6.1
Fig. elements variable The returningis
value. is,
Methods the
aperform Methods List) it
Methods Steps
of and that instructions
Nane>(Parameter
<Method the to
DefiningPassingDelegates
3. the a not identifier refers
optional;
of areturn parentheses,
together declare is class.
:Topic visibility method list
1. 2.
1-23
follows: not the
unique parameter
are of
Main. basically set
that in
Syllabus the may the betweenParameters
declared the
IT-Sem-V) statements
named If a
determines or is
returns. name The contains
you as may
method
:to C# is Type>
Specifier>
<Return identifier
need in method,C#
method
method.
Enclosed method.
of Methods in method Method
B.Sc. method
Define
the method It This
groupawith we Call
the
method adefine Specifier: a
(MU Methods method, A : other :list from :
ais class Defining a :typethe name body
ofparameters
Prog. void.
is
type any data
method you defining value Parameter parameters.
activity.
Method
Web one a Method
Body Access Return Method
as receive
use When class. the same
Adv. least To for <Access of
A 1.
1.6 ’
Inttoduc
value
access define 0is elements
which
to to
isused value value, assigned. its
access
variahle
which
is constant
default
is to
type value indices
a a
array location.
memory
consecutive and assigned time
multiple
singleindex array, same
being commas:
ain an
with the the }; uses declaration.
1-22 initialize members at 99 that
stored and 10,2,
associated0. as
one declared more double[3,4|;
from declared
Sem-V) variables array (5,9, simply the
to 1.5.1.1
Mutidimensional
Arrays <baseType>L
<name>;
require
default int[5|;used
in
the array
be <baseType>|<name>;
IT- in <haseType>]<name>; is
array myIntAray is can
of array by value new explicitly all array dimensions new
BSC. niW
Response.
te(i}; lists in
the
=
starts below array of stdetails
(MU indexedthe variable. the mylntArray results multidimensional
in member int[] size
value store is two-dimensional
Proo Arrays clement keyword mehod numernc
types. example more the double[,]
Web are indexto of define
AravsEach belps oSyntax int] the Arrays
Adv The Eg, new size. This E.g. We
1.5.1 It In A
lntrexaing Net
AN Wahy (MU RSIT Sem
V)

Example
MIble int Num(int,in)

And st
’ 1 elegates
indireet calls to methods.
Dlegates are reterene vpes which allow by invoking
references to some number of methods, and
A delegate instance holds
methods to be called.
the delegate one causes all of these invoke them are
the fact that the functions which
The usefulness of delegates lies in
blind to the underlying methods.
function pointers.
functionally rather sinmilar to C++s
’ 2 Passing Parzmeters to a Method It can be scen that dclegates are differences.
two main
Whe metod i parzmes is callei. you need to pass the parameters to the However, it is important to bear in mind
than value types.
There re tre wzvs that perzeters can be passed to a metho:
method Fistly,delegates are reference types rather
methods.
Sr. Secondly, some single delegates can reference multiple
Mechanism &Description public delegate void Pointfunc
(String s):
contains the following method:
Value parameters Suppose, for instance, that a class
1.
The values re copied locally inside the function so any changes made inside the blie void deleMethod (String myng
function is not available outside.
I method code
Reference parameters
2 Sine the reference of the arguments is passed any changes
made inside is available instantiate the Print delegate in the following
OutSide too. Another method in this class could then
realMethod:
Output parameters way, so that it holds a reference to
3 SiCe tunction can only return one value and in a situation where we need to retur Print delerateVariable = new Pointunc(delemes
foore th2r one value this pararneter is used.
can simultaneously reference multiple
some delegates - termed multicast delegates
-specify a void return type.
methods. These delegates must - like our Pointfunc delegate
T Methods Over Loading
running of the method is the handling
C# supprts overloading We have the sarne furction name but The method invocation is termed an event, and the
perforning different user's selection of a button on a graphical
operations. of the event. An typical example of an event is a
of methods to handle it.
We can use sarne Tiarne to get different outputs. For exarnple surn) to add two intenet user inteface; this action may trigger a number
delegate.
rumber, decimal nurnber and concatenate two string. The event keyword is used to declare a particular multicast
Adv. Web Prog. (MU B.Sc. IT -Sem-V) 1-26
Introducing
else there js a
Multicast delegates must contain only methods that return void, run-i
exception
Each delegate has an invocation list
Methods are invoked sequentially, in the order added
respectively
The += and -= operators are used to add and remove delegates,
+= and -= operators are
thrcad-safe
delegate void mainEvent(int x, int y):
static void Funcl(int x, int y) {
Response. Write("lnfunction l"):

static void Func2(int x, int y) {


Response. Write("ln function 2");
}
protected void Buttonl Click(object sender, EventArgs e)
{mainEvent func = new Event(Funcl);
fune += new mainEvent(Func2);
|/ Funcl& Func2 are called
func(1,2);
func -= new SomeEvent(Funcl);

func(2,3); I/Only Func2 is called

Review Questions

Q. 1 Discuss on .net frame work architecture

Q.2 Write a note on type casting. Give examples


Q.3 What is a delegate, explain with example.
Q. 4 Write on looping statements, explain any one with example.
Q.5 Write a note on control flow statements.

You might also like