You are on page 1of 48

Basics of .NET [BSIT-61] Chapter 1 1. What is .Net Framework? Ans: .Net is framework developed by icrosoft. It comprises wit!

t! programming methodologies, platform technologies, and code execution environment. "ll t!ese t!i#$s combi#ed to$et!er i# a framework% w!ic! is &sed to provide ob'ect-orie#ted developme#t platform. It is a# e#viro#me#t for t!e developers. 2. What does .Net Framework comprised off? Ans: .Net (ramework comprised of followi#$ t!i#$s) i. Programming Methodologies) - *# pro$rammi#$ met!odolo$y side% a# i#teresti#$ t!i#$ abo&t .NET is t!at it allows mi+ed-pro$rammi#$ la#$&a$e for t!e developer. T!e desi$#ers of .NET were able to b&ild a system w!ere t!e pro$rammer ca# freely mi+ mod&les of t!e pro$ram betwee# o#e la#$&a$e a#d a#ot!er. By desi$#i#$ from t!e $ro&#d level% a specificatio# t!at is commo# to all la#$&a$es t!at works i# .NET framework% desi$# a type system t!at all la#$&a$es s&pport% a#d specifically desi$# a# i#termediate la#$&a$e t!at all la#$&a$es compile i#to, before t!ey compile i#to #ative code. ii. Platform Technologies) - -latform tec!#olo$ies% are mai#ly ADO .Net, Internet technologies and User Interface designing. ".* .Net is t!e tec!#olo$y t!at is &sed to access t!e databases. .Net !as improved t!at ki#d of pro$rammi#$ dramatically from a# ease-of-&se sta#dpoi#t% t!ro&$! some feat&res. T!e #et effect of ".* .Net for t!e developer !as $reatly simplified a#d e#!a#ced capability of pro$rammi#$ wit! databases. I#ter#et was t!e certai#ly o#e of t!e keysto#e of t!e developme#t of .Net% as we ca# &#dersta#d from t!e #ame. T!e partic&lar part of t!e tec!#olo$y% w!ic! deals wit! I#ter#et is called "S- .Net. T!e idea for developi#$ software for t!e I#ter#et ca# be tricky. It become more tricky% if we are tryi#$ to !ave reaso#able amo&#t of pro$rammi#$ lo$ic sitti#$ be!i#d a web pa$e. "#ot!er place w!ere .Net !as i##ovated is i# /0ser I#terface .esi$#i#$1. It was started i# t!e world of 2is&al Basic. I# 344% we str&$$le wit! !acki#$ code to positio# o&r b&tto#s a#d o&r te+t bo+es% w!ere as t!e 2is&al basic &ser were '&st dra$$i#$ t!em o# t!e scree# a#d p&tti#$ t!em w!ere t!ey wa#ted t!em. T!at1s t!e bea&ty of t!e tools t!at icrosoft !as b&ilt% t!at work wit! t!ose la#$&a$es. T!e 5i#dow pro$rams r&# &#der .Net% w!ic! are desi$#ed vis&ally% are called 65i#(orms7. iii. Code Execution) - T!e i#teresti#$ t!i#$ abo&t code e+ec&tio# o# .NET framework is% t!e sta#dardi8ed commo# la#$&a$e i#frastr&ct&re is completely arc!itect&re-#e&tral. T!e w!ole p&rpose of maki#$ it sta#dard a#d p&tti#$ it i# t!e p&blic domai# was so t!at i#frastr&ct&re co&ld be impleme#ted o# a#y platform. T!e w!ole idea is t!at we are #e&tral i# terms of t!e arc!itect&re. "#ot!er i#teresti#$ idea i# .NET% is t!e #otio# of '&st-i#-time compili#$. I# t!e .NET platform% all t!e .NET la#$&a$es $et compiled i#to t!e icrosoft I#termediate la#$&a$e or simply called as I9. T!is i#termediate la#$&a$e $ets compiled by '&st i# time compiler% to t!e #ative mac!i#e code. 3. Write Short Notes on a. Programming Methodologies) - *# pro$rammi#$ met!odolo$y side% a# i#teresti#$ t!i#$ abo&t .Net is t!at it allows mi+ed-pro$rammi#$ la#$&a$e for t!e developer. 1

Basics of .NET [BSIT-61] T!e desi$#ers of .Net were able to b&ild a system w!ere t!e pro$rammer ca# freely mi+ mod&les of t!e pro$ram betwee# o#e la#$&a$e a#d a#ot!er. By desi$#i#$ from t!e $ro&#d level% a specificatio# t!at is commo# to all la#$&a$es t!at works i# .Net framework% desi$# a type system t!at all la#$&a$es s&pport% a#d specifically desi$# a# i#termediate la#$&a$e t!at all la#$&a$es compile i#to, before t!ey compile i#to #ative code. b. Platform Technologies) - -latform tec!#olo$ies% are mai#ly : ".* .Net% I#ter#et tec!#olo$ies a#d 0ser I#terface desi$#i#$. ".* .Net is t!e tec!#olo$y t!at is &sed to access t!e databases. .Net !as improved t!at ki#d of pro$rammi#$ dramatically from a# ease-of-&se sta#dpoi#t% t!ro&$! some feat&res. T!e #et effect of ".* .Net for t!e developer !as $reatly simplified a#d e#!a#ced capability of pro$rammi#$ wit! databases. I#ter#et was t!e certai#ly o#e of t!e keysto#e of t!e developme#t of .Net% as we ca# &#dersta#d from t!e #ame. T!e partic&lar part of t!e tec!#olo$y% w!ic! deals wit! I#ter#et% is called "S- .Net. T!e idea for developi#$ software for t!e I#ter#et ca# be tricky. It becomes trickier% if we are tryi#$ to !ave reaso#able amo&#t of pro$rammi#$ lo$ic sitti#$ be!i#d a web pa$e. "#ot!er place w!ere .Net !as i##ovated is i# /0ser I#terface .esi$#i#$1. It was started i# t!e world of 2is&al Basic. I# 344% we str&$$le wit! !acki#$ code to positio# o&r b&tto#s a#d o&r te+t bo+es% w!ere as t!e 2is&al basic &ser were '&st dra$$i#$ t!em o# t!e scree# a#d p&tti#$ t!em w!ere t!ey wa#ted t!em. T!at1s t!e bea&ty of t!e tools t!at icrosoft !as b&ilt% t!at work wit! t!ose la#$&a$es. T!e 5i#dow pro$rams r&# &#der .Net% w!ic! are desi$#ed vis&ally% are called 65i#(orms7. c. Code Execution) - T!e i#teresti#$ t!i#$ abo&t code e+ec&tio# o# .NET framework is% t!e sta#dardi8ed commo# la#$&a$e i#frastr&ct&re is completely arc!itect&re-#e&tral. T!e w!ole p&rpose of maki#$ it sta#dard a#d p&tti#$ it i# t!e p&blic domai# was so t!at i#frastr&ct&re co&ld be impleme#ted o# a#y platform. T!e w!ole idea is t!at we are #e&tral i# terms of t!e arc!itect&re. "#ot!er i#teresti#$ idea i# .NET% is t!e #otio# of '&st-i#-time compili#$. I# t!e .NET platform% all t!e .NET la#$&a$es $et compiled i#to t!e icrosoft I#termediate la#$&a$e or simply called as I9. T!is i#termediate la#$&a$e $ets compiled by '&st i# time compiler% to t!e #ative mac!i#e code. 4. Which are the platform technologies s pported !" .Net framework? Ans: -latform tec!#olo$ies s&pported by .Net framework are : ".* .Net% I#ter#et tec!#olo$ies a#d 0ser I#terface desi$#i#$. ".* .Net is t!e tec!#olo$y t!at is &sed to access t!e databases. .Net !as improved t!at ki#d of pro$rammi#$ dramatically from a# ease-of-&se sta#dpoi#t% t!ro&$! some feat&res. T!e #et effect of ".* .Net for t!e developer !as $reatly simplified a#d e#!a#ced capability of pro$rammi#$ wit! databases. I#ter#et was t!e certai#ly o#e of t!e keysto#e of t!e developme#t of .Net% as we ca# &#dersta#d from t!e #ame. T!e partic&lar part of t!e tec!#olo$y% w!ic! deals wit! I#ter#et% is called "S- .Net. T!e idea for developi#$ software for t!e I#ter#et ca# be tricky. It become more tricky% if we are tryi#$ to !ave reaso#able amo&#t of pro$rammi#$ lo$ic sitti#$ be!i#d a web pa$e. "#ot!er place w!ere .Net !as i##ovated is i# /0ser I#terface .esi$#i#$1. It was started i# t!e world of 2is&al Basic. I# 344% we str&$$le wit! !acki#$ code to positio# o&r b&tto#s a#d o&r te+t bo+es% w!ere as t!e 2is&al basic &ser were '&st dra$$i#$ t!em o# t!e scree# a#d p&tti#$ t!em w!ere t!ey wa#ted t!em. T!at1s t!e bea&ty of t!e tools t!at icrosoft !as b&ilt% t!at work wit! t!ose la#$&a$es. T!e 5i#dow pro$rams r&# &#der .Net% w!ic! are desi$#ed vis&ally% are called 65i#(orms7. ;

Basics of .NET [BSIT-61] .NET s&pports a co#cept called data bi#di#$ t!at lets t!at !appe# completely a&tomatically. T!e data so&rce for t!is $rid is t!is partic&lar dataset% a#d t!at is t!e e#d of it. If we c!a#$e t!e dataset% t!e $rid &pdates a&tomatically. If we c!a#$e t!e $rid pro$rammatically% it &pdates i#to t!e dataset% a#d t!e# we ca# decide w!et!er to commit t!ose c!a#$es to t!e database. It is a very #ice a#d very powerf&l co#cept. #. $ist o t the ad%antages of .Net Framework? Ans) T!e .NET (ramework provides #&mber of adva#ta$es to t!e developers% w!ic! are me#tio#ed below : i. Consistent Programming Model) - Every pro$rammi#$ la#$&a$e !ave differe#t approac!es for doi#$ a task. (or e+ample% accessi#$ data wit! a 2B applicatio# a#d a 2344 applicatio# is totally differe#t. " developer !as to k#ow bot! differe#t tec!#i<&es to do t!e task. T!e .NET 3lass 9ibrary provides t!e f&#ctio#ality to all .NET la#$&a$es res&lti#$ i# a co#siste#t ob'ect model re$ardless of t!e pro$rammi#$ la#$&a$e &sed by t!e developer. ii. Language Independence) - 9a#$&a$e I#depe#de#ce mea#s t!e developer !as t!e i#depe#de#ce to code o#e mod&le of a# applicatio# i# o#e la#$&a$e a#d ot!er mod&le of same applicatio# i# ot!er la#$&a$e. T!is la#$&a$e i#teroperability ca# !elp i# code re&se a#d improve t!e efficie#cy of t!e developme#t process. iii. No ersioning Problem) - It makes deployme#t of applicatio#s very easy. T!e .NET e#s&res t!at all t!e compo#e#ts t!e applicatio# depe#ds o# are available o# t!e comp&ter before t!e applicatio# be$i#s to r&#. iv. Impro ed !ecurit") - T!e .NET (ramework provides several mec!a#isms for protecti#$ reso&rces a#d code from &#a&t!ori8ed &sers) a.= "S-.NET 5eb "pplicatio# Sec&rity provides a way to co#trol access to a web site by compari#$ a&t!e#ticated permissio#s wit! (ile System permissio#s for proper a&t!ori8atio#. b.= 3ode access sec&rity &ses permissio#s to co#trol t!e code accessi#$ protected reso&rces a#d operatio#s. c.= >ole-based sec&rity provides i#formatio# #eeded to make decisio#s abo&t &ser1s permissio#s. v. !upport for #eb !er ices) - 5it! t!e adve#t of .NET tec!#olo$y% web services provide ma#y b&ilt i# Base 3lass library facilities% w!ic! ope# &p a w!ole #ew world of i#formatio# for &sers. 5eb services ca# provide everyt!i#$ from basic te+t #ews i#formatio# to vital database or applicatio# i#formatio#. vi. $"namic #eb) - .y#amic web co#te#t is easier to create wit! .NET. "S-.NET a#d 2is&al St&dio.NET allow developers to create dy#amic web m&c! easier t!a# IIS a#d "S- tec!#olo$ies. vii. %isual !tudio .NET) -2is&al St&dio .NET is t!e icrosoft applicatio# a#d i#terface for pro$rammi#$ i# .NET. 2is&al St&dio. T!is developme#t e#viro#me#t !as e+te#sive set of ready to &se co#trols% $ood deb&$$i#$ facilities% easy a#d simple pro$rammi#$ feat&res a#d vast set of .NET Base 3lasses. &. What is !ackward compati!ilit"? '(ackward compati!ilit" self)st d" topic* Ans) Backward compatibility is t!e special case of compatibility i# w!ic! t!e #ew server?applicatio# !as a direct a#cestral relatio#s!ip wit! t!e old server?applicatio#. I# tec!#olo$y% irrespective of platform% a prod&ct is said to be backward compatible w!e# it is able to take t!e place of a# older prod&ct% by i#teroperati#$ wit! ot!er prod&cts t!at were desi$#ed for t!e older @

Basics of .NET [BSIT-61] prod&ct. .NET is a fres! start w!ic!% s&pports backward compatibility. It is capable of r&##i#$ applicatio#s developed i# 2is&al St&dio o# 2is&al St&dio .Net. (or E+ample - icrosoft is well k#ow# for its operati#$ system for !ome -3. (rom 5i#dows @.1 developed i# 1AA; to today1s 5i#dows B-% icrosoft !as made ma#y adva#ces i# its tec!#olo$ies. 5it! t!e developme#t towards #ew tec!#olo$y it !as to mai#tai# backward compatibility wit! t!e e+isti#$ tec!#olo$ies. Backward compatibility is t!e stre#$t! of t!e 5i#dows +. ,oes .N-. platform s pport !ackward compati!ilit"? -/plain. Ans) .Net platform s&pport backward compatibility. It is a fres! start. It is capable of r&##i#$ applicatio#s developed i# 2is&al St&dio o# 2is&al St&dio .Net. T!e addable co#trols a#d 2B.Net !as $ood deb&$$i#$ feat&res. .NET is a #ew framework developed wit! #ew set of "-I. 3C is a #ew la#$&a$e desi$#ed from scratc! wit! .NET a#d for .NET platform. .NET platform s&pports ob'ect-orie#ted pro$rammi#$. T!e .NET framework !as 2is&al St&dio .Net% w!ic! is t!e &p $radatio# of 2is&al St&dio. I# 2is&al St&dio 2is&al Basic D2B= was very well k#ow# for its simplicity a#d lar$e #&mber of co#trols b&t did #ot !ave $ood deb&$$i#$ feat&re. 5!ereas t!e 2344 !ad well b&ilt deb&$$i#$ feat&re% b&t did #ot !ad t!e simplicity a#d vast co#trols as i# 2B. T!e 2is&al St&dio.Net !as overcome all t!ese facts. Now 2344.Net !as easily I#trod&ctio# addable co#trols a#d 2B.Net !as $ood deb&$$i#$ feat&res.

Basics of .NET [BSIT-61]

Chapter 2 1. With a neat diagram0 e/plain the .N-. Framework architect re. Ans:

T!e .NET (ramework is partitio#ed i#to mod&les% eac! wit! its ow# disti#ct respo#sibility. T!e !i$!er tiers re<&est services o#ly from t!e lower tiers% so .NET is !ierarc!al. T!e modified arc!itect&ral layo&t of t!e .NET (ramework is followi#$) F T!e .NET (ramework is a ma#a$ed e#viro#me#t. F T!e 39> mo#itors t!e e+ec&tio# of .NET applicatio#s a#d provides re<&ired services. It ma#a$es memory% !a#dles e+ceptio#s% e#s&res sec&rity% a#d m&c! more. 9a#$&a$e i#teroperability is o#e $oal of .NET. F .NET la#$&a$es s!are a 3ommo# 9a#$&a$e Specificatio# D39S=% 3ommo# Type System D3TS= a#d (ramework Base 3lass 9ibrary D(39=. F 5eb Services% ".*.NET% 5i#dows or co#sole applicatio#s a#d >emoti#$ e+ec&te o# 39>. I# .NET% t!e pro$rammi#$ la#$&a$e like 3C% 2B.NET% or GScript.NET !as similar e+perie#ce e+cept little differe#ces. F .NET abstracts lower-level services% w!ile retai#i#$ most of t!eir fle+ibility. "ssemblies% "pplicatio# domai#s a#d metadata are bro&$!t to 39> e#viro#me#t before t!ey are e+ec&ted. *#ce t!ey are bro&$!t to t!e e+ec&tio# e#viro#me#t% 39> takes t!e respo#sibility of ma#a$i#$ a#d e+ec&ti#$ it. 2. $ist o t the o!1ecti%es of .N-. framework? Ans: T!e ob'ectives of .NET framework are as follows : To provide a# ob'ect-orie#ted pro$rammi#$ e#viro#me#t w!et!er ob'ect codes is stored a#d e+ec&ted locally% or e+ec&ted remotely. To provide a code-e+ec&tio# e#viro#me#t t!at mi#imi8es software deployme#t a#d versio#i#$ co#flicts. To provide a code-e+ec&tio# e#viro#me#t t!at $&ara#tees safe e+ec&tio# of code% i#cl&di#$ code created H

Basics of .NET [BSIT-61] by a# &#k#ow# or semi-tr&sted t!ird party. To provide a code-e+ec&tio# e#viro#me#t t!at elimi#ates t!e performa#ce problems of scripted or i#terpreted e#viro#me#ts. 3. 2ow the Windows 3rogramming is different from .N-. 3rogramming? Ans: 5i#dows -ro$rammi#$ is differe#t from .NET -ro$rammi#$ i# followi#$ ways : #indo&s Programming) - I# 5i#dows -ro$rammi#$% t!e applicatio# pro$rams call wi#dows "-I f&#ctio# directly. T!e applicatio#s r&# i# t!e wi#dows e#viro#me#t i.e. operati#$ system itself. T!ese types of applicatio#s are called &#ma#a$ed or &#safe applicatio#s. .NET Programming) - I# .NET -ro$rammi#$ t!e applicatio# pro$rams call .Net Base 3lass library f&#ctio#s w!ic! will comm&#icate wit! operati#$ system. T!e applicatio#s r&# i# .Net >&#time e#viro#me#t. T!ese types of applicatio#s are called as ma#a$ed or safe applicatio#s. T!e .Net Base classes are f&lly ob'ect-orie#ted. It provides all f&#ctio#alities of traditio#al wi#dows "-I alo#$ wit! f&#ctio#alities i# #ew areas like accessi#$ database% I#ter#et co##ectio#s a#d web services. 4. ,efine the following ) "#s) a. CL' ( 3ommo# 9a#$&a$e >&#time D39>= is also k#ow# as .NET >&#time. T!e code ma#a$eme#t is a f&#dame#tal pri#ciple of t!e r&#time. T!e respo#sibility of r&#time is to load t!e code% r&# t!e code a#d provide all ki#ds of services. T!e services provided by .NET >&#time are as memory ma#a$eme#t% t!read ma#a$eme#t% remoti#$% strict type safety% e#s&re code sec&rity a#d rob&st#ess. b. Managed Code - a#a$ed code is o#e% w!ic! is desi$#ed to r&# o# .Net e#viro#me#t. T!e code w!ic! r&#s i# wi#dows e#viro#me#t is &#ma#a$ed code. c. Intermediate Language )IL* - T!is is also called as icrosoft I#termediate 9a#$&a$e D SI9=. T!e i#termediate la#$&a$e is a#alo$o&s to t!e byte code of Gava. T!e si$#ifica#t differe#ce is t!at t!e byte code i# i#terpreted w!ile t!e I9 is compiled by GIT compilatio# to #ative mac!i#e code. T!e I9 code is #ot t!e mac!i#e code. T!e .Net >&#time calls t!e G&st-i#-Time DGIT= compiler to $et #ative mac!i#e code from I9 code. T!is feat&re makes t!e .NET platform i#depe#de#t. d. Common T"pe !"stem )CT!* - T!is is a sta#dardi8ed a$reed set of basic data types. T!is system provides a way for la#$&a$e i#teroperability. 9a#$&a$e I#teroperability mea#s t!at a ob'ect impleme#ted i# o#e la#$&a$e ca# call a ob'ect impleme#ted i# a#ot!er la#$&a$e. I# more $e#eral way yo& ca# say t!at a applicatio# ca# be developed i# two or more la#$&a$es s&pported by .Net framework. To make &se of la#$&a$e i#teroperability feat&re% t!e developers !ave to follow 3TS. e. .NET +ase Classes - .NET Base 3lasses is a library comprisi#$ of e+te#sive set of f&#ctio#s for all services s&pported by t!e framework. T!is vast collectio# !as well writte# code for almost all tasks o# all services of .Net framework. T!e services ca# be wi#dows creatio#?displayi#$ or form !a#dli#$% I#ter#et or web services% database accessi#$ or file !a#dli#$. f. Assemblies - "# "ssembly is t!e &#it i# w!ic! compiled ma#a$ed code is stored. "# "ssembly co#tai#s I9 a#d metadata. etadata $ives details of t!e assembly% properties a#d met!ods stored i# it% sec&rity i#formatio# etc.

Basics of .NET [BSIT-61] g. Common Language !pecification )CL!* - T!e set of r&les of basic la#$&a$e feat&res !as bee# defi#ed to e#!a#ce a#d e#s&re la#$&a$e i#teroperability is called t!e 3ommo# 9a#$&a$e Specificatio# D39S=. T!e 39S r&les defi#e a s&bset of t!e 3TS. h. 'eflection - T!e process of obtai#i#$ i#formatio# abo&t assemblies a#d t!e types defi#ed wit!i# t!em as metadata at r&#time. i. ,ust(in(Time ),IT* Compilation - T!e compilatio# co#verts I9 i#to its #ative mac!i#e code. T!e #ame G&st-i#-Time is beca&se it compiles portio# of code as a#d w!e# re<&ired at r&# time. -. Manifest - T!e area of assembly w!ere metadata is stored is called as ma#ifest. .. Application $omain - " bo&#dary t!at t!e commo# la#$&a$e r&#time establis!es aro&#d ob'ects created wit!i# t!e same applicatio# is by mea#s of "pplicatio# .omai#. "pplicatio# domai# allows differe#t code to r&# i# t!e same process space. l. /arbage Collection - T!e Iarba$e 3ollector frees t!e applicatio# from t!e respo#sibility of freei#$ memory w!e# #o lo#$er re<&ired. 39> calls Iarba$e collector to !a#dle memory efficie#tly. #. What are f nctionalities of C$4? 5i%e the steps in%ol%ed in managed e/ec tion process. Ans: The basic functionalities of !" - To e#able t!e r&#time to provide services to ma#a$ed code, la#$&a$e compilers m&st emit metadata t!at describes t!e types% members% a#d refere#ces i# o&r code. etadata is stored wit! t!e compiled code. Every loadable file co#tai#s metadata called as portable e+ec&table D-E=. T!e r&#time &ses metadata to locate a#d load classes% lay o&t i#sta#ces i# memory% resolve met!od i#vocatio#s% $e#erate #ative code% e#force sec&rity% a#d set r&#-time co#te+t bo&#daries. T!e r&#time a&tomatically !a#dles ob'ect layo&t a#d ma#a$es refere#ces to ob'ects% releasi#$ t!em w!e# t!ey are #o lo#$er bei#$ &sed. "&tomatic memory ma#a$eme#t elimi#ates memory leaks as well as some ot!er commo# pro$rammi#$ errors. T!e 39> makes it easy to desi$# compo#e#ts a#d applicatio#s w!ose ob'ects i#teract across la#$&a$es. *b'ects writte# i# differe#t la#$&a$es ca# comm&#icate wit! eac! ot!er% a#d t!eir be!aviors ca# be ti$!tly i#te$rated. T!is cross-la#$&a$e i#te$ratio# or la#$&a$e i#teroperability is possible beca&se la#$&a$e compilers a#d tools t!at tar$et t!e r&#time &se 3TS defi#ed by t!e r&#time% a#d t!ey follow t!e r&#time1s r&les for defi#i#$ #ew types% as well as for creati#$% &si#$% persisti#$% a#d bi#di#$ to types. T!e r&#time &ses metadata i#formatio# to e#s&re t!at yo&r compo#e#t or applicatio# !as t!e specified versio#s of everyt!i#$ it #eeds% w!ic! makes yo&r code less likely to break d&e to some depe#de#cy. >e$istratio# i#formatio# a#d state data are #o lo#$er stored i# t!e re$istry w!ere t!ey ca# be diffic&lt to establis! a#d mai#tai#. >at!er% i#formatio# abo&t t!e types a#d t!eir depe#de#cies is stored wit! t!e code as etadata. .he managed e/ec tion process incl des the following steps6 3!oosi#$ a# appropriate la#$&a$e compiler. 3ompili#$ code to icrosoft i#termediate la#$&a$e D SI9 or simply I9=. 3ompili#$ tra#slates so&rce code i#to I9 a#d $e#erates t!e re<&ired metadata. 3ompili#$ I9 to #ative code. "t e+ec&tio# time% a '&st-i#-time DGIT= compiler tra#slates t!e I9 i#to #ative mac!i#e code. .&ri#$ t!is compilatio#% code m&st pass a verificatio# process t!at e+ami#es t!e I9 a#d J

Basics of .NET [BSIT-61] metadata to fi#d o&t w!et!er t!e code ca# be determi#ed to be type safe. E+ec&ti#$ code. T!e 39> provides t!e e#viro#me#t t!at e#ables e+ec&tio# to take place as well as a variety of services t!at ca# be &sed d&ri#$ e+ec&tio#. &. What are assem!lies? What are static and d"namic assem!lies? Ans) "# "ssembly is t!e &#it i# w!ic! compiled ma#a$ed code is stored. "# "ssembly co#tai#s I9 a#d metadata. etadata $ives details of t!e assembly% properties a#d met!ods stored i# it% sec&rity i#formatio# etc. "ssemblies ca# be static or dy#amic. Static assemblies ca# i#cl&de .NET types Di#terfaces a#d classes=% as well as re<&ired reso&rces for t!e assembly Dbitmaps% G-EI files% reso&rce files% a#d so o#=. Static assemblies are stored o# disk i# -E files. .y#amic assemblies are o#e% w!ic! r&# directly from memory a#d are #ot saved to disk before e+ec&tio#. T!ey ca# be saved disk after t!ey !ave e+ec&ted. +. 2ow the .N-. e/ec tion en%ironment with application domain differs from traditional wa" of e/ec tion? Ans: .NET execution en ironment differs from traditional &a" of execution in follo&ing &a"s: ( 0 If we are writi#$ applicatio# i# 3C% we #eed a 3C compiler. If we are writi#$ i# 2is&al basic% we #eed a 2is&al Basic compiler% eve# for 344% we #eed a 344 compiler. B&t i# .NET e+ec&tio# e#viro#me#t% t!e approac! take# !ere is t!at all of t!ose compilers compile i#to t!is same i#termediate la#$&a$e. F T!e i#termediate la#$&a$e is t!e# compiled by G&st-I#-Time compiler to #ative mac!i#e code. F So it really does#1t matter t!at w!at la#$&a$e we write o&r code i#% it all compiles i#to same I#termediate 9a#$&a$e. F G&st-I#-Time compiler o#ly #eeds to &#dersta#d o#e la#$&a$e% #ot all of t!em. It o#ly #eeds to &#dersta#d t!e I#termediate 9a#$&a$e. F It compiles it i#to #ative code a#d #at&rally% t!at does #ot take place &#til r&# time. F .his process is completel" a tomatic and independent of how the program is coded. 7. What is the f nction of C.S? -/plain the classification of t"pes in C.S with a diagram. Ans) T!e 3ommo# Type System D3TS= defi#es !ow types are declared% &sed% a#d ma#a$ed i# t!e r&#time. It is a# importa#t for 9a#$&a$e I#teroperability. It performs t!e followi#$ f&#ctio#s) Establis!es a commo# framework t!at e#ables cross-la#$&a$e i#te$ratio#% type safety% a#d !i$! performa#ce code e+ec&tio#. -rovides a# ob'ect-orie#ted model. .efi#es r&les t!at la#$&a$es m&st follow% so t!at differe#t la#$&a$es ca# i#teract wit! eac! ot!er. lassifications of t#pes in T$ are as follo%s& ' 1. 8al e t"pes 2. 4eference t"pes

Basics of .NET [BSIT-61]

8al e t"pes directly stores data i# t!e variable. 2al&e types ca# be b&ilt-i# type% &ser-defi#ed types% or e#&meratio#s. 4eference t"pes store a refere#ce to t!e data1s memory address. >efere#ce types ca# be self describi#$ types% poi#ter types% or i#terface types. Self-describi#$ types are f&rt!er split i#to arrays a#d class types. The class t#pes are user'defined classes, boxed value t#pes, and delegates. 9ll t"pes deri%e from the S"stem.:!1ect !ase t"pes. ;.Write a note on metadata. Ans: Metadata: - I# earlier days% it was #ot possible t!at a software compo#e#t?mod&le D.e+e or .dll= writte# i# o#e la#$&a$e co&ld $et &sed i# a#ot!er mod&le% w!ic! is writte# i# a#ot!er la#$&a$e. T!e sol&tio# came i# t!e form of 3* compo#e#ts. T!e .NET (ramework makes la#$&a$e i#teroperatio# eve# easier by allowi#$ compilers to p&t additio#al i#formatio# i#to all compiled mod&les a#d assemblies. T!is i#formatio# is called etadata. etadata is bi#ary i#formatio# describi#$ t!e pro$ram. 5!e# we compile o&r code i#to a -E file% metadata is i#serted i#to o#e portio# of t!e -E file% w!ile t!e code is co#verted to I9 a#d i#serted i#to a#ot!er portio# of t!e -E file. Every type% member% w!ic! are defi#ed or refere#ced i# a mod&le% is described wit!i# metadata. 5!e# code is e+ec&ted, t!e r&#time loads metadata i#to memory a#d refers it to $et i#formatio# abo&t t!e codes classes% members% i#!erita#ce% a#d so o#. etadata allows .NET la#$&a$es to describe t!emselves i# a la#$&a$e-#e&tral ma##er. It $ives re<&ired i#formatio# to 39> to perform m&c! of its f&#ctio#alities. T!e prese#ce of metadata makes t!e assembly Self-describi#$ file. T!e 39> mod&les or compo#e#ts or ot!er assemblies% w!ic! wa#t to comm&#icate wit! eac! ot!er &ses% t!ese i#formatio# stored i# metadata to perform t!eir tasks. It stores information li(e ' a. .escriptio# of t!e assembly1s operatio#. b. "ssembly Ide#tity D#ame% versio#% c&lt&re% p&blic key=. c. E+ported types d. *t!er depe#de#t assemblies e. Sec&rity permissio#s f. .escriptio# of types A

Basics of .NET [BSIT-61] $. !. embers like met!ods% fields% properties% eve#ts% #ested types% etc. "ttrib&tes

1<. =>t is told that if we learn an" one .N-. lang age0 then working with other .N-. lang ages will !e eas"?. 2ow? -/plain with the appropriate e/ample. Ans:

Chapter 3 1. $ist o t the important feat res of C@? Ans: Important features of ) are& 3C is a# ele$a#t% simple% type-safe% ob'ect-orie#ted la#$&a$e. 3C !as t!e capability to b&ild d&rable system-level compo#e#ts d&e to followi#$ feat&res) (&ll 3* ?-latform s&pport for e+isti#$ code i#te$ratio#. >ob&st#ess t!ro&$! $arba$e collectio# a#d type safety. Sec&rity provided t!ro&$! $ood desi$#. (&ll s&pport of e+te#sible metadata co#cepts. 3C s&pports 9a#$&a$e I#teroperability a#d -latform I#depe#de#ce d&e to followi#$ feat&res) (&ll i#teroperability wit! 3* a#d .NET (ramework services% t!ro&$! ti$!t .NET 9ibrary Base classes. B 9 s&pport for web-based compo#e#t i#teractio#. 2ersio#ability.
2. Write a program in C@ to displa" =Welcome to the world of C Sharp?. -/plain the program. Ans:

?? " 65elcome to t!e world of 3 S!arp7 pro$ram i# 3C &si#$ System, class 5elcome L p&blic static void ai# Dstri#$[] ar= L 3o#sole.5rite9i#e D65elcome to t!e world of 3 S!arp7=, M M

1N

Basics of .NET [BSIT-61] $escription of the abo e &ritten program 1 Comments) - 3omme#ts are &sed for proper doc&me#tatio# of t!e code. T!e compiler does #ot co#sider t!e comme#ts as t!e part of code. 3ompiler i$#ores t!e comme#ts d&ri#$ t!e time of compilatio#. 3omme#ts make t!e pro$ram more &#dersta#dable. 3C adopts t!e 344 style of comme#ts. T!e first li#e is a comme#t) ?? " 65elcome to t!e world of 3 S!arp7 pro$ram i# 3C T!e c!aracters ?? co#vert t!e rest of t!e li#e to a comme#t. Oo& ca# also comme#t a block of te+t by placi#$ it betwee# t!e c!aracters ?P a#d P?% for e+ample) The Main Method) - T!e 3C pro$ram m&st co#tai# a ai# met!od% i# w!ic! co#trol starts a#d e#ds. Oo& ca# create ob'ects a#d e+ec&te ot!er met!ods wit!i# t!is ai# met!od. Note t!at t!e alp!abet / 1 i# ai# is &ppercase% w!ere as i# 3 a#d 344 it is lowercase. T!e ai# met!od is a static met!od a#d resides i#side a class. I# t!e 65elcome to t!e world of 3 S!arp7 e+ample% it resides i#side t!e /5elcome1 class. Input and 2utput) - I# t!e above 5elcome pro$ram t!ere is o#ly o#e o&tp&t stateme#t% #o i#p&t stateme#ts. T!e stateme#t) System.3o#sole.5rite9i#eD65elcome to t!e world of 3 S!arp7=, &ses t!e 5rite9i#e met!od% of t!e 3o#sole class w!ic! resides i# t!e .NET r&#-time library. T!is met!od displays its stri#$ parameter o# t!e sta#dard o&tp&t. T!ere are ma#y ot!er 3o#sole met!ods are &sed for differe#t i#p&t a#d o&tp&t operatio#s. If yo& i#cl&de t!e followi#$ &si#$ stateme#t at t!e be$i##i#$ of t!e pro$ram) Compilation and Execution) - T!e compile a#d e+ec&tio# comma#ds $ive# !ere are for comma#d li#e compiler. 5e are #ot $oi#$ i#to t!e 2is&al 3C I.E. (ollow t!e below steps to compile a#d e+ec&te a pro$ram. By &si#$ a#y te+t editor create a so&rce file a#d save it as 5elcome.cs. T!e 3C so&rce code files s!o&ld !ave .cs e+te#sio#. To compile t!e pro$ram &se t!e comma#d $ive# below. If t!ere are #o compilatio# errors 5elcome.e+e is created. csc 5elcome.cs To r&# t!e pro$ram% e#ter t!e comma#d - 5elcome It will display 65elcome to t!e world of 3 S!arp7 o# t!e co#sole. 3. -/plain the general str ct re of a C@ program. Ans) 3C pro$ram ca# co#sist of o#e or more files. Eac! file ca# co#tai# o#e or more #amespaces. " #amespace co#tai#s $ro&p of related types s&c! as classes% str&cts% i#terfaces% e#&meratio#s% a#d dele$ates. Namespaces may be #ested. T!e followi#$ is t!e skeleto# of a 3C pro$ram t!at co#tai#s all of t!ese eleme#ts. ?? " skeleto# of a 3C pro$ram &si#$ System, #amespace Namespace1 L class 3lass1 LM str&ct Str&ct1 LM i#terface I#terface1 LM dele$ate i#t .ele$ate1D=, e#&m E#&m1 LM 11

Basics of .NET [BSIT-61] #amespace Namespace; LM class 3lass; L p&blic static void ai#Dstri#$[] ar$s= LMMM 4. 2ow do namespaces and t"pes in C@ ha%e niA e names? 5i%e an e/ample. Ans) Namespaces a#d types always !ave &#i<&e #ames. (or e+ample% If t!ere are ; class wit! same #ame b&t are prese#t i# differe#t #amespaces t!e# bot! t!e ob'ects will !ave t!eir &#i<&e f&lly <&alified #ames. #amespace "B3 ?? "B3 L class 3lass1 ?? "B3.3lass1 LM M #amespace BOQ ?? BOQ L class 3lass1 ?? BOQ.3lass1 LM M Rere t!e "B3.3lass1 a#d BOQ.3lass1 are two differe#t types% eve# if t!e class #ame is same. I# t!e followi#$ code e+ample s!ows t!e #ested classes a#d #amespaces. 3aref&lly e+ami#e t!e f&lly <&alified #ames $ive# as comme#ts. #amespace "B3 ?? "B3 L class 3lass1 ?? "B3.3lass1 L class 3lass; ?? "B3.3lass1.3lass; LM M #amespace BOQ ?? "B3.BOQ L class 3lass; ?? "B3.BOQ.3lass; LM M M (rom t!e above code e+ample) T!e #amespace "B3 is a member of t!e $lobal #amespace. Its f&lly <&alified #ame is "B3. T!e #amespace BOQ is a member of "B3. Its f&lly <&alified #ame is "B3.BOQ. T!e class 3lass1 is a member of t!e "B3. Its f&lly <&alified #ame is "B3.3lass1. T!e class #ame 3lass; is &sed twice i# t!is code. Rowever% t!e f&lly <&alified #ames are &#i<&e. T!e first o#e is declared i#side 3lass1, t!&s its f&lly <&alified #ame is) "B3.3lass1.3lass;. T!e seco#d is declared i#side a #amespace BOQ, t!&s% its f&lly <&alified #ame is "B3.BOQ.3lass;. 0si#$ t!e precedi#$ code se$me#t% yo& ca# add a #ew class member 3lass@ to t!e #amespace "B3.BOQ as follows) #amespace "B3.BOQ L class 3lass@ ?? "B3.BOQ.3lass@ LM 1;

Basics of .NET [BSIT-61] M #. $ist o t the difference !etween C@ and CBB. Ans: *ollo%ing are the differences bet%een ) and ++ ' C@ is p rel" o!1ect)oriented lang age0 whereas CBB s pport o!1ect)oriented programming. 9rra"s6 The s#ntax of declaring ) arra#s is different from that of ++ arra#s. The to(ens ,-./ appear follo%ing the arra# t#pe in ). The !ool t"pe in )& There is no conversion bet%een the bool t#pe and int. The long t"pe& In C@0 the long data t"pe is &4 !its0 while in CBB0 it is 32 !its. The str ct t"pe& In ), classes and structs are semanticall# different. A struct is a value t#pe, %hile a class is a reference t#pe. The switch statement& Unli(e the ++ s%itch statement, ) does not support fall through from one case label to another. The delegate t"pe& Delegates are roughl# similar to function pointers in ++, but the# are t#pesafe and secure. 3reprocessor directi%es are used for conditional compilation. No header files are used in ). C@ operators& ) supports additional operators such as is and t#pe of. It also introduces different functionalit# of some logical operators. The Cain method is declared differentl# from the main function in ++. The usage of the command'line arguments are also different. Cethod parameters& ) supports ref and out parameters, %hich are used instead of pointers in passing parameters b# reference. 3ointers are allowed in C@ ! t onl" in nsafe mode. :%erloading operators is performed differentl" in C@. Strings& ) strings are different from ++ strings. The foreach ke"word allo%s #ou to iterate through arra#s and collections. No global methods or variables in )& 0ethods and variables must be contained %ithin a t#pe declaration. No header files or )include directives in )& The using directive is used to reference t#pes in other namespaces %ithout full# 1ualif#ing the t#pe names. ,estr ctors6 In ), #ou don2t have control over %hen a destructor is called because destructors are called automaticall# b# the garbage collector. Constr ctors6 Unli(e ++, if #ou don2t provide a class constructor in ), a default constructor is automaticall# generated for #ou. The default constructor initiali3es all the fields to their default values. ) does not support bit fields.

1@

Basics of .NET [BSIT-61]

Chapter 4 1. Which are two different categories of t"pe in C@? Ans: T!ere are two differe#t cate$ories of type i# 3C are : [i.] 2al&e type [ii.] >efere#ce type. i. %alue t"pes: ( T!ey directly co#tai# data. F T!e variable !ave t!eir ow# copy of t!e data. F " variable of a type always stores a data val&e of t!at type. F "ssi$#me#t to a val&e type variable creates a copy of t!e val&e bei#$ assi$#ed. F Some e+amples of t!e 2al&e type are : c!ar% i#t% float. ii. 'eference t"pes: ( T!e >efere#ce type do #ot co#tai# data b&t t!ey co#tai# a refere#ce to t!e variables% w!ic! are stored i# memory. F 0si#$ more t!a# o#e variables% we ca# &se refere#ce types to refer to a memory locatio#. F T!is mea#s t!at if t!e val&e i# t!e memory locatio# is modified by o#e of t!e variables% t!e ot!er variables a&tomatically reflect t!e c!a#$ed val&e. F I# refere#ce type% it is possible for a data to refere#ce a# ob'ect of ot!er derived type. F "ssi$#me#t to a refere#ce type variable% copies t!e address of ob'ect. F Some e+amples of >efere#ce type are : class% stri#$% i#terface% array% dele$ate type. 2. What are %aria!les? 2ow man" categories of %aria!les are there in C@? ,efine each. Ans: " variable is a locatio# i# t!e memory t!at !as a #ame a#d co#tai#s a val&e. F T!e 2al&e co&ld be a# i#te$er% decimal% c!aracter% stri#$. F " variable is associated wit! a data type t!at defi#es t!e type of data% w!ic! ca# be stored i# a variable. F 2ariables ca# be i#itially assi$#ed or i#itially #ot assi$#ed. I# 3C variables are cate$ori8ed i# J cate$ories w!ic! are me#tio#ed below : i. !tatic ariables: ( " variable declared wit! t!e static modifier is called a static variable. T!ere will be 1E

Basics of .NET [BSIT-61] o#ly o#e copy of t!e variable% re$ardless of !ow ma#y i#sta#ces of class are created. ii. Instance ariables: ( " variables declared wit!o&t t!e static modifiers are i#sta#ce variables. iii. Arra" elements: ( T!e array is a co#tai#er t!at !as a list of stora$e locatio#s for a specified type. T!e eleme#ts for a specified type of a# array come i#to e+iste#ce w!e# a# array i#sta#ce is created% a#d e+ist &#til t!ere are #o refere#ces to t!at array i#sta#ce. i . %alue parameters: ( -arameters are ar$&me#ts passed to t!e f&#ctio#. " parameter declared wit!o&t a ref or o&t modifier is a val&e parameter. " val&e parameter e+ists &#til t!e f&#ctio# ret&r#s. . 'eference parameters: ( " parameter declared wit! a ref modifier is a refere#ce parameter. " refere#ce parameter does #ot create a #ew i#sta#ce% it poi#ts to t!e same stora$e locatio# of t!e parameter passed as a# ar$&me#t. i. 2utput parameters: ( " parameter declared wit! a# o&t modifier is a# o&tp&t parameter. " refere#ce parameter does #ot create a #ew i#sta#ce% it poi#ts to t!e same stora$e locatio# of t!e parameter passed as a# ar$&me#t. B&t o&tp&t parameter m&st be defi#itely assi$#ed before t!e f&#ctio# ret&r#s. ii. Local ariables: ( " local variable is declared a#d e+its wit!i# a block% for-stateme#t% switc!stateme#t% &si#$ stateme#t. " local variable is #ot a&tomatically i#itiali8ed% t!&s !as #o defa&lt val&e. 3. What is the importance of a tomatic memor" management? -/plain with e/ample? Ans: I# earlier days% developers #eed to ma#a$e t!e allocatio# a#d de-allocatio# of blocks of memory% w!ic! is k#ow# as a#&al memory ma#a$eme#t. a#&al memory ma#a$eme#t ca# be bot! time-co#s&mi#$ a#d diffic&lt. F B&t i# .NET (ramework% 39> provides a r&#-time e#viro#me#t% w!ic! ma#a$es t!e e+ec&tio# of code a#d provides esse#tial services like "&tomatic memory ma#a$eme#t. F "&tomatic memory ma#a$eme#t elimi#ates memory leaks as well as some ot!er commo# pro$rammi#$ errors. F I# 3C% a&tomatic memory ma#a$eme#t is provided so t!at developers are freed from t!is b&rde#some task. F "&tomatic memory ma#a$eme#t i#creases code <&ality a#d e#!a#ces developer prod&ctivity wit!o&t #e$ative impact o# eit!er e+pressive#ess or performa#ce. 4. Write a note on e/pression and operators in C@. Ans: Expression: ( "# e+pressio# is a se<&e#ce of operators a#d opera#ds t!at specifies comp&tatio# a#d assi$#s t!e res&lt to a variable. FE+pressio#s are co#str&cted from opera#ds a#d operators. T!e operators of a# e+pressio# i#dicate w!ic! operatio#s to perform. 2perators: ( "pplicatio# &se operators to process t!e data e#tered by a &ser. F *perators like 4 a#d : are &sed to process variables a#d ret&r# a val&e. F "# operator is a set of o#e or more c!aracters t!at is &sed for comp&tatio#s or compariso#s. F *perator ca# tra#sform o#e or more data val&es% called opera#ds% i#to a #ew data val&e. F Some e+amples of *perators are "rit!metic% "rit!metic "ssi$#me#t% 0#ary% 3ompariso#% 9o$ical operators. #. What are empt" statement and la!eled statements? Wh" are the" sed? Ans: Empt" statement: ( "# empty stateme#t DS= simply tra#sfers co#trol to t!e e#d poi#t of t!e stateme#t. F It is &sed w!e# t!ere are #o operatio#s to perform% b&t re<&ire a# stateme#t. 1H

Basics of .NET [BSIT-61] F "# empty stateme#t ca# be &sed w!e# writi#$ a w!ile stateme#t wit! a #&ll body a#d ca# also be &sed to declare a label '&st before t!e closi#$ 6M7 of a block. Labeled statement: ( " labeled stateme#t is prefi+ed by a label. F 9abeled stateme#ts are permitted i# blocks. F " label is a #ame $ive# by t!e ide#tifier. F T!e scope of a label is t!e block i# w!ic! t!e label is declared. F If two labels !ave same #ame we $et compile time error. F " label ca# be refere#ced from $oto stateme#ts wit!i# t!e scope of t!e label. F 9abels a#d ot!er ide#tifiers ca# !ave same #ame. &. What is an iterati%e statement? -/plain foreach statement with e/ample? Ans: Iterative stateme#ts repeatedly e+ec&te a block of stateme#ts. F T!e #&mber of iteratio#s depe#ds o# t!e co#ditio# represe#ted by Boolea# e+pressio#. F T!e block of code will be iterated &#til t!e co#ditio# is tr&e. 3oreach statement: 1 " foreac! stateme#t lets &s iterate over t!e eleme#ts i# arrays a#d collectio#s. F T!e eleme#ts of si#$le-dime#sio#al arrays are traversed i# i#creasi#$ order of i#de+% starti#$ wit! i#de+ N to 9e#$t!-1. F T!e eleme#ts of m&lti-dime#sio#al arrays eleme#ts are traversed s&c! t!at t!e i#dices of t!e ri$!tmost dime#sio# are i#creme#ted first% t!e# t!e #e+t left dime#sio#% a#d so o# to t!e left. 4 *ollo%ing example uses a foreach statement to iterate over the elements of an arra# &si#$ System, class (orEac!E+ample L static void ai# D= L i#t[] arr T#ew i#t[] L1%;%@%EM, foreac! Di#t i i# arr= 3o#sole.5rite9i#e D62al&e is LNM7% arr=, M M +. Which are different 1 mp statements s pported !" C@? Ans: .iffere#t '&mp stateme#ts s&pported by 3C are) +rea. !tatements: ( 5!e# t!ere is a #eed to e+it a loop before t!e loop co#ditio# is re-eval&ated after iteratio#. "s wit! t!e w!ile loop% we ca# &se break stateme#t to e+it from t!e loop. e.$.) - &si#$ System, class E+UBreak L static void ai# Dstri#$[] ar= L i#t iTN, w!ile Dtr&e= L if DiTTar.9e#$t!= break, 3o#sole.5rite9i#e Dar[i44]=, 16

Basics of .NET [BSIT-61] M M M Continue !tatements: ( T!e co#ti#&e stateme#t is &sed to skip all t!e s&bse<&e#t i#str&ctio#s a#d take t!e co#trol back to t!e loop. e.$.) - &si#$ System, class E+U3o#ti#&e L static void ai# Dstri#$[] ar= L i#t iTN, w!ile Dtr&e= L 3o#sole.5rite9i#e Dar[i44]=, if Di V ar.9e#$t!= co#ti#&e, break, M M M 'eturn !tatement: ( et!ods ca# eit!er ret&r# a type or #ot. " met!od t!at does #ot ret&r# a type m&st $ive its ret&r# type as /void1. " met!od t!at does ret&r# a type m&st #ame t!e type ret&r#ed. " met!od will stop e+ec&ti#$ a#d ret&r# a val&e w!e# it reac!es a /ret&r#1 stateme#t at a#y poi#t i# its e+ec&tio#. T!e type ret&r#ed is $ive# at t!e e#d of s&c! a ret&r# stateme#t, its type m&st correspo#d wit! t!e type specified i# t!e met!od declaratio#. (or e+ample% t!e followi#$ piece of code ill&strates t!is poi#t. e.$.) - &si#$ System, class E+U>et&r# L static i#t add Di#t a% i#t b= L ret&r# a4b, M static void ai# D= L 3o#sole.5rite9i#e DaddD1%;==, ret&r#, M M Thro& !tatement: ( 5e ca# t!row a# ob'ect if t!e ob'ect is eit!er directly or i#directly derived from System.E+ceptio#. 5e ca# &se t!row stateme#t i# t!e catc! block to t!row t!e prese#t ob'ect a#d we ca# also t!row a #ew e+ceptio# of a differe#t type. " t!row wit!o&t a#y e+pressio# ca# o#ly be &sed i# t!e catc! block. S&c! a t!row reverts t!e e+ceptio# t!at is c&rre#tly bei#$ !a#dled. Tr" !tatement: ( T!e Try block $&ards stateme#ts t!at may t!row a# e+ceptio#. T!e block $over#s 1J

Basics of .NET [BSIT-61] stateme#ts t!at are e#closed wit!i# it a#d defi#es t!e scope of t!e e+ceptio# !a#dlers associated wit! it. If a# e+ceptio# occ&rs wit!i# t!e try block% a# appropriate e+ceptio# !a#dler t!at is associated wit! t!e try block !a#dles t!e e+ceptio#. e.$.) - &si#$ System) class E+UT!rowUTry L static i#t ( Di#t a% i#t b= L if DbTTN= t!row #ew e+ceptio# D6.ivided by Qero7=, ret&r# a?b, M static void ai# D= L try L 3o#sole.5rite9i#e D(DH%N==, M catc! DE+ceptio# e= L 3o#sole.5rite9i#e D6Error7=, M M M 7. What are checked and nchecked statements? 5i%e -/ample. Ans: Chec.ed !tatement: ( T!e 3!ecked stateme#t ca&ses all e+pressio#s i# t!e c!ecked block to be eval&ated i# a c!ecked co#te+t. 4nchec.ed !tatement: ( T!e 0#c!ecked stateme#t ca&ses all e+pressio#s i# t!e &#c!ecked block to be eval&ated i# a# &#c!ecked co#te+t. e.$.) - &si#$ System, class E+U3!eckU0#c!eck L static void ai#D= L i#t + T I#t@;. a+2al&e, 3o#sole.5rite9i#eD+ 4 1=, ?? *verflow c!ecked L 3o#sole.5rite9i#eD+ 4 1=, ?? E+ceptio# M &#c!ecked L 3o#sole.5rite9i#eD+ 4 1=, ?? *verflow M
}

M ;. 5i%e an e/ample lock and sing statements. 1K

Basics of .NET [BSIT-61] Ans: Loc. !tatements: ( T!e lock stateme#t obtai#s t!e m&t&al e+cl&sio# lock for a $ive# ob'ect% e+ec&tes stateme#ts% a#d t!e# releases t!e lock. T!e e+ample is s!ow# below : e.$.) - static void ai#D= L " a T ..., lockDa= L a.- T a.- 4 1, M M 4sing !tatements: ( T!e &si#$ stateme#t obtai#s o#e or more reso&rces% e+ec&tes stateme#ts% a#d t!e# releases of t!e reso&rce. e.$.) - static void ai#D= L &si#$ D>eso&rce r T #ew >eso&rceD== L r.(&#cD=, M M 1<. 2ow are str ct res different from classes in C@? Ans: (ollowi#$ are t!e differe#ces betwee# Str&ct&res a#d 3lasses i# 3C) F Str&ct&res are val&e types a#d t!ey $et stored i# a stack. F Str&ct&res do #ot s&pport i#!erita#ce. F Str&ct&res ca##ot !ave defa&lt co#str&ctor. 11. What are interfaces? 5i%e an e/ample. Ans: Interfaces: - I#terface is a sy#tactical co#tract t!at all derived classes s!o&ld follow. I#terface keyword declares a refere#ce type t!at !as abstract members. I#terface ca# co#tai# met!ods% properties% i#de+ers a#d eve#ts as members. T!ey ca# co#tai# co#sta#ts% fields% co#str&ctors a#d destr&ctors or a#y type of static member. "ll t!e members of a# i#terface are p&blic by defi#itio#. e.$.) - i#terface E+ample L Stri#$ t!is[i#t i#de+] L $et, set, M eve#t Eve#tRa#dler E, void (&#c Di#t val&e=, stri#$ L $et, set, M M p&blic dele$ate void Eve#tRa#dler Dob'ect se#der% Eve#t"r$s=, 12. What is delegate? What is the se of it? 5i%e e/ample? 1A

Basics of .NET [BSIT-61] Ans: " .ele$ate is a refere#ce type variable% w!ic! !olds t!e refere#ce to a met!od. F T!is refere#ce ca# be c!a#$ed at r&#time% as desired. F .ele$ates i# 3C allow &s to dy#amically c!a#$e t!e refere#ce to t!e met!ods i# a class. F .ele$ates are primarily &sed i# 3C pro$rammi#$ for impleme#ti#$ eve#ts a#d t!e call-back met!ods. F T!ere are t!ree steps i# defi#i#$ a#d &si#$ dele$ates) declaratio#% i#sta#tiatio# a#d i#vocatio#. e.$.) - (irst declare a dele$ate by followi#$ sy#ta+ : dele$ate void Simple.ele$ateD=, ?? .ele$ate t!at takes #o ar$&me#ts a#d ret&r#s #o res&lt &si#$ System, class Test L static void (D= L 3o#sole.5rite9i#eD6Test.(7=, M static void ai# D= L Simple.ele$ate dT#ew Simple.ele$ateD(=, dD=, M M 13. Write a program to demonstrate the sage of en ms in C@? Ans: &si#$ System, Namespace E#&mOear L class .isplayOear L e#&m yearLGa#%(eb% arc!%"pril% ay%G&#e%G&ly%"&$%Sept%*ct%Nov%.ecM, static void ai#Dstri#$[] ar= L i#t (irstU o#t!TDi#t=year.Ga#, i#t 9astU o#t!TDi#t=year..ec, 3o#sole.5rite9i#eD6Ga# TLNM7% (irstU o#t!=, 3o#sole.5rite9i#eD6.ec TLNM7%9astU o#t!=, M M M 14. Write a note on namespaces in C@. Ans: Namespaces: ( Namespace provides a way to $ro&p classes% by providi#$ a# e+tra level of #ami#$ beyo#d t!e class #ame. F 3C pro$rams are or$a#i8ed &si#$ #amespaces. F Namespaces are &sed bot! as a# 6i#ter#al7 or$a#i8atio# system for a pro$ram% a#d as a# 6e+ter#al7 or$a#i8atio# system. F 3lass act&ally co#tai# data a#d #amespaces are &sed to lo$ically arra#$e classes. F Namespaces ca# also co#tai# ma#y ot!er Namespaces a#d classes. F 3C pro$rams are or$a#i8ed &si#$ #amespaces. F Namespaces are implicitly p&blic a#d t!e declaratio# of a #amespace does #ot i#cl&de a#y access ;N

Basics of .NET [BSIT-61] modifiers. F Namespaces ca# be #ested. a#y #amespaces i# t!e base class library are #ested. F " #amespace declaratio# co#sist of t!e keyword #amespace% followed by a #amespace #ame a#d body) #amespace NamespaceName L W.?? #amespace body M 1#. What is the se of attri! te in C@ program? Ans: "ttrib&tes are &sed for addi#$ metadata% s&c! as compiler i#str&ctio#s a#d ot!er i#formatio# s&c! as comme#ts% descriptio#% met!ods% a#d classes to a pro$ram. F -ro$rams specify t!is additio#al declarative i#formatio# by defi#i#$ a#d &si#$ "ttrib&tes. F "ttrib&tes are applied to differe#t eleme#ts of t!e code. F T!ese eleme#ts i#cl&des assemblies% mod&les% str&cts% e#&ms% co#str&ctors% properties% fields a#d dele$ates. F I#formatio# abo&t attrib&te is stored wit! t!e metadata of t!e eleme#ts t!ey are associated wit!.

Chapter # 1. Write a note on classes in C@? Ans: 3lass is t!e collectio# of ember data a#d ember f&#ctio#s. F 3lass declaratio#s are refere#ce type. F " class ca# i#!erit from a#ot!er class% a#d ca# impleme#t i#terfaces. F 3lass members ca# i#cl&de co#sta#ts% fields% met!ods% properties% eve#ts% i#de+ers% operators% i#sta#ce co#str&ctors% destr&ctors% static co#str&ctors% a#d #ested type declaratio#s. F Eac! member !as a# associated accessibility% w!ic! co#trols t!e portio# of pro$ram code t!at is able to access t!e member. 2. What are constant mem!ers? 5i%e an e/ample. Ans: " co#sta#t member is a class member t!at represe#ts a co#sta#t val&e. 3o#sta#t members are i#itiali8ed at t!e time of t!eir declaratio#. F 3o#sta#ts are co#sidered static members. F 3o#sta#t member declaratio# does #ot re<&ire t!e static modifier. e.$.) - class 3o#sta#t L p&blic co#st i#t "T1, p&blic co#st i#t BT"41, M 3. 5i%e an e/ample of field mem!er. Ans: " field is a member t!at represe#ts a variable associated wit! a# ob'ect or class. F T!e fields are i#itiali8ed at some poi#t before t!ey are &sed% b&t after t!is i#itiali8atio# t!ey ca# be c!a#$ed. e.$.) - class 3olor L ;1

Basics of .NET [BSIT-61] i#ter#al &s!ort red-art, i#ter#al &s!ort bl&e-art, i#ter#al &s!ort $ree#-art, p&blic 3olorD&s!ort red% &s!ort bl&e% &s!ort $ree#= L red-art T red, bl&e-art T bl&e, $ree#-art T $ree#, M p&blic static 3olor >ed T #ew 3olorDN+((% N% N=, p&blic static 3olor Bl&e T #ew 3olorDN% N+((% N=, p&blic static 3olor Iree# T #ew 3olorDN% N% N+((=, p&blic static 3olor 5!ite T #ew 3olorDN+((% N+((% N+((=, M

4. With appropriate e/amples e/plain parameter passing in C@. Ans: I# 3C% parameter ca# be passed eit!er by alue or by reference. -assi#$ parameters by refere#ce allows f&#ctio# members Dmet!ods% properties% i#de+ers% operators a#d co#str&ctors= to c!a#$e t!e val&e of t!e parameters a#d !ave t!at c!a#$e persist. To pass a parameter by refere#ce% &se t!e ref or o t keyword. e.$. ) - In the follo%ing example, it is not the value of m#Int that is passed5 rather, a reference to m#Int is passed. The parameter x is not an int5 it is a reference to an int. Therefore, %hen x is s1uared inside the method, %hat actuall# gets s1uared is %hat x refers to & m#Int.
using System; class ParameterPassingValByRef { static void SquareIt(ref int x) // !e "arameter x is "assed #y reference$ // %!anges to x &ill affect t!e original value of myInt$ { x '( x; %onsole$)rite*ine(+ !e value inside t!e met!od, {-.+/ x); . "u#lic static void 0ain() { int myInt ( 1; %onsole$)rite*ine(+ !e value #efore calling t!e met!od, {-.+/myInt); SquareIt(ref myInt); // Passing myInt #y reference$ %onsole$)rite*ine(+ !e value after calling t!e met!od, {-.+/myInt); . .

2utput : ( T!e val&e before calli#$ t!e met!od) H T!e val&e i#side t!e met!od ) ;H T!e val&e after calli#$ t!e met!od ) ;H ;;

Basics of .NET [BSIT-61] #. What is the se of propert" mem!er in a class? Ans: -roperty is a member t!at provides access to a c!aracteristic of a# ob'ect or a class. F -roperties !ave accessors t!at is &sed to specify t!e block of stateme#t to be e+ec&ted w!e# t!eir val&es are read or writte#. D -roperties t!at ca# bot! read a#d writte#% s&c! as captio#% i#cl&de bot! $et a#d set accessors. F T!e declaratio# of properties is relatively strai$!tforward % b&t t!e real val&e of properties is see# w!e# t!ey are &sed. &. What are e%ents? 5i%e an e/ample to demonstrate its sage. Ans: "# eve#t is a# actio# or occ&rre#ce% s&c! as clicks% key presses% mo&se moveme#ts or system $e#erated #otificatio#s. F "# eve#t is a member t!at e#ables a# ob'ect or class to provide #otificatio#s. F " class defi#es a# eve#t by providi#$ a# eve#t declaratio# a#d a# optio#al set of eve#t accessors. F Eve#ts are a# affective mea# of i#ter-process comm&#icatio#. F Eve#ts are messa$e se#t by t!e ob'ect to i#dicate t!e occ&rre#ce of t!e eve#t. F "pplicatio#s ca# respo#d to eve#ts w!e# t!ey occ&r. e.$.) - &si#$ System, p&blic class (orm1 L p&blic (orm1D= L B&tto#1.3lick 4T #ew Eve#tRa#dlerDB&tto#1U3lick=, M B&tto# B&tto#1T#ew B&tto#D=, void B&tto#1U3lick Dob'ect se#der% Eve#t"r$s e= L 3o#sole.5rite9i#eD6B&tto#1 was 3licked7=, M p&blic void disco##ectD= L B&tto#1.3lick -T #ew Eve#tRa#dlerDB&tto#1U3lick=, M M +. With an e/ample e/plain inde/ers. Ans: "# i#de+er e#ables a# ob'ect to be i#de+ed i# t!e same way as a# array. F I#de+ers e#able array-like access. F I#de+ers declaratio#s are similar to property declaratio#s% wit! t!e mai# differe#ces bei#$ t!at i#de+ers are #ameless a#d t!at i#de+ers i#cl&de i#de+i#$ parameters. e.$.) - &si#$ System, p&blic class Stack L private Node IetNodeDi#t i#de+= L Node temp T first, w!ile Di#de+ X N= L temp T temp.Ne+t, i#de+ Y Y, ;@

Basics of .NET [BSIT-61] M ret&r# temp, M p&blic ob'ect t!is[i#t i#de+] L $et L if DZ2alidI#de+Di#de+== t!row #ew E+ceptio#D6I#de+ o&t of ra#$e.7=, else ret&r# IetNodeDi#de+=.2al&e, M set L if DZ2alidI#de+Di#de+== t!row #ew E+ceptio#D6I#de+ o&t of ra#$e.7=, else IetNodeDi#de+=.2al&e T val&e, M M M class Test L static void ai#D= L Stack s T #ew StackD=, s.-&s!D1=, s.-&s!D;=, s.-&s!D@=, s[N] T @@, s[1] T ;;, s[;] T 11, M M 7. -/plain each with an e/ample Ans: a. Instance constructors: ( "# I#sta#ce co#str&ctor is called w!e#ever a# i#sta#ce of a class is created. F T!ese co#str&ctors are &sed to i#itiali8e data members of t!e class. e.$.) - &si#$ System, #amespace calc L class 3alc&lator L static i#t #&m1%#&m;%total, 3alc&latorD= L #&m1T1N, ;E

Basics of .NET [BSIT-61] #&m;T;N, M p&blic void "ddN&mberD= L totalT#&m14#&m;, M p&blic void .isplayN&mberD= L 3o#sole.5rite9i#eD[T!e total is LNM[%total=, M p&blic static void ai#Dstri#$[] ar = L 3alc&lator cT#ew 3alc&latorD=, c."ddN&mberD=, c..isplayN&mberD=, M M M b. $estructor: ( " .estr&ctor !as t!e same #ame as its class b&t is prefi+ed wit! a \ % w!ic! is t!e symbol of tilde. .estr&ctors ca##ot be i#!erited or overloaded. e.$.) - &si#$ System, #amespace calc L class 3alc&lator L static i#t #&m1%#&m;%total, p&blic void "ddN&mberD= L totalT#&m14#&m;, 3o#sole.5rite9i#eD6T!e Total is LNM7%total=, M 3alc&latorD= L #&m1T1N, #&m;T;N, totalTN, 3o#sole.5rite9i#eD63o#str&ctor I#voked7=, M \3alc&latorD= L 3o#sole.5rite9i#eD6.estr&ctor I#voked7=, M p&blic static void ai#Dstri#$[] ar = L 3alc&lator cT#ew 3alc&latorD=, c."ddN&mberD=, M M M c. !tatic constructor: ( Static co#str&ctors are &sed to i#itiali8e t!e static variables of a class. F T!ese variables are created &si#$ t!e static keyword a#d t!ey store val&es t!at ca# be s!ared by all t!e ;H

Basics of .NET [BSIT-61] i#sta#ces of a class. F T!e co#str&ctors will be i#voked o#ly o#ce d&ri#$ t!e e+ec&tio# of a pro$ram. F Static 3o#str&ctors !ave a# implicit private access. e.$.) - p&blic class StatU3o#str&ct L static i#t #&m1, i#t #&m;, static StatU3o#str&ct L #&m1T1N, ??*]. Si#ce #&m1 is static variable i# t!is class. #&m;T;@, ??E>>*>. Si#ce #&m; is #o#-static variable i# t!is class. M M ;. 2ow does C@ s pports inheritance? Ans: I# 3C% I#!erita#ce is t!e property by w!ic! t!e ob'ects of a derived class possess copies of t!e data members a#d t!e member f&#ctio#s of t!e base class. F " class t!at i#!erits or derives attrib&tes from a#ot!er class is called t!e derived class. F T!e class from w!ic! attrib&tes are derived is called t!e base class. F I# 3C% I#!erita#ce e#ables t!e easy mai#te#a#ce of code. "#y c!a#$e made to t!e base class a&tomatically c!a#$es t!e be!avior of its s&bclasses.

F T!e sy#ta+ &sed i# 3C for creati#$ derived classes is as follows) VaccessUspecifierX class VbaseUclassX L WW. M class VderivedUclassX ) VbaseUclassX L WW. M 1<. What is method o%erloading? 5i%e an e/ample. Ans: et!od *verloadi#$ is t!e most commo# way of impleme#ti#$ polymorp!ism. 5e ca# impleme#t et!od overloadi#$ by defi#i#$ two or more f&#ctio#s i# a class s!ari#$ t!e same #ame. e.$.) - &si#$ System, #amespace 3alc&late a+ L class 3alc&late a+ L p&blic i#t a+Di#t #&m1% i#t #&m;= L if D#&m1X#&m;= L ret&r# #&m1, M else L ret&r# #&m;, ;6

Basics of .NET [BSIT-61] M M p&blic float a+Dfloat #&m1% float #&m;= L ifD#&m1X#&m;= L ret&r# #&m1, M else L ret&r# #&m;, M M M class L a+3alc

static i#t ai#D= L 3alc&late a+ calT#ew 3alc&late a+D=, 3o#sole.5rite9i#eD6LNM7%cal. a+DH.E(% K.6(==, 3o#sole.5rite9i#eD6LNM7%cal. a+D1A%1;==, 3o#sole.>ead9i#eD=, ret&r# N, MM 11. What is operator o%erloading? 5i%e an e/ample. Ans: *perator overloadi#$ allows &ser-defi#ed types s&c! as str&ct&res a#d classes% to &se overloaded operators for easy ma#ip&latio# of t!eir ob'ects. *perator overloadi#$ ca# be ac!ieved by defi#i#$ t!e static member f&#ctio#s &si#$ t!e operator keyword. F T!e followi#$ code is a# e+ample s!owi#$ t!e &sa$e of operatorD4= Ro&r !1, Ro&r !;, Ro&r !@, R@TR14R;,

;J

Basics of .NET [BSIT-61] Chapter & 1. What is a preprocessor? What does the @if0 @else0 @elif0 @endif0 @define0 @ ndef0 @warning0 @error0 and @line directi%es do? 5i%e e/amples for each. Ans: 3reprocessor : ( 5!ile t!e compiler does #ot !ave a separate preprocessor% t!e directives are processed as if t!ere was a preprocessor. T!ese directives are &sed d&ri#$ co#ditio#al compilatio#. 0#like 3 a#d 344 directives% we ca##ot &se t!ese directives to create macros. " preprocessor directive m&st be t!e o#ly i#str&ctio# o# a li#e. 5if : ( Cif% be$i#s a co#ditio#al directive for testi#$ a symbol% to see if t!ey eval&ate to tr&e. If t!ey do eval&ate to tr&e% t!e compiler eval&ates all t!e code betwee# t!e Cif a#d t!e #e+t correspo#di#$ directive. T!e followi#$ operators ca# also be &sed to eval&ate m&ltiple symbols% like - TT De<&ality symbol=% ZT Di#e<&ality symbol= ^^ Da#d symbol=% __ Dor symbol=. e.$. ) - 2define 34B56
2define V%7V8 using System; "u#lic class 0y%lass { "u#lic static void 0ain() { 2if (34B56 99 :V%7V8) %onsole$)rite*ine(+34B56 2elif (:34B56 99 V%7V8) %onsole$)rite*ine(+V%7V8 2elif (34B56 99 V%7V8) %onsole$)rite*ine(+34B56 2else %onsole$)rite*ine(+34B56 2endif . .

is defined+); is defined+); and V%7V8 are defined+); and V%7V8 are not defined+);

5else : ( 5else lets &s create a compo&#d co#ditio#al directive% s&c! t!at% if #o#e of t!e e+pressio# i# t!e precedi#$ Cif or Celif directive did #ot eval&ate to tr&e% t!e compiler will eval&ate all code betwee# Celse a#d t!e s&bse<&e#t Ce#dif. e.$. ) - 2define 34B56
2define V%7V8 using System; "u#lic class 0y%lass { "u#lic static void 0ain() { 2if (34B56 99 :V%7V8) %onsole$)rite*ine(+34B56 is defined+); 2else %onsole$)rite*ine(+34B56 and V%7V8 are not defined+); 2endif . .

5elif : ( Celif let &s create a compo&#d co#ditio#al directive. T!e Celif e+pressio# will be eval&ated if #eit!er t!e precedi#$ Cif #or a#y precedi#$Doptio#al= Celif directive e+pressio#s eval&atevto tr&e. If a Celif e+pressio# eval&ates to tr&e% t!e compiler eval&ates all t!e code betwee# t!e Celif a#d t!e #e+t directive. ;K

Basics of .NET [BSIT-61] e.$. ) - 2define 34B56


2define V%7V8 using System; "u#lic class 0y%lass { "u#lic static void 0ain() { 2if (34B56 99 :V%7V8) %onsole$)rite*ine(+34B56 is defined+); 2else %onsole$)rite*ine(+34B56 and V%7V8 are not defined+); 2endif . .

5endif : ( Ce#dif specifies t!e e#d of a co#ditio#al directive% w!ic! be$a# wit! t!e Cif directive. " co#ditio#al directive% be$i##i#$ wit! a Cif directive% m&st e+plicitly be termi#ated wit! a Ce#dif directive. e.$. ) - 2define 34B56
2define V%7V8 using System; "u#lic class 0y%lass { "u#lic static void 0ain() { 2if (34B56 99 :V%7V8) %onsole$)rite*ine(+34B56 is defined+); 2else %onsole$)rite*ine(+34B56 and V%7V8 are not defined+); 2endif . .

5define : ( Cdefi#e lets yo& defi#e a symbol% s&c! t!at% by &si#$ t!e symbol as t!e e+pressio# passed to t!e Cif directive% t!e e+pressio# will eval&ate to tr&e. e.$. ) - 2define 34B56
2define V%7V8 using System; "u#lic class 0y%lass { "u#lic static void 0ain() { 2if (34B56 99 :V%7V8) %onsole$)rite*ine(+34B56 is defined+); 2else %onsole$)rite*ine(+34B56 and V%7V8 are not defined+); 2endif . .

5undef : ( C&#def% &#defi#es a symbol by &si#$ t!e symbol as t!e e+pressio# i# a Cif directive% t!e e+pressio# will eval&ate to false. T!e C&#def directive m&st appear i# t!e file before we &se a#y stateme#ts t!at are #ot directives. e.$. ) - 2undef 34B56
using System;

;A

Basics of .NET [BSIT-61]


"u#lic class 0y%lass { "u#lic static void 0ain() { 2if 34B56 %onsole$)rite*ine(+34B56 is defined+); 2else %onsole$)rite*ine(+34B56 is not defined+); 2endif . .

5&arning : ( Cwar#i#$% $e#erates a level o#e war#i#$ from a specific locatio# i# o&r code. " commo# &se of Cwar#i#$ is i# a co#ditio#al directive. e.$. ) - 2define 34B56
"u#lic class 0y%lass { "u#lic static void 0ain() { 2if 34B56 2&arning 34B56 is defined 2endif . .

5error : ( Cerror% $e#erates a# error from a specific locatio# i# yo&r code. " commo# &se of Cerror is i# a co#ditio#al directive. e.$. ) - 2define 34B56
"u#lic class 0y%lass { "u#lic static void 0ain() { 2if 34B56 2error 34B56 is defined 2endif . .

5line : ( Cli#e% modifies t!e compiler1s li#e #&mber a#d t!e file #ame o&tp&t for errors a#d war#i#$s. " so&rce code file may !ave a#y #&mber of Cli#e directives. e.$. ) - "u#lic class 0y%lass;
{ "u#lic static void 0ain() { 2line ;-int i; // error or &arning &ill #e, <%S-=>? on line ;--@ 2line ? c!ar c; // error or &arning &ill #e, <%S-=>? on line ?@ .

2. What is an e/ception? 2ow e/ceptions are handled in C@? Ans: "# e+ceptio# is a# erro#eo&s sit&atio# or &#e+pected be!avior t!at e#co&#tered d&ri#$ pro$ram e+ec&tio#. E+ceptio#al sit&atio#s arise w!e# a# operatio# ca##ot be completed #at&rally. @N

Basics of .NET [BSIT-61] D E+ceptio# Ra#dli#$ i# 3C provides a str&ct&red way of !a#dli#$ bot! system level a#d applicatio# level error co#ditio#s. F I# 3C% all e+ceptio#s m&st be represe#ted by a# i#sta#ce of a class type derived from System.E+ceptio#. F I# 3C% t!ere are try% catc! a#d fi#ally blocks are prese#t t!ere to !a#dle e+ceptio#s. " fi#ally block ca# be &sed to write termi#atio# code w!ic! e+ec&tes i# bot! #ormal e+ec&tio# a#d e+ceptio#al co#ditio#s. F I# 3C% system-level e+ceptio#s s&c! as overflow% divide-by-8ero a#d #&ll de-refere#ces !ave well defi#ed e+ceptio#. e.g.& ' 66 This program sho%s an exception if a variable is divided b# 3ero& ' &si#$ System, #amespace SystemUE+ceptio# L class "ddN&m L p&blic static void addU#&mbersDi#t #&m1%i#t #&m;= L try L i#t resT#&m1?#&m;, 3o#sole.5rite9i#eDres=, M catc!D.ivideByQeroE+ceptio# e= L 3o#sole.5rite9i#eD6E+ceptio# *cc&rredLNM7%e=, M 3o#sole.5rite9i#eD6"fter Ra#dli#$ t!e E+ceptio#7=, M p&blic static void ai#D= L "ddN&m.addU#&mbersD1N%N=, 3o#sole.>ead9i#eD=, M M M 3. What is an nsafe code? -/plain. Ans: Ensafe Code : ( 0#safe code are t!e codes% w!ere it is possible to declare a#d operate o# poi#ters% to perform co#versio#s betwee# poi#ters a#d i#te$ral types% to take t!e address of variables% a#d so fort!. I# a se#se% writi#$ &#safe code is m&c! like writi#$ 3 code wit!i# a 3C pro$ram. F 0#safe code is i# fact a 6safe7 feat&re from t!e perspective of bot! developers a#d &sers. F 0#safe code m&st be clearly marked wit! t!e modifier &#safe% so developers ca#`t possibly &se &#safe feat&res accide#tally% a#d t!e e+ec&tio# e#$i#e works to e#s&re t!at &#safe code ca##ot be e+ec&ted i# a# &#tr&sted e#viro#me#t. 4. What is threading? $ist o t the ad%antages and disad%antages of m ltiple threads. Ans: T!readi#$ is defi#ed as t!e e+ec&tio# pat! of a pro$ram. T!reads are &sed to r&# applicatio#s t!at perform lar$e a#d comple+ comp&tatio#s. T!reads are t!e basic &#it to w!ic! a# operati#$ system allocates processor time% a#d more t!a# o#e t!read ca# r&# i#side t!at process. 9d%antages of C ltiple .hreads6 ) i. 3a# comm&#icate over a #etwork% to a 5eb server a#d to a database. @1

Basics of .NET [BSIT-61] ii. -erform operatio#s t!at take a lar$e amo&#t of time. iii. .isti#$&is! tasks of varyi#$ priority. (or e+ample% a !i$!-priority t!read ma#a$es time-critical tasks% a#d a low-priority t!read performs ot!er tasks. iv. "llow t!e &ser i#terface to remai# respo#sive% w!ile allocati#$ time to back$ro&#d tasks. ,isad%antages of C ltiple .hreads6 ) i. emory re<&ireme#t for t!e co#te+t i#formatio# re<&ired by processes% "pplicatio# .omai# ob'ects a#d t!reads. T!erefore% t!e #&mber of processes% "pplicatio# .omai# ob'ects a#d t!reads w!ic! ca# be created is limited by memory availability. ii. ]eepi#$ track of a lar$e #&mber of t!reads co#s&mes si$#ifica#t processor time. If t!ere are too ma#y t!reads% t!e# t!ere will #ot be a#y si$#ifica#t adva#ta$es of t!reads. iii. 3o#trolli#$ e+ec&tio# of a# applicatio# wit! ma#y t!reads ca# be a so&rce of ma#y b&$s a#d it is very comple+. #. Write a note in $i!rar" (ase Classes. Ans: T!e .NET (ramework base classes i#cl&de classes% i#terfaces% a#d val&e types t!at provide access to system f&#ctio#ality. F To facilitate i#teroperability betwee# la#$&a$es% t!e .NET (ramework base classes are 39S complia#t a#d t!erefore ca# be &sed from a#y pro$rammi#$ la#$&a$e. F T!e .NET (ramework provides a ric! set of i#terfaces% abstract a#d co#crete classes. F 5e ca# &se t!e co#crete classes as is or ca# derive ow# classes from t!em. F To &se t!e f&#ctio#ality of a# i#terface% we ca# eit!er create a class t!at impleme#ts t!e i#terface or device a class from o#e of t!e i#terface of .NET (ramework classes. F It also follows #ami#$ co#ve#tio#s like .NET (ramework types &se a dot D.= sy#ta+ #ami#$ sc!eme t!at is i# a !ierarc!y. F T!ese also i#cl&de types t!at perform t!e followi#$ f&#ctio#s ) a. >eprese#t base data types a#d e+ceptio#s. b. E#caps&late data str&ct&res. c. I?* operatio#s. d. "ccess i#formatio# of types. e. .NET (ramework sec&rity c!ecks. f. -rovide data access% I0I a#d web services. &. What is the role of S"stem.:!1ect0 S"stem.String0 S"stem.9rra" and S"stem.Collections Classes? Ans: !"stem.2b-ect: ( System.*b'ect class is t!e &ltimate base class t!at all types directly or i#directly derive from. !"stem.!tring: ( I# 3C% a te+t stri#$ is stored i# a data type #amed stri#$% w!ic! is a# alias to System.Stri#$ type. !"stem.Arra": ( "rray types are refere#ce types derived from t!e abstract base type System."rray. !"stem.Collections: ( T!e System.3ollectio#s #amespace co#tai#s i#terfaces a#d classes t!at defi#e vario&s collectio#s of ob'ects% s&c! as lists% <&e&es% bit arrays% !as!tables a#d dictio#aries.

@;

Basics of .NET [BSIT-61] +. What are the different methods s pported !" S"stem.:!1ect class? 5i%e an e/ample for each. Ans: (ollowi#$ are t!e differe#t met!ods s&pported by S"stem.:!1ect ) a. The E6uals Method: ( T!is is a commo# operatio#% especially for searc!i#$ a#d sorti#$ i# collectio#s% it tests two ob'ects for e<&ality. T!e 71uals met!od of t!e ob8ect class provides a defa&lt impleme#tatio# t!at compares two refere#ce type ob'ects for refere#ce e<&ality. T!e 71uals met!od is virt&al% so derived refere#ce types may override it. e.$. ) - using System;
class 4m"loyee { string e7name; "u#lic 4m"loyee(string name) { e7name ( name; . . class 4quals3emonstration { static void 0ain() { 4quals3emonstration eq3emo ( ne& 4quals3emonstration(); eq3emo$Instance4qual(); %onsole$Read*ine(); . "u#lic void Instance4qual() { string name ( +Aitin+; 4m"loyee em"loyee= ( ne& 4m"loyee(name); 4m"loyee em"loyee; ( ne& 4m"loyee(name);

// com"aring references of se"arate instances #ool is4qual ( em"loyee=$4quals(em"loyee;); %onsole$)rite*ine(+em"loyee= (( em"loyee; B {-.+/ is4qual); em"loyee; ( em"loyee=; // com"aring references of t!e same instance is4qual ( em"loyee=$4quals(em"loyee;); %onsole$)rite*ine(+em"loyee= (( em"loyee; B {-.+/ is4qual); . .

b. The 'eferenceE6uals Method: ( t!e 71uals a#d "eference71uals met!ods are sema#tically e<&ivale#t% e+cept t!at t!e "eference71uals works o#ly o# ob'ect i#sta#ces. T!e "eference71uals met!od is static. e.$. ) - using System;
class 4m"loyee { string e7name; "u#lic 4m"loyee(string name) { e7name ( name; . . class Reference4quals3emo { static void 0ain() { Reference4quals3emo ref4q3em ( ne& Reference4quals3emo();

@@

Basics of .NET [BSIT-61]


ref4q3em$Instance4qual(); %onsole$Read*ine(); . "u#lic void Instance4qual() { string name ( +Coe+; 4m"loyee em"loyee= ( ne& 4m"loyee(name); 4m"loyee em"loyee; ( ne& 4m"loyee(name); // com"aring se"arate instances #ool is4qual ( D#Eect$Reference4quals(em"loyee=/ em"loyee;); %onsole$)rite*ine(+em"loyee= (( em"loyee; B {-.+/ is4qual); em"loyee; ( em"loyee=; // com"aring t!e same instance is4qual ( D#Eect$Reference4quals(em"loyee=/ em"loyee;); %onsole$)rite*ine(+em"loyee= (( em"loyee; B {-.+/ is4qual); . .

c. The To!tring Method: ( T!e p&rpose of t!e To$tring met!od is to ret&r# a stri#$ represe#tatio# of a type. T!e defa&lt impleme#tatio# i# t!e ob8ect class ret&r#s a stri#$ wit! t!e #ame of t!e ob'ect. T!e ToStri#$ met!od overrides ToStri#$ i# t!e ob8ect class. e.$.) - using System;
class 4m"loyee { string e7name; "u#lic 4m"loyee(string name) { e7name ( name; . // If oString() is not overridden/ t!e out"ut of t!is "rogram &ill #e <4m"loyee@$ "u#lic override string oString() { return String$Format(+G4m"loyee, {-.H+/ e7name); . . class oString3emonstration { static void 0ain() { 4m"loyee em" ( ne& 4m"loyee(+Coe+); %onsole$)rite*ine(em"$ oString()); %onsole$Read*ine(); . .

d. The /etT"pe Method: ( 9etT#pe is t!e basis for &si#$ reflectio# i# .NET. It ret&r#s a T#pe ob'ect% describi#$ t!e ob'ect it was called o#. It ca# t!e# be &sed to e+tract type member data like met!ods a#d fields t!at may s&bse<&e#tly be &sed for late-bo&#d met!od i#vocatio#s. T!e 9etT#pe met!od is also &sef&l if we $et a# ob'ect at r&#time a#d we do #ot k#ow w!at its type is. e.$.) - using System;
class 4m"loyee { . class 6et y"e3emonstration { static void 0ain() {

@E

Basics of .NET [BSIT-61]


o#Eect em"= ( ne& 4m"loyee(); 4m"loyee em"; ( ne& 4m"loyee(); %onsole$)rite*ine(em"=$6et y"e()); %onsole$)rite*ine(em";$6et y"e()); %onsole$Read*ine(); . .

e. The /et7ashCode Method: ( T!e 9et:ash ode met!od makes a#y ob'ect &sable i# a :ashtable or a#y !as!i#$ al$orit!m. Si#ce t!e defa&lt al$orit!m s&pplied by t!e 9et:ash ode met!od of t!e ob8ect class is #ot $&ara#teed to be &#i<&e% we s!o&ld override 9et:ash ode i# o&r c&stom types. e.$. ) - using System;
class 4m"loyee { string e7name; "u#lic 4m"loyee(string name) { e7name ( name; . "u#lic override int 6etIas!%ode() { string uniqueString ( oString(); return uniqueString$6etIas!%ode(); . "u#lic override string oString() { return String$Format(+G4m"loyee, {-.H+/ e7name); . . class 6etIas!%ode3emonstration { static void 0ain() { 4m"loyee em" ( ne& 4m"loyee(+Coe+); %onsole$)rite*ine(em"$6etIas!%ode()); %onsole$Read*ine(); . .

f. The Member&iseClone Method: ( 5!e#ever we #eed to create a bitwise copy Ds!allow copy= of o&r type% t!e 0ember%ise lone met!od is &sed. "s s&c!% if we perform a 0ember%ise lone o# o&r class% it will make a copy of t!e type a#d all co#tai#ed val&e types a#d refere#ces types. Rowever% it will #ot copy t!e ob'ects t!at t!e refere#ce type members i# o&r type refer to. T!is be!avior of o#ly maki#$ a copy of t!e first level of o&r type tells t!e reaso# w!y a 0ember%ise lone is called a s!allow copy. Si#ce t!e 0ember%ise lone met!od is #ot virt&al% we ca# #ot override it i# derived classes. e.$.) - using System;
"u#lic class Jddress { . class 4m"loyee { Jddress e7address ( ne& Jddress(); string e7name; "u#lic 4m"loyee(string name) { e7name ( name; . "u#lic 4m"loyee S!allo&%o"y() {

@H

Basics of .NET [BSIT-61]


return (4m"loyee)0em#er&ise%lone(); . "u#lic Jddress 4m"loyeeJddress { get { return e7address; . . . class 0em#er&ise%lone3emonstration { static void 0ain() { 4m"loyee em"= ( ne& 4m"loyee(+Coe+); 4m"loyee em"; ( em"=$S!allo&%o"y(); // com"are 4m"loyee references #ool is4qual ( D#Eect$Reference4quals(em"=/ em";); %onsole$)rite*ine(+em"= (( em"; B {-.+/ is4qual); //com"are reference of Jddress o#Eect in eac! 4m"loyee o#Eect is4qual ( D#Eect$Reference4quals(em"=$4m"loyeeJddress/em";$4m"loyeeJddress); %onsole$)rite*ine(+em"=$4m"loyeeJddress (( em";$4m"loyeeJddress B {-.+/ is4qual); %onsole$Read*ine(); .

7. What is !o/ing and n!o/ing? -/plain with an e/ample. Ans: (o/ing : ( Bo+i#$ is a# implicit co#versio# of a val&e type ob'ect or to a#y i#terface type impleme#ted by t!is val&e type. Bo+i#$ a val&e of a val&e allocates a# ob'ect i#sta#ce a#d copies t!e val&e i#to t!e #ew ob'ect. e.$.) - // Boxing$cs
// Boxing an integer varia#le using System; class estBoxing { "u#lic static void 0ain() { int i ( =;K; o#Eect o ( i; // Im"licit #oxing i ( L1>; // %!ange t!e contents of i %onsole$)rite*ine(+ !e valueBty"e value ( {-.+/ i); %onsole$)rite*ine(+ !e o#EectBty"e value ( {-.+/ o); . .

2utput ) - T!e val&e-typeTEH6 a#d T!e ob'ect-typeT1;@ ?P T!is e+ample co#verts a# i#te$er variable i to a# ob'ect o via bo+i#$. T!e# t!e val&e stored i# t!e variable i is c!a#$ed from 1;@ to EH6. T!e e+ample s!ows t!at t!e ob'ect keeps t!e ori$i#al copy of t!e co#te#ts% 1;@. P? En!o/ing :(0#bo+i#$ is a# e+plicit co#versio# from t!e type ob'ect to a val&e type or from a# refere#ce type to a val&e type t!at impleme#ts t!e i#terface. "# &#bo+i#$ operatio# co#sists of) a. 3!ecki#$ t!e ob'ect i#sta#ce to make s&re it is a bo+ed val&e of t!e $ive# val&e type. b. 3opyi#$ t!e val&e from t!e i#sta#ce i#to t!e val&e-type variable. e.$. ) - using System;
"u#lic class 5n#oxing est {

@6

Basics of .NET [BSIT-61]


"u#lic static void 0ain() { int intI ( =;K; // Boxing o#Eect o ( intI; // Reference to incom"ati#le o#Eect "roduces Invalid%ast4xce"tion try { /' int intC ( (s!ort)o; 3is"lay t!e System$Invalid%ast4xce"tion at 5n#oxing est$0ain() 4rror , Incorrect 5n#oxing '/ int intC ( (int)o;// 3is"lay t!e Dut"ut &it!out 4xce"tion %onsole$)rite*ine(+5n#oxing DM$+); . catc! (Invalid%ast4xce"tion e) { %onsole$)rite*ine(+{-. 4rror, Incorrect un#oxing$+/ e); .

. .

2utput ) - 0#bo+i#$ *]. ?P T!e above me#tio#ed e+ample demo#strates a case of i#valid &#bo+i#$% of !ow i#correct &#bo+i#$ leads to I#valid3astE+ceptio#. By &si#$ try a#d catc!% a# error messa$e is dispalyed w!e# t!e error occ&rs P? ;. Write a program to replace all =r? !" =4? in the string Frose is a roseG. Ans: using System;
class %!ange%ase { static void 0ain(stringGH args) { string str ( +rose is a rose+; str(str$Re"lace(+r+/+R+); %onsole$)rite*ine(str); %onsole$Read*ine(); .

1<. Write a note on a. !ingle($imensional Arra"s Ans: T!is array co#tai#s t!e eleme#t from arr[N] to arr[#]. T!e #ew operator is &sed to create t!e array a#d i#itiali8e t!e array eleme#ts to t!eir defa&lt val&es. I# t!e followi#$ e+ample% all t!e array eleme#ts are i#itiali8ed to 8ero. e.$. ) - using System;
"u#lic class Single3imensionalJrray%lass { static void PrintJrray(stringGH &) { for (int i ( -; i N &$*engt!; iOO) %onsole$)rite(&GiH O +{-.+/ i N &$*engt! B = P + + , ++); %onsole$)rite*ine(); .

"u#lic static void 0ain() { // 3eclare and initialiQe an array,

@J

Basics of .NET [BSIT-61]


stringGH )eeR3ays ( ne& stringGH { +Sun+/ +Sat+/ +0on+/ + ue+/ +)ed+/ + !u+/ +Fri+ .; // Pass t!e array as a "arameter, PrintJrray()eeR3ays); . .

b. Multidimensional Arra"s Ans: "rrays ca# !ave more t!a# o#e dime#sio#. e.$. ) - using System;
"u#lic class 0ulti3imensionalJrray%lass { static void PrintJrray(intG/H &) { // 3is"lay t!e array elements, for (int i ( -; i N L; iOO) for (int E ( -; E N ;; EOO) %onsole$)rite*ine(+4lement({-./{=.)({;.+/ i/ E/ &Gi/ EH); . "u#lic static void 0ain() { // Pass t!e array as a "arameter, PrintJrray(ne& intG/H { { =/ ; ./ { K/ L ./ { 1/ > ./ { 8/ ? . .); .

c. ,agged Arra"s Ans: " 'a$$ed array is a# array w!ose eleme#ts are arrays. T!e eleme#ts of a 'a$$ed array ca# be differe#t dime#sio# a#d si8es. " 'a$$ed array is sometimes called a# 6array-of-arrays7. e.$. ) - using System;
"u#lic class CaggedJrray est { "u#lic static void 0ain() { // 3eclare t!e array of t&o elements, intGHGH myJrray ( ne& intG;HGH; // InitialiQe t!e elements, myJrrayG-H ( ne& intG1H { =/ K/ 1/ 8/ S .; myJrrayG=H ( ne& intGLH { ;/ L/ >/ ? .; // 3is"lay t!e array elements, for (int i ( -; i N myJrray$*engt!; iOO) { %onsole$)rite(+4lement({-.), +/ i); for (int E ( -; E N myJrrayGiH$*engt!; EOO) %onsole$)rite(+{-.{=.+/ myJrrayGiHGEH/ E (( (myJrrayGiH$*engt! B =) P ++ , + +); . . . %onsole$)rite*ine();

@K

Basics of .NET [BSIT-61] d. Passing Arra"s 4sing ref and out Ans: 9ike all o&t parameters% a# o&t parameter of a# array type m&st be assi$#ed before it is &sed, t!at is% it m&st be assi$#ed by t!e caller. I# case of ref parameters% a ref parameter of a# array type m&st be defi#itely assi$#ed by t!e caller. T!erefore% t!ere is #o #eed to be defi#itely assi$#ed by t!e caller. " ref parameter of a# array type may be altered as a res&lt of t!e call. (or e+ample% t!e array ca# be assi$#ed t!e #&ll val&e or ca# be i#itiali8ed to a differe#t array. e.$. ) using System;
class PassingJrray5sing7Ref7and7Dut { "u#lic static void FillJrray(ref intGH arr) { // %reate t!e array on demand, if (arr (( null) arr ( ne& intG=-H; // Dt!er&ise fill t!e array, arrG-H ( =;K; arrGLH ( =-;L; . static "u#lic void 0ain() { // InitialiQe t!e array, intGH myJrray ( { =/ ;/ K/ L/ 1 .; // Pass t!e array using ref, FillJrray(ref myJrray); // 3is"lay t!e u"dated array, %onsole$)rite*ine(+Jrray elements are,+); for (int i ( -; i N myJrray$*engt!; iOO) %onsole$)rite*ine(myJrrayGiH); . .

11. What is the se of S"stem.Collections class? Ans: System.3ollectio#s class is a #amespace w!ic! co#tai#s i#terfaces a#d classes t!at defi#e vario&s collectio#s of ob'ects% s&c! as lists% <&e&es% bit arrays% !as!tables a#d dictio#aries. F Eac! class !as a set of met!ods a#d properties% w!ic! are &sed o# t!ose correspo#di#$ class ob'ects.

@A

Basics of .NET [BSIT-61] Chapter + 1. 5i%e the o%erall concept of 9,:.N-. with the help of fig res. '2int6 (ig pict re* Ans:

F "ctiveB .ata *b'ects for t!e .NET (ramework is a set of classes t!at provide data access services to t!e .NET pro$rammer. F It is a# i#te$ral part of .NET (ramework% providi#$ access to relatio#al database% B 9 a#d applicatio# data. F It is based o# a# ob'ect model t!at is based o# t!e sta#dards laid dow# by 5orld 5ide 5eb 3o#sorti&m. F It provides a ric! set of compo#e#ts for creati#$ distrib&ted% data-s!ari#$ applicatio#s. F It s&pports a variety of developme#t #eeds% i#cl&di#$ t!e creatio# of fro#t e#d database clie#ts a#d middle tier b&si#ess ob'ects &sed by applicatio#s% tools% la#$&a$es or I#ter#et browsers. F ".* .NET provides co#siste#t access to data so&rces s&c! as S Sa9 Server% or data so&rces e+posed via *9E .B a#d B 9. .ata S!ari#$ applicatio#s ca# &se ".* .NET to co##ect to t!ese data so&rces a#d retrieve% ma#ip&late a#d &pdate data. F By &si#$ ".* .NET data ca# be retrieved from o#e data so&rce a#d saved i# a#ot!er. (or e+ample% data ca# be retrieved from icrosoft E+cel a#d t!e# saved i# a# B 9 doc&me#t. F I# t!e ".* .NET ob'ect model% t!e data residi#$ i# a database is retrieved t!ro&$! a data provider. F T!e .ata -rovider is a set of compo#e#ts i#cl&di#$ t!e co##ectio#% comma#d% data reader a#d data adapter ob'ects. F It provides data to t!e applicatio#% a#d &pdates t!e database wit! t!e c!a#$es made i# t!e applicatio#. F "# applicatio# ca# access data eit!er t!ro&$! a dataset or t!ro&$! a data reader ob'ect. F ".* .NET also e#ables to create a# B 9 represe#tatio# of a dataset. F I# a# B 9 represe#tatio# of a dataset% data is writte# i# B 9 format% a#d dataset sc!ema is writte# by &si#$ t!e B 9 Sc!ema defi#itio# la#$&a$e DBS.=. 0 The t&o .e" components of the A$2 .NET are: ( a. Data ;rovider b. Dataset

EN

Basics of .NET [BSIT-61] 4 The four (e# components of a data provider are& ' <. Connection 7stablishes a connection to a specified data source. =. Command 7xecutes a command against a data source. >. ,ata 4eader "eads a for%ard'onl#, read'onl# stream of data from a data source ?. ,ata 9dapter ;opulates a Data$et and resolves updates %ith a data source. 4 The (e# components of Dataset are& ' <. ,ata.a!leCollection 6 ) It contains all the tables retrieved from the Data source. =. ,ata4elationCollection 6 ) It contains relationship and lin(s bet%een tables in a Dataset. >. ,ata.a!le 6 ) It represents a table in the data table ollection of a dataset. ?. ,ata4owCollection 6 ) It contains all the ro%s in a data table. @. ,ataCol mnCollection 6 ) It contains all the columns in a data table. F "# applicatio# ca# access data eit!er t!ro&$! a dataset or t!ro&$! a data reader ob'ect) 4sing a $ataset: ( I# t!is case% t!e data is cac!ed i# a dataset a#d t!e applicatio# accesses t!e data from t!e dataset. 4sing a $ata reader: ( I# t!is met!od% a data reader ob'ect% w!ic! is a compo#e#t of t!e data provider% &ses t!e co##ectio# ob'ect to co##ect to t!e database% &ses t!e comma#d ob'ect to retrieve data% a#d provides data to t!e applicatio# i# a read-o#ly a#d forward-o#ly mode. 2. -/plain in detail 9,:.N-. architect re. Ans:

3. What is a data pro%ider? -/plain. Ans: ,ata 3ro%ider : ( " .ata -rovider is &sed for co##ecti#$ to a database% retrievi#$ data% stori#$ t!e data i# a dataset% readi#$ t!e retrieved data% a#d &pdati#$ database. F Selecti#$ a# appropriate data provider for a clie#t applicatio# depe#ds o# t!e type of data so&rces t!at is bei#$ accessed. F T!e fo&r mai# types of data providers are) a. .NET 3rame&or. data pro ider for !8L !er er : ( T!e System..ata.dll assembly impleme#ts Sa9 Server .NET (ramework data provider i# t!e S"stem.,ata.SAlClient #amespace a#d it specifically worked wit! icrosoft Sa9 Server. b. .NET 3rame&or. data pro ider for 2LE $+ : ( T!is type of data provider works wit! t!e *9E .B provider% *racle *9E .B provider% a#d t!e Get *9E .B provider. T!e System..ata.dll assembly impleme#ts *9E .B .NET (ramework data provider i# t!e S"stem.,ata.:le,! #amespace. c. .NET 3rame&or. data pro ider for 2$+C : ( To &se t!is type of provider% we m&st &se a# *.B3 driver. T!e S"stem.,ata.:,(C.dll assembly impleme#ts *.B3 .NET (ramework data provider. d. .NET 3rame&or. data pro ider for 2racle : ( *racle clie#t software m&st be i#stalled o# t!e system before we &se t!e provider to co##ect to a# *racle data so&rce. T!e System..ata.*racle3lie#t.dll assembly impleme#ts t!e *racle .NET (ramework data provider i# t!e S"stem.,ata.:racleClient #amespace. F T!e .NET data provider is desi$#ed to be li$!twei$!t% creati#$ a mi#imal layer betwee# t!e data so&rce a#d applicatio# code% i#creasi#$ performa#ce wit!o&t sacrifici#$ f&#ctio#ality. F T!e fo&r mai# compo#e#ts of .NET data provider ) 2b-ect @. Connection A. Command B. ,ata 4eader $escription 7stablishes a connection to a specified data source. 7xecutes a command against a data source. "eads a for%ard'onl#, read'onl# stream of data from a data source E1

C. ,ata 9dapter

Basics of .NET [BSIT-61] ;opulates a Data$et and resolves updates %ith a data source.

4. What is a data set? -/plain Ans: ,ataset : ( T!e .ataset is a memory based relatio#al represe#tatio# of data. " .ataset is a part of t!e disco##ected e#viro#me#t. F " dataset is a disco##ected% cac!ed set of records t!at are retrieved from a database. F " dataset co#tai#s a collectio# of o#e or more .ataTable ob'ects made &p of rows a#d col&m#s of data as well as co#strai#t a#d relatio# i#formatio# abo&t data i# t!e .ataTable ob'ects. F " .ataset is prese#t i# t!e .ataset class i# t!e System..ata #amespace. T!e key compo#e#ts of a .ataset are ) A. ,ata.a!leCollection 6 ) It contains all the tables retrieved from the Datasource. B. ,ata4elationCollection 6 ) It contains relationship and lin(s bet%een tables in a Dataset. C. ,ata.a!le 6 ) It represents a table in the datatable ollection of a dataset. D. ,ata4owCollection 6 ) It contains all the ro%s in a datatable. <E. ,ataCol mnCollection 6 ) It contains all the columns in a datatable. #. Write a program to show the demonstration of 9,:.N-.. Ans: using System;
using System$%ollections$6eneric; using System$ ext; using System$3ata$Sql%lient; names"ace 3emo7J3D7A4 { class Program { static void 0ain(stringGH args) { string connectionString ( +3ata Source(AB0SMA;Initial %atalog(Jdventure)orRs; 5ser id(sa;Pass&ord(AB0SMAS+; string sST* ( +Select ' from IumanResources$4m"loyee+; Sql%onnection cn ( ne& Sql%onnection(connectionString); cn$D"en(); Sql%ommand cmd ( ne& Sql%ommand(sST*/cn); Sql3ataReader dr ( cmd$4xecuteReader(); &!ile (dr$Read()) { %onsole$)rite*ine(drG-HO++OdrG=H); . cn$%lose(); %onsole$Read*ine(); . . .

E;

Basics of .NET [BSIT-61] Chapter 7 1. What is a we! ser%ice? -/plain its working. Ans: #eb !er ice: ( " 5eb service is a very simple idea. It is a applicatio# lo$ic% accessible t!ro&$! some sta#dard 5eb protocol. F " 5eb service is '&st betwee# comp&ter to comp&ter applicatio#s. F It is like a pro$ram o# o#e comp&ter calli#$ a met!od t!at e+ists o# a#ot!er comp&ter a#d calli#$ it t!ro&$! t!e I#ter#et. F " 5eb service does #ot !ave &ser i#terface. " 5eb service is desi$#ed to i#teract wit! applicatio#s. 2. Who does the we! ser%ice differ from we! page? Ans: 3. With a neat diagram e/plain the e%ent handling process of win formsHwe! forms. Ans:

NUV Page *anguage(+%2+ Juto4vent)ireu"(+true+ In!erits(+73efault+ UW

%odeFile(+3efault$as"x$cs+

N:3D% XP4 !tml P5B*I% +B//)K%//3 3 YI 0* =$- ransitional//4A+ +!tt",//&&&$&K$org/ R/x!tml=/3 3/x!tml=Btransitional$dtd+W N!tmlW N!eadW N/!eadW Nscri"t language(+%2+ runat(+server+W void Su#mitBtn7%licR(D#Eect sender/ 4ventJrgs e) { 0essage$ ext ( +Ii + O Itt"5tility$Itml4ncode(Aame$ ext) O +/ Selected, + O %ategory$SelectedItem; . N/scri"tW N#odyW NcenterW Nform action(+3efault$as"x+ met!od(+"ost+ runat(+server+W N!KW Aame, Nas",text#ox id(+Aame+ runat(+server+/W

!anRs::::: Xou !ave

E@

Basics of .NET [BSIT-61]


%ategory, Nas",dro"do&nlist id(+%ategory+ runat(+server+W Nas",listitemW DXSN/as",listitemW Nas",listitemWSPDR S MI N/as",listitemW Nas",listitemWF*D)4RSN/as",listitemW N/as",dro"do&nlistW N/!KW Nas",#utton text(+*ooRu"+ Dn%licR(+Su#mitBtn7%licR+ runat(+server+/W Nas",la#el id(+0essage+ runat(+server+/W N/formW N/centerW N/#odyW N/!tmlW

0 Eac! "S-.NET server co#trol is capable of e+posi#$ a# ob'ect model co#tai#i#$ properties% met!ods a#d eve#ts. "S-.NET developers ca# &se s&c! ob'ect model to clea#ly modify a#d i#teract wit! t!e pa$e. F T!e above me#tio#ed e+ample demo#strates !ow a# "S-.NET pa$e developer ca# !a#dle t!e *#3lick eve#t from t!e Nas",#utton runat(+server+W co#trol to ma#ip&late t!e te+t property of t!e Nas",la#el runat(+server+/W co#trol. F I# t!is e+ample% w!e# t!is web form loads o# t!e web browser% t!ere is two 9abel% o#e Te+tbo+% o#e .rop.ow#9ist% o#e B&tto#. F 5!e# t!e 0SE> select a#y item prese#t i# t!e .rop.ow#9ist a#d click o# t!e B&tto#% it displays a messa$e alo#$ wit! t!e #ame a#d t!e selected item from t!e dropdow#list. F T!is eve#t occ&rs d&e to "S-.NET server co#trols w!ic! !a#dles it. 4. What is 9S3.N-.? Ans: "S-.NET is a# e#tirely #ew paradi$m for server-side "S- scripti#$. F "S-.NET is a &#ified 5eb developme#t platform t!at provides t!e services #ecessary for &s to b&ild e#terprise 5eb applicatio#s. F "S-.NET is lar$ely sy#ta+ compatible wit! "ctive Server -a$es% it also provides a #ew pro$rammi#$ model a#d i#frastr&ct&re t!at allow &s to create sec&re% scalable% stable a#d powerf&l applicatio#s. F "S-.NET is a compiled% .NET-based e#viro#me#t, a#d allows &s to take f&ll adva#ta$e of t!e feat&res of t!e 39>% s&c! as type safety% i#!erita#ce% la#$&a$e i#teroperability a#d versio#i#$. F "S-.NET !as bee# desi$#ed to work wit! 65!at Oo& See Is 5!at Oo& Iet7 D5OSI5OI= RT 9 editors a#d ot!er pro$rammi#$ tools% i#cl&di#$ icrosoft 2is&al St&dio .NET. #. What is the se of 9S3.N-.? Ans: "S-.NET is precompiled code so it become very easy to &se it i# web applicatio#s developme#t. F 5e ca# easily &se t!e be#efits of t!ese tec!#olo$ies% w!ic! i#cl&de ma#a$ed 39> e#viro#me#t% type safety% I#!erita#ce% la#$&a$e i#teroperability a#d versio#i#$. &. Write a program to displa" FWelcome to 9S3.N-.G 7 times in increasing order of their font siIe sing 9S3.N-.. Ans: NUV Page *anguage(+%2+ Juto4vent)ireu"(+true+ %odeFile(+3efault$as"x$cs+
In!erits(+73efault+ UW N:3D% XP4 !tml P5B*I% +B//)K%//3 3 YI 0* =$- ransitional//4A+ +!tt",//&&&$&K$org/ R/x!tml=/3 3/x!tml=Btransitional$dtd+W N!tml xmlns(+!tt",//&&&$&K$org/=SSS/x!tml+ W N!ead runat(+server+WNtitleW5ntitled PageN/titleWN/!eadW N#odyW NcenterW Nform id(+form=+ runat(+server+W NdivW NUfor (int i(-; iN?;iOO){UW

EE

Basics of .NET [BSIT-61]


Nfont siQe(+NU(iUW+W)elcome to t!e JSP $A4 N/fontWN#r /W NU.UW N/divW N/formW N/centerW N/#odyW N/!tmlW

+. What is the role of S"stem.We!? Ans: T!e System.5eb #amespace s&pplies classes a#d i#terface t!at e#able browser a#d server comm&#icatio#. F T!is #amespace i#cl&des t!e Rttp>e<&est classes w!ic! provides e+te#sive i#formatio# abo&t t!e c&rre#t RTT- re<&est% t!e RTT- respo#se class w!ic! ma#a$es RTT- o&tp&t to t!e clie#t a#d RttpServer0tility class w!ic! provides access to server-side &tilities a#d processes. F System.5eb also i#cl&des classes for cookie ma#ip&latio#% file tra#sfer% e+ceptio# i#formatio# a#d o&tp&t cac!e co#trol. 7. Write a program to demonstrate handling of ser%er control e%ents. Ans: NUV Page *anguage(+%2+ Juto4vent)ireu"(+true+ %odeFile(+3efault$as"x$cs+
In!erits(+73efault+ UW N:3D% XP4 !tml P5B*I% +B//)K%//3 3 YI 0* =$- ransitional//4A+ +!tt",//&&&$&K$org/ R/x!tml=/3 3/x!tml=Btransitional$dtd+W N!tmlW N!eadW N/!eadW Nscri"t language(+%2+ runat(+server+W void Su#mitBtn7%licR(D#Eect sender/ 4ventJrgs e) { 0essage$ ext ( +Ii + O Itt"5tility$Itml4ncode(Aame$ ext) O +/ !anRs::::: Xou !ave Selected, + O %ategory$SelectedItem; . N/scri"tW N#odyW NcenterW Nform action(+3efault$as"x+ met!od(+"ost+ runat(+server+W N!KW Aame, Nas",text#ox id(+Aame+ runat(+server+/W %ategory, Nas",dro"do&nlist id(+%ategory+ runat(+server+W Nas",listitemW DXSN/as",listitemW Nas",listitemWSPDR S MI N/as",listitemW Nas",listitemWF*D)4RSN/as",listitemW N/as",dro"do&nlistW N/!KW Nas",#utton text(+*ooRu"+ Dn%licR(+Su#mitBtn7%licR+ runat(+server+/W Nas",la#el id(+0essage+ runat(+server+/W N/formW N/centerW N/#odyW N/!tmlW

EH

Basics of .NET [BSIT-61] Chapter ; 1. What is a .N-. remoting? Ans: .NET >emoti#$ e#ables comm&#icatio# betwee# applicatio#s. F .iffere#t applicatio#s ca# comm&#icate wit! o#e a#ot!er% w!et!er t!ose applicatio#s reside o# t!e same comp&ter% o# differe#t comp&ters i# t!e same local area #etwork% or across t!e world i# very differe#t #etworks a#d eve# if t!e comp&ters r&# differe#t operati#$ systems. F T!e .NET (ramework provides a #&mber of services s&c! as activatio# a#d lifetime co#trol. F It also provides comm&#icatio# c!a##els respo#sible for tra#sporti#$ messa$es to a#d from remote applicatio#s. F It ca# also &se B 9 e#codi#$ w!e# i#teroperability wit! ot!er remoti#$ systems is esse#tial. F >emoti#$ was desi$#ed wit! sec&rity i# mi#d% so we ca# seriali8e streams i# order to sec&re t!em before t!ey are tra#sported over t!e c!a##el. 2. 2ow is .N-. remoting different from we! ser%ices and ,C:C? Ans: .NET 'emoting ersus $istributed C2M )$C2M* 1 I# t!e earlier days% I#ter--rocess comm&#icatio# betwee# applicatio#s was !a#dled t!ro&$! .istrib&ted 3ompo#e#t *b'ect odelD.3* =. .3* works well a#d t!e performa#ce is ade<&ate w!e# applicatio#s e+ist o# comp&ters of similar type o# t!e same #etwork. 0 7o&e er9 $C2M has t&o ma-or dra&bac.s 1 a. It relies o# a proprietary bi#ary protocol t!at #ot all ob'ect models s&pport. b. It also wa#ts to comm&#icate over a ra#$e of ports t!at are typically blocked by firewalls. .N-. 4emoting eliminates the diffic lties of ,C:C by supporting different transport protocol formats and communication protocols. T!is allows .NET >emoti#$ to be adaptable to t!e #etwork e#viro#me#t i# w!ic! it is bei#$ &sed. .NET 'emoting ersus #eb !er ices 1 5eb services fall &#der t!e roof of .NET >emoti#$% b&t web services !ave a simplified pro$rammi#$ model a#d are i#te#ded for a wide tar$et a&die#ce. F 5eb services i#volve allowi#$ applicatio#s to e+c!a#$e messa$es t!at may be platform i#depe#de#t a#d pro$rammi#$ la#$&a$e i#depe#de#t. F 5eb services k#ow #ot!i#$ abo&t t!e clie#t t!at is maki#$ t!e re<&est. T!e clie#ts comm&#icate by tra#sferri#$ messa$es back a#d fort! i# a specific format k#ow# as t!e Simple *b'ect "ccess -rotocol% or S*"- T!e followi#$ list $ives some of t!e ma'or differe#ces betwee# .NET >emoti#$ a#d 5eb services) "S-.NET based 5eb services ca# o#ly be accessed over RTT-. 5!ereas t!e .NET >emoti#$ ca# be &sed across a#y protocol. 5eb services work i# a stateless e#viro#me#t w!ere eac! re<&est res&lts i# a #ew ob'ect created to service t!e re<&est. .NET >emoti#$ s&pports state ma#a$eme#t optio#s a#d ca# ide#tify m&ltiple calls from t!e same clie#t. 5eb services seriali8e ob'ects t!ro&$! B 9 co#tai#ed i# t!e S*"- messa$es a#d ca# t!&s o#ly !a#dle items t!at ca# be f&lly e+pressed i# B 9. .NET >emoti#$ relies o# t!e e+iste#ce of t!e metadata wit!i# assemblies t!at co#tai# i#formatio# abo&t data types. T!is limited metadata i#formatio# is passed abo&t a# ob'ect% w!e# it is passed by refere#ce. 5eb services s&pport i#teroperability across platforms a#d are $ood for !etero$e#eo&s e#viro#me#ts. .NET >emoti#$ re<&ires t!e clie#ts be b&ilt &si#$ .NET% w!ic! mea#s a !omo$e#eo&s e#viro#me#t. 3. -/plain remoting architect re. Ans: T!e >emoti#$ "rc!itect&re% prese#ts to t!e pro$rammer a# eve# simple proced&re. If we co#fi$&re t!e clie#t properly% we #eed o#ly creates a #ew i#sta#ce of t!e remote ob'ect &si#$ new. F 3lie#t receives a refere#ce to t!e server ob'ect% a#d t!e# call its met!ods as t!o&$! t!e ob'ect were i# E6

Basics of .NET [BSIT-61] process rat!er t!a# r&##i#$ o# a separate comp&ter. F T!e remoti#$ system &ses pro+y ob'ects to create t!e impressio# t!at t!e server ob'ect is i# t!e clie#t1s process. F -ro+ies are sta#d-i# ob'ects t!at prese#t t!emselves as some ot!er ob'ect. 5!e# clie#t creates a# i#sta#ce of t!e remote type% t!e remoti#$ i#frastr&ct&re creates a pro+y ob'ect t!at looks e+actly like t!e remote type. F 3lie#t calls a met!od o# t!at pro+y% a#d t!e remoti#$ system receives t!e call% ro&tes it to t!e server process% i#vokes t!e server ob'ect% a#d ret&r#s t!e ret&r# val&e to t!e clie#t pro+y% w!ic! ret&r#s t!e res&lt to t!e clie#t. F >emote calls m&st be co#veyed i# some way betwee# t!e clie#t a#d server process. F (or b&ildi#$ a remoti#$ system% we mi$!t start by lear#i#$ #etwork pro$rammi#$ a#d a wide array of protocols a#d seriali8atio# format specificatio#s. F .NET remoti#$ system% t!e combi#atio# of &#derlyi#$ tec!#olo$ies re<&ired to ope# a #etwork co##ectio# a#d &se a partic&lar protocol to se#d t!e bytes to t!e receivi#$ applicatio# are represe#ted as a tra#sport c!a##el. F " channel is a type t!at takes a stream of data% creates a packa$e accordi#$ to a partic&lar #etwork protocol% a#d se#ds t!e packa$e to a#ot!er comp&ter. F Some c!a##els ca# o#ly receive i#formatio#% ot!ers ca# o#ly se#d i#formatio#% a#d still ot!ers% s&c! as t!e defa&lt Tcp3!a##el a#d Rttp3!a##el classes% ca# be &sed i# bot! directio#s. 4. With a neat diagram e/plain the remoting process. Ans:

S&ppose we !ave a# applicatio# r&##i#$ o# o#e comp&ter% a#d we wa#t to &se t!e f&#ctio#ality t!at is stored o# a#ot!er comp&ter. T!e above me#tio#ed s!ows t!e $e#eral remoti#$ process. F 5!e# bot! t!e server a#d clie#t are co#fi$&red properly% a clie#t creates a #ew i#sta#ce of t!e server class. F T!e remoti#$ system creates a pro+y ob'ect t!at represe#ts t!e class a#d ret&r#s to t!e clie#t ob'ect a refere#ce to t!e pro+y. F 5!e# a clie#t calls a met!od% t!e remoti#$ system c!ecks t!e type i#formatio#% a#d se#ds t!e call over t!e c!a##el to t!e server process. F " liste#i#$ c!a##el picks &p t!e re<&est a#d forwards it to t!e server remoti#$ system% w!ic! locates a#d calls t!e re<&ested ob'ect. F T!e process is t!e# reversed% as t!e server remoti#$ system b&#dles t!e respo#se i#to a messa$e a#d t!e server c!a##el se#ds to t!e clie#t c!a##el. F (i#ally% t!e clie#t remoti#$ system ret&r#s t!e res&lt of t!e call to t!e clie#t ob'ect t!ro&$! t!e pro+y.

EJ

Basics of .NET [BSIT-61] #. -/plain the steps or phases in%ol%ed in implementing .N-. remoting applications. Ans: &. Write a program to displa" F2ello WorldG string to demonstrate .N-. remoting. Ans: &si#$ System, &si#$ System.>&#time.>emoti#$, &si#$ System.>&#time.>emoti#$.3!a##els, &si#$ System.>&#time.>emoti#$.3!a##els.Tcp, #amespace >emoti#$ L ?? Sample remote ob'ect p&blic class Sample*b'ect ) ars!alBy>ef*b'ect L ?? 3o#str&ctor p&blic Sample*b'ectD= L M ?? >et&r# a Rello world messa$e p&blic stri#$ Rello5orldD= L ret&r# 6Rello 5orldZ7, M M M

EK

You might also like