basics of .net for kuvempu university

Upload: saurabh-varshney

Post on 04-Jun-2018

217 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/13/2019 Basics of .Net for Kuvempu University

    1/48

    Basics of .NET [BSIT-61

    Chapter 1

    1. What is .Net Framework?

    Ans:.Net is framework developed by icrosoft.

    It comprises wit!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!a

    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 $rod 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#termediatela#$&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 !avereaso#able amot 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#$. 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!ats 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 5i#(orms7.

    iii. Code Execution) - T!e i#teresti#$ t!i#$ abo&t code e+ec&tio# o# .NET framework is% t!e sta#dardi8edcommo# 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

  • 8/13/2019 Basics of .Net for Kuvempu University

    2/48

    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 $rod 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 amot 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#$. 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!ats 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 5i#(orms7.

    c.Code Execution) - T!e i#teresti#$ t!i#$ abo&t code e+ec&tio# o# .NET framework is% t!e sta#dardi8edcommo# 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!isi#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 spported !" .Net framework?

    Ans:latform tec!#olo$ies s&pported by .Net framework are : "* .Net% I#ter#et tec!#olo$ies a#d 0se

    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 amot 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#$. 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!ats 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 5i#(orms7.

    ;

  • 8/13/2019 Basics of .Net for Kuvempu University

    3/48

    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 ot 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#$ atask.

    (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

  • 8/13/2019 Basics of .Net for Kuvempu University

    4/48

    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 todays 5i#dows % 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 spport !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.

  • 8/13/2019 Basics of .Net for Kuvempu University

    5/48

    Basics of .NET [BSIT-61

    Chapter 2

    1. With a neat diagram0 e/plain the .N- Framework architectre.

    Ans:

    T!e .NET (ramework is partitio#ed i#to mod&les% eac! wit! its ow# disti#ct respo#sibility. T!e !i$!etiers re mo#itors t!e e+ec&tio# of .NET applicatio#s a#d provides re

  • 8/13/2019 Basics of .Net for Kuvempu University

    6/48

    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. ow the Windows rogramming is different from .N- rogramming?

    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 fctio

    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 fctio#

    w!ic! will commicate 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 fctio#alities of traditio#al wi#dows "I

    alo#$ wit! fctio#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

    fdame#tal pri#ciple of t!e rtime. T!e respo#sibility of rtime is to load t!e code% r t!e code a#

    provide all ki#ds of services. T!e services provided by .NET >time are as memory ma#a$eme#t% t!reama#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

    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 DSI9=. T!

    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 ii#terpreted w!ile t!e I9 is compiled by GIT compilatio# to #ative mac!i#e code. T!e I9 code is #ot t!

    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 providea way for la#$&a$e i#teroperability. 9a#$&a$e I#teroperability mea#s t!at a ob'ect impleme#ted i# o#

    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

    applicatio# ca# be developed i# two or more la#$&a$es s&pported by .Net framework. To make &se of la#$&a$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 fctio#s for all services&pported by t!e framework. T!is vast collectio# !as well writte# code for almost all tasks o# all services o

    .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.

    6

  • 8/13/2019 Basics of .Net for Kuvempu University

    7/48

    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 te#!a#ce a#d e#s&re la#$&a$e i#teroperability is called t!e 3ommo# 9a#$&a$e Specificatio# D39S=. T!e 39

    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 rtime.

    i. ,ust(in(Time ),IT* Compilation- T!e compilatio# co#verts I9 i#to its #ative mac!i#e code. T!e #amG&st-i#-Time is beca&se it compiles portio# of code as a#d w!e# re

  • 8/13/2019 Basics of .Net for Kuvempu University

    8/48

    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#

    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

  • 8/13/2019 Basics of .Net for Kuvempu University

    9/48

    Basics of .NET [BSIT-61

    8ale t"pesdirectly stores data i# t!e variable. 2al&e types ca# be b&ilt-i# type% &ser-defi#ed types% or

    e#&meratio#s.eference t"pesstore a refere#ce to t!e datas 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.:!ect !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# io#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!

    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 rtime 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&tralma##er.It $ives re to perform m&c! of its fctio#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 commicate 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 assemblys operatio#.b. "ssembly Ide#tity D#ame% versio#% c&lt&re% p&blic key=.

    c. E+ported types

    d. *t!er depe#de#t assembliese. Sec&rity permissio#s

    f. escriptio# of types

    A

  • 8/13/2019 Basics of .Net for Kuvempu University

    10/48

    Basics of .NET [BSIT-61

    $. embers like met!ods% fields% properties% eve#ts% #ested types% etc.!. "ttrib&tes

    1t is told that if we learn an" one .N- langage0 then working with other .N- langages will !

    eas". ow? -/plain with the appropriate e/ample.

    Ans:

    Chapter 3

    1. $ist ot the important featres of C@?

    Ans:Importantfeatures 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.

    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:

    ?? " 5elcome to t!e world of 3 S!arp7 pro$ram i# 3C

    &si#$ System,

    class 5elcome

    Lp&blic static void ai# Dstri#$[] ar=

    L

    3o#sole.5rite9i#e D5elcome to t!e world of 3 S!arp7=,M

    M

    1

  • 8/13/2019 Basics of .Net for Kuvempu University

    11/48

    Basics of .NET [BSIT-61

    $escription of the aboe &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!

    comme#ts as t!e part of code. 3ompiler i$#ores t!e comme#ts d&ri#$ t!e time of compilatio#. 3omme#t

    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)

    ?? " 5elcome 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 byplaci#$ 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& cacreate 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 / i# ai# i

    &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

    class. I# t!e 5elcome to t!e world of 3 S!arp7 e+ample% it resides i#side t!e /5elcome 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#eD5elcome to t!e world of 3 S!arp7=, &ses t!e 5rite9i#e met!odof 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#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#

    compiler. 5e are #ot $oi#$ i#to t!e 2is&al 3C IE. (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 5elcome to t!e world of 3 S!arp7 o# t!e co#sole.

    3. -/plain the general strctre 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 Namespace1L

    class 3lass1

    LMstr&ct Str&ct1

    LM

    i#terface I#terface1LM

    dele$ate i#t ele$ate1D=,

    e#&m E#&m1

    LM

    11

  • 8/13/2019 Basics of .Net for Kuvempu University

    12/48

    Basics of .NET [BSIT-61

    #amespace Namespace;LM

    class 3lass;

    Lp&blic static void ai#Dstri#$[] ar$s=

    LMMM

    4. ow do namespaces and t"pes in C@ ha%e niAe names? 5i%e an e/ample.Ans) Namespaces a#d types always !ave i

  • 8/13/2019 Basics of .Net for Kuvempu University

    13/48

    Basics of .NET [BSIT-61

    M

    #. $ist ot the difference !etween C@ and CBB.

    Ans:*ollo%ing are the differences bet%een ) and ++ '

    C@ is prel" o!ect)oriented langage0 whereas CBB spport o!ect)oriented programming.

    9rra"s6The 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"pein )& 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 strct 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.

    reprocessor directi%esare 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.Theain method is declared differentl# from the main function in ++. The usage of the command'line

    arguments are also different.

    ethod parameters& ) supports ref and out parameters, %hich are used instead of pointers in passing

    parameters b# reference.

    ointers are allowed in C@ !t onl" in nsafe mode.

    :%erloading operators is performed differentl" in C@.

    Strings& ) strings are different from ++ strings.

    Theforeach ke"wordallo%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 othernamespaces %ithout full# 1ualif#ing the t#pe names.

    ,estrctors6In ), #ou don2t have control over %hen a destructor is called because destructors are called

    automaticall# b# the garbage collector.

    Constrctors6Unli(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@

  • 8/13/2019 Basics of .Net for Kuvempu University

    14/48

    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? ow 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

    1

  • 8/13/2019 Basics of .Net for Kuvempu University

    15/48

    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 fctio#. " parameter declared wit!o&t a

    ref or o&t modifier is a val&e parameter. " val&e parameter e+ists til t!e fctio# 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 passedas a# ar$&me#t. B&t o&tp&t parameter m&st be defi#itely assi$#ed before t!e fctio# 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&ltval&e.

    3. What is the importance of atomatic 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#dprovides 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

  • 8/13/2019 Basics of .Net for Kuvempu University

    16/48

    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 todeclare a label '&st before t!e closi#$ M7 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+

    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

    Lstatic void ai# D=

    L

    i#t[] arr #ew i#t[] L1%;%@%M,foreac! Di#t i i# arr=

    3o#sole.5rite9i#e D2al&e is LM7% arr=,

    M

    M

    +. Which are different mp statements spported !" 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+UBreakL

    static void ai# Dstri#$[] ar=

    Li#t i,

    w!ile Dtr&e=

    Lif Diar.9e#$t!=

    break,

    3o#sole.5rite9i#e Dar[i44]=,

    16

  • 8/13/2019 Basics of .Net for Kuvempu University

    17/48

    Basics of .NET [BSIT-61

    MM

    M

    Continue !tatements: ( T!e co#ti#&e stateme#t is &sed to skip all t!e s&bse

  • 8/13/2019 Basics of .Net for Kuvempu University

    18/48

    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#dlersassociated 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=

    Lif Db=

    t!row #ew e+ceptio# Divided by Qero7=,

    ret&r# a?b,M

    static void ai# D=

    Ltry

    L

    3o#sole.5rite9i#e D(DH%==, M

    catc! DE+ceptio# e= L

    3o#sole.5rite9i#e DError7=, M

    M

    M

    7. What are checked and nchecked statements? 5i%e -/ample.

    Ans:Checed !tatement: (T!e 3!ecked stateme#t ca&ses all e+pressio#s i# t!e c!ecked block to beeval&ated i# a c!ecked co#te+t.

    4ncheced !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 + I#t@;.a+2al&e,

    3o#sole.5rite9i#eD+ 4 1=, ?? *verflow c!ecked

    L

    3o#sole.5rite9i#eD+ 4 1=, ?? E+ceptio#

    Mc!ecked

    L

    3o#sole.5rite9i#eD+ 4 1=, ?? *verflowM

    }

    M

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

    1K

  • 8/13/2019 Basics of .Net for Kuvempu University

    19/48

    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&tesstateme#ts% a#d t!e# releases t!e lock. T!e e+ample is s!ow# below :

    e.$.) - static void ai#D=L

    " a ...,

    lockDa=

    La. 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 #ew >eso&rceD== L

    r.(cD=, M

    M

    1

  • 8/13/2019 Basics of .Net for Kuvempu University

    20/48

    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 rtime% 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 Simpleele$ateD=, ?? ele$ate t!at takes #o ar$&me#ts a#d ret&r#s #o res&lt&si#$ System,

    class Test

    Lstatic void (D=

    L

    3o#sole.5rite9i#eDTest.(7=,M

    static void ai# D=

    LSimpleele$ate d#ew Simpleele$ateD(=,

    dD=,M

    M

    13. Write a program to demonstrate the sage of enms in C@?

    Ans: &si#$ System,

    Namespace E#&mOear

    L class isplayOear

    L

    e#&m yearLGa#%(eb%arc!%"pril%ay%Ge%G&ly%"&$%Sept%*ct%Nov%ecM, static void ai#Dstri#$[] ar=

    L

    i#t (irstUo#t!Di#t=year.Ga#,

    i#t 9astUo#t!Di#t=year.ec,3o#sole.5rite9i#eDGa# LM7% (irstUo#t!=,

    3o#sole.5rite9i#eDec LM7%9astUo#t!=,

    M M

    M

    14. Write a note on namespaces in [email protected]: 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# i#ter#al7 or$a#i8atio# system for a pro$ram% a#d as a# e+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

    ;

  • 8/13/2019 Basics of .Net for Kuvempu University

    21/48

    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! ascomme#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 fctio#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

  • 8/13/2019 Basics of .Net for Kuvempu University

    22/48

    Basics of .NET [BSIT-61

    i#ter#al &s!ort redart,i#ter#al &s!ort bl&eart,

    i#ter#al &s!ort $ree#art,

    p&blic 3olorD&s!ort red% &s!ort bl&e% &s!ort $ree#= L

    redart red,

    bl&eart bl&e,

    $ree#art $ree#, M

    p&blic static 3olor >ed #ew 3olorD+((% % =,

    p&blic static 3olor Bl&e #ew 3olorD% +((% =,p&blic static 3olor ree# #ew 3olorD% % +((=,

    p&blic static 3olor 5!ite #ew 3olorD+((% +((% +((=,

    M

    4. With appropriate e/amples e/plain parameter passing in C@.

    Ans: I# 3C% parameter ca# be passed eit!er by alueor by reference. assi#$ parameters by refere#ce allowfctio# members Dmet!ods% properties% i#de+ers% operators a#d co#str&ctors= to c!a#$e t!e val&e of t!

    parameters a#d !ave t!at c!a#$e persist. To pass a parameter by refere#ce% &se t!e refor otkeyword.

    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 th

    method, %hat actuall# gets s1uared is %hat x refers to & m#Int.

    usingSystem;classParameterPassingValByRef{ staticvoidSquareIt(refintx)

    // !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#licstaticvoid0ain() { intmyInt 1; %onsole$rite*ine(+!e value #efore calling t!e met!od, {-.+myInt); SquareIt(refmyInt); // 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

    ;;

  • 8/13/2019 Basics of .Net for Kuvempu University

    23/48

    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&esare 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 commicatio#.

    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

    Lp&blic (orm1D=

    L

    B&tto#1.3lick 4 #ew Eve#tRa#dlerDB&tto#1U3lick=,M

    B&tto# B&tto#1#ew B&tto#D=,

    void B&tto#1U3lick Dob'ect se#der% Eve#t"r$s e=

    L3o#sole.5rite9i#eDB&tto#1 was 3licked7=,

    M

    p&blic void disco##ectD=

    LB&tto#1.3lick - #ew Eve#tRa#dlerDB&tto#1U3lick=,

    MM

    +. 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 StackL

    private Node etNodeDi#t i#de+=

    L

    Node temp first,w!ile Di#de+ X =

    L

    temp temp.Ne+t,

    i#de+ Y Y,

    ;@

  • 8/13/2019 Basics of .Net for Kuvempu University

    24/48

    Basics of .NET [BSIT-61

    Mret&r# temp,

    M

    p&blic ob'ect t!is[i#t i#de+]

    L

    $et

    Lif DZ2alidI#de+Di#de+==

    t!row #ew E+ceptio#DI#de+ o&t of ra#$e.7=,

    elseret&r# etNodeDi#de+=.2al&e,

    M

    setL

    if DZ2alidI#de+Di#de+==

    t!row #ew E+ceptio#DI#de+ o&t of ra#$e.7=,else

    etNodeDi#de+=.2al&e val&e,M

    MM

    class TestL

    static void ai#D=

    LStack s #ew StackD=,

    s.&s!D1=,

    s.&s!D;=,s.&s!D@=,

    s[] @@,

    s[1] ;;,

    s[;] 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,

    #amespacecalc

    Lclass3alc&lator

    L

    statici#t#&m1%#&m;%total,3alc&latorD=

    L

    #&m11,

    ;

  • 8/13/2019 Basics of .Net for Kuvempu University

    25/48

    Basics of .NET [BSIT-61

    #&m;;,M

    p&blicvoid"ddN&mberD=

    Ltotal#&m14#&m;,

    M

    p&blicvoidisplayN&mberD=

    L3o#sole.5rite9i#eDT!e total is LM%total=,

    M

    p&blicstaticvoidai#Dstri#$[] ar =L

    3alc&latorc#ew3alc&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

    Lclass 3alc&lator

    L

    static i#t #&m1%#&m;%total,p&blic void "ddN&mberD=

    L

    total#&m14#&m;,3o#sole.5rite9i#eDT!e Total is LM7%total=,

    M

    3alc&latorD=

    L#&m11, #&m;;, total,

    3o#sole.5rite9i#eD3o#str&ctor I#voked7=,

    M\3alc&latorD=

    L

    3o#sole.5rite9i#eDestr&ctor I#voked7=,

    Mp&blic static void ai#Dstri#$[] ar =

    L

    3alc&lator c#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

  • 8/13/2019 Basics of .Net for Kuvempu University

    26/48

    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&ctL

    static i#t #&m1, i#t #&m;,

    static StatU3o#str&ct

    L#&m11, ??*. Si#ce #&m1 is static variable i# t!is class.

    #&m;;@, ??E>>*>. Si#ce #&m; is #o#-static variable i# t!is class.

    M M

    ;. ow does C@ spports 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 fctio#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&tomaticallyc!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

    LWW.

    M

    1

  • 8/13/2019 Basics of .Net for Kuvempu University

    27/48

    Basics of .NET [BSIT-61

    MM

    p&blic float a+Dfloat #&m1% float #&m;=

    LifD#&m1X#&m;=

    L

    ret&r# #&m1,

    Melse

    L

    ret&r# #&m;,M

    M

    Mclass a+3alc

    L

    static i#t ai#D=L

    3alc&latea+ cal#ew 3alc&latea+D=,3o#sole.5rite9i#eDLM7%cal.a+DH.(% K.6(==,

    3o#sole.5rite9i#eDLM7%cal.a+D1A%1;==,3o#sole.>ead9i#eD=,

    ret&r# , 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 fctio#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@R14R;,

    ;J

  • 8/13/2019 Basics of .Net for Kuvempu University

    28/48

    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:reprocessor : (5!ile t!e compiler does #ot !ave a separate preprocessor% t!e directives are processe

    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#l

    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 - De

  • 8/13/2019 Basics of .Net for Kuvempu University

    29/48

    Basics of .NET [BSIT-61

    e.$. ) - 2define34B562defineV%7V8usingSystem;"u#licclass0y%lass{ "u#licstaticvoid0ain() { 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#adirective% be$i##i#$ wit! a Cif directive% m&st e+plicitly be termi#ated wit! a Ce#dif directive.

    e.$. ) - 2define34B562defineV%7V8usingSystem;"u#licclass0y%lass

    { "u#licstaticvoid0ain() {

    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 C

    directive% t!e e+pressio# will eval&ate to tr&e.

    e.$. ) - 2define34B562defineV%7V8usingSystem;"u#licclass0y%lass{

    "u#licstaticvoid0ain() { 2if(34B56 99 :V%7V8) %onsole$rite*ine(+34B56 is defined+); 2else %onsole$rite*ine(+34B56 and V%7V8 are not defined+); 2endif

    . .

    5undef : (Cdef% 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 Cdef directive m&st appear i# t!e file before we &se a#y stateme#ts t!at are #o

    directives.

    e.$. ) - 2undef34B56usingSystem;

    ;A

  • 8/13/2019 Basics of .Net for Kuvempu University

    30/48

    Basics of .NET [BSIT-61"u#licclass0y%lass{ "u#licstaticvoid0ain()

    {2if34B56

    %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 ofCwar#i#$ is i# a co#ditio#al directive.

    e.$. ) - 2define34B56"u#licclass0y%lass{ "u#licstaticvoid0ain()

    {2if34B56

    2&arning34B56 is defined2endif

    ..

    5error : (Cerror% $e#erates a# error from a specific locatio# i# yo&r code. " commo# &se of Cerror is i# co#ditio#al directive.

    e.$. ) - 2define34B56"u#licclass0y%lass{ "u#licstaticvoid0ain()

    {2if34B562error34B56 is defined2endif.

    .

    5line : (Cli#e% modifies t!e compilers li#e #&mber a#d t!e file #ame o&tp&t for errors a#d war#i#$s. " so&rcecode file may !ave a#y #&mber of Cli#e directives.

    e.$. ) - "u#licclass0y%lass{

    "u#licstaticvoid0ain()

    {2line--

    inti; // error or &arning &ill #e, ? on line --@2line?

    c!arc; // error or &arning &ill #e, ? on line ?@ .

    .

    2. What is an e/ception? ow e/ceptions are handled in C@?

    Ans: "# e+ceptio# is a# erro#eo&s sit&atio# or e+pected be!avior t!at e#cotered d&ri#$ pro$rame+ec&tio#. E+ceptio#al sit&atio#s arise w!e# a# operatio# ca##ot be completed #at&rally.

    @

  • 8/13/2019 Basics of .Net for Kuvempu University

    31/48

    Basics of .NET [BSIT-61

    DE+ceptio# Ra#dli#$ i# 3C provides a str&ct&red way of !a#dli#$ bot! system level a#d applicatio# levelerror 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

    Lp&blic static void addU#&mbersDi#t #&m1%i#t #&m;=

    L

    tryL

    i#t res#&m1?#&m;,3o#sole.5rite9i#eDres=,

    Mcatc!DivideByQeroE+ceptio# e=

    L

    3o#sole.5rite9i#eDE+ceptio# *cc&rredLM7%e=,M

    3o#sole.5rite9i#eD"fter Ra#dli#$ t!e E+ceptio#7=,

    Mp&blic static void ai#D=

    L

    "ddN&m.addU#&mbersD1%=,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% toperform 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 safe7 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 safefeat&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 ot the ad%antages and disad%antages of mltiple 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#$ systemallocates processor time% a#d more t!a# o#e t!read ca# r i#side t!at process.

    9d%antages of ltiple hreads6 )

    i. 3a# commicate over a #etwork% to a 5eb server a#d to a database.

    @1

  • 8/13/2019 Basics of .Net for Kuvempu University

    32/48

    Basics of .NET [BSIT-61

    ii. erform operatio#s t!at take a lar$e amot of time.

    iii. isti#$&is! tasks of varyi#$ priority. (or e+ample% a !i$!-priority t!read ma#a$es time-criticaltasks% 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$rod tasks.

    ,isad%antages of ltiple hreads6 )

    i. emory re

  • 8/13/2019 Basics of .Net for Kuvempu University

    33/48

    Basics of .NET [BSIT-61

    +. What are the different methods spported !" S"stem.:!ect class? 5i%e an e/ample for each.

    Ans: (ollowi#$ are t!e differe#t met!ods s&pported byS"stem.:!ect ) -

    a. The E6uals Method : (T!is is a commo# operatio#% especially for searc!i#$ a#d sorti#$ i# collectio#it tests two ob'ects for e

  • 8/13/2019 Basics of .Net for Kuvempu University

    34/48

    Basics of .NET [BSIT-61 ref4q3em$Instance4qual();

    %onsole$Read*ine(); .

    "u#licvoidInstance4qual() { stringname +Coe+; 4m"loyeeem"loyee= ne&4m"loyee(name); 4m"loyeeem"loyee ne&4m"loyee(name);

    // com"aring se"arate instances #oolis4qual D#Eect$Reference4quals(em"loyee= em"loyee); %onsole$rite*ine(+em"loyee= em"loyee {-.+ 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 {-.+ 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.$.) - usingSystem;class4m"loyee{ stringe7name; "u#lic4m"loyee(stringname) { e7name name;

    . // If oString() is not overridden t!e out"ut of t!is "rogram &ill #e

  • 8/13/2019 Basics of .Net for Kuvempu University

    35/48

    Basics of .NET [BSIT-61 o#Eectem"= ne&4m"loyee(); 4m"loyeeem" ne&4m"loyee(); %onsole$rite*ine(em"=$6ety"e()); %onsole$rite*ine(em"$6ety"e()); %onsole$Read*ine(); .

    .

    e. The /et7ashCode Method : (T!e 9et:ashode met!od makes a#y ob'ect &sable i# a:ashtable ora#y !as!i#$ al$orit!m. Si#ce t!e defa&lt al$orit!m s&pplied by t!e 9et:ashode met!od of t!e ob8ect

    class is #ot $&ara#teed to be i

  • 8/13/2019 Basics of .Net for Kuvempu University

    36/48

    Basics of .NET [BSIT-61return(4m"loyee)0em#er&ise%lone();."u#licJddress4m"loyeeJddress{get{returne7address;...class0em#er&ise%lone3emonstration{staticvoid0ain(){4m"loyeeem"= ne&4m"loyee(+Coe+);4m"loyeeem" em"=$S!allo&%o"y();

    // com"are 4m"loyee references#oolis4qual D#Eect$Reference4quals(em"= em");%onsole$rite*ine(+em"= em" {-.+ is4qual);//com"are reference of Jddress o#Eect in eac! 4m"loyee o#Eect

    is4qual D#Eect$Reference4quals(em"=$4m"loyeeJddressem"$4m"loyeeJddress);

    %onsole$rite*ine(+em"=$4m"loyeeJddress em"$4m"loyeeJddress {-.+ 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#te

    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 #ewob'ect.

    e.$.) - // Boxing$cs// Boxing an integer varia#leusingSystem;classestBoxing

    { "u#licstaticvoid0ain() {

    inti =K; o#Eecto i; // Im"licit #oxing

    i L1>; // %!ange t!e contents of i %onsole$rite*ine(+!e valuety"e value {-.+ i);

    %onsole$rite*ine(+!e o#Eectty"e value {-.+ o); .

    .

    2utput) - T!e val&e-typeH6 a#d T!e ob'ect-type1;@

    ?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

    is c!a#$ed from 1;@ to H6. 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 typ

    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.$. ) - usingSystem;"u#licclass5n#oxingest{

    @6

  • 8/13/2019 Basics of .Net for Kuvempu University

    37/48

    Basics of .NET [BSIT-61 "u#licstaticvoid0ain()

    { intintI =K; // Boxing o#Eecto 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#oxingest$0ain() 4rror , Incorrect 5n#oxing '/

    intintC (int)o;// 3is"lay t!e Dut"ut &it!out 4xce"tion %onsole$rite*ine(+5n#oxing DM$+);

    .

    catc!(Invalid%ast4xce"tione) {

    %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 t

    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 !" = in the string rose is a roseG.

    Ans: usingSystem; class%!ange%ase { staticvoid0ain(stringGH args) { stringstr +rose is a rose+;

    strstr$Re"lace(+r++R+); %onsole$rite*ine(str); %onsole$Read*ine(); . .

    1

  • 8/13/2019 Basics of .Net for Kuvempu University

    38/48

    Basics of .NET [BSIT-61 stringGH ee3ays ne&stringGH { +Sun+ +Sat+ +0on+ +ue+ +ed+ +!u+ +Fri+.

    // Pass t!e array as a "arameter, PrintJrray(ee3ays); .

    .

    b. Multidimensional Arra"s

    Ans: "rrays ca# !ave more t!a# o#e dime#sio#.e.$. ) - usingSystem;

    "u#licclass0ulti3imensionalJrray%lass{ staticvoidPrintJrray(intGH &) { // 3is"lay t!e array elements,

    for(inti -; i N L; iOO) for(intE -; E N ; EOO) %onsole$rite*ine(+4lement({-.{=.){.+ i E &Gi EH); .

    "u#licstaticvoid0ain()

    { // Pass t!e array as a "arameter, PrintJrray(ne&intGH { { = . { 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#

    dime#sio# a#d si8es. " 'a$$ed array is sometimes called a# array-of-arrays7.e.$. ) - usingSystem;

    "u#licclassCaggedJrrayest{ "u#licstaticvoid0ain()

    { // 3eclare t!e array of t&o elements, intGHGH myJrray ne&intGHGH;

    // InitialiQe t!e elements, myJrrayG-H ne&intG1H { = K 1 8 .; myJrrayG=H ne&intGLH { L > ? .;

    // 3is"lay t!e array elements, for(inti -; i N myJrray$*engt!; iOO) { %onsole$rite(+4lement({-.), + i);

    for(intE -; E N myJrrayGiH$*engt!; EOO) %onsole$rite(+{-.{=.+ myJrrayGiHGEH E (myJrrayGiH$*engt! =) ++, + +);

    %onsole$rite*ine(); . ..

    @K

  • 8/13/2019 Basics of .Net for Kuvempu University

    39/48

    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% i

    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#itel

    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# b

    i#itiali8ed to a differe#t array.

    e.$. ) - usingSystem;

    classPassingJrray5sing7Ref7and7Dut{

    "u#licstaticvoidFillJrray(refintGH 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#licvoid0ain()

    { // InitialiQe t!e array,

    intGH myJrray { = K L 1 .; // Pass t!e array using ref,

    FillJrray(refmyJrray); // 3is"lay t!e u"dated array, %onsole$rite*ine(+Jrray elements are,+); for(inti -; 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&

    collectio#s of ob'ects% s&c! as lists%

  • 8/13/2019 Basics of .Net for Kuvempu University

    40/48

    Basics of .NET [BSIT-61

    Chapter +

    1. 5i%e the o%erall concept of 9,:.N- with the help of figres. 'int6 (ig pictre*

    Ans:

    F "ctive 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% 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 S9 Server% or data so&rces e+posed via*9E B a#d 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% dataca# be retrieved from icrosoft E+cel a#d t!e# saved i# a# 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 adapterob'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# 9 represe#tatio# of a dataset.F I# a# 9 represe#tatio# of a dataset% data is writte# i# 9 format% a#d dataset sc!ema is writte# by

    &si#$ t!e 9 Sc!ema defi#itio# la#$&a$e DS=.

    0 The t&o e" components of the A$2 .NET are: (

    a. Data ;rovider b. Dataset

  • 8/13/2019 Basics of .Net for Kuvempu University

    41/48

    Basics of .NET [BSIT-61

    4 The four (e# components of a data provider are& '. ,ata eader "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& '

    . ,ataa!le 6 )It represents a table in the data table ollection of a dataset.

    ?. ,ataowCollection 6 )It contains all the ro%s in a data table.

    @. ,ataColmnCollection 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- architectre.Ans:

    3. What is a data pro%ider? -/plain.

    Ans:,ata ro%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 isbei#$ accessed.

    F T!e fo&r mai# types of data providers are) -

    a. .NET 3rame&or data proider for !8L !erer : (T!e System.ata.dll assembly impleme#ts SServer .NET (ramework data provider i# t!e S"stem.,ata.SAlClient #amespace a#d it specificall

    worked wit! icrosoft S9 Server.

    b. .NET 3rame&or data proider 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 assemblimpleme#ts *9E B .NET (ramework data provider i# t!eS"stem.,ata.:le,!#amespace.

    c. .NET 3rame&or data proider for 2$+C : (To &se t!is type of provider% we m&st &se a# *B

    driver. T!eS"stem.,ata.:,(C.dllassembly impleme#ts *B3 .NET (ramework data provider.d. .NET 3rame&or data proider 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.d

    assembly impleme#ts t!e *racle .NET (ramework data provider i# t!e S"stem.,ata.:racleClien

    #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#$ fctio#ality.F T!e fo&r mai# compo#e#ts of .NET data provider ) -

    2b-ect $escription

    @. Connection 7stablishes a connection to a specified data source.

    . Command 7xecutes a command against a data source.

    B. ,ata eader "eads a for%ard'onl#, read'onl# stream of data from a data source

    1

  • 8/13/2019 Basics of .Net for Kuvempu University

    42/48

    Basics of .NET [BSIT-61

    C. ,ata 9dapter ;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!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 ) -. ,ataa!leCollection 6 )It contains all the tables retrieved from the Datasource.

    B. ,ataelationCollection 6 )It contains relationship and lin(s bet%een tables in a Dataset.

    C. ,ataa!le 6 )It represents a table in the datatable ollection of a dataset.. ,ataowCollection 6 )It contains all the ro%s in a datatable.

  • 8/13/2019 Basics of .Net for Kuvempu University

    43/48

    Basics of .NET [BSIT-61

    Chapter 7

    1. What is a we! ser%ice?-/plain its working.

    Ans: #eb !erice: ( " 5eb service is a very simple idea. It is a applicatio# lo$ic% accessible t!ro&$! somesta#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:

    NUPage*anguage+%2+Juto4ventireu"+true+ %odeFile+3efault$as"x$cs+In!erits+73efault+UW

    N:3D%XP4!tmlP5B*I%+//K%//33 Y0* =$- ransitional//4A++!tt",//&&&$&K$org/R/x!tml=/33/x!tml=transitional$dtd+W

    N!tmlWN!eadW

    N/!eadWNscri"tlanguage+%2+runat+server+WvoidSu#mitBtn7%lic(D#Eectsender 4ventJrgse){0essage$ext +i +O tt"5tility$tml4ncode(Aame$ext) O + !ans::::: Xou !aveSelected, +O %ategory$SelectedItem;.N/scri"tWN#odyWNcenterWNformaction+3efault$as"x+met!od+"ost+runat+server+WN!KWAame, Nas",text#oxid+Aame+runat+server+/W

    @

  • 8/13/2019 Basics of .Net for Kuvempu University

    44/48

    Basics of .NET [BSIT-61%ategory, Nas",dro"do&nlistid+%ategory+runat+server+WNas",listitemWDXSN/as",listitemWNas",listitemWSPDRS MIN/as",listitemWNas",listitemWF*D4RSN/as",listitemWN/as",dro"do&nlistWN/!KWNas",#uttontext+*oou"+Dn%lic+Su#mitBtn7%lic+runat+server+/W

    Nas",la#elid+0essage+runat+server+/WN/formWN/centerWN/#odyWN/!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!eNas",la#elrunat+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

    ropow#9ist% o#e B&tto#.F 5!e# t!e 0SE> select a#y item prese#t i# t!e ropow#9ist a#d click o# t!e B&tto#% it displays a messa$

    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 9S.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 oft!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! 5!at Oo& See Is 5!at Oo& et7 D5OSI5O= RT9 editors

    a#d ot!er pro$rammi#$ tools% i#cl&di#$ icrosoft 2is&al St&dio .NET.

    #. What is the se of 9S.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 safetyI#!erita#ce% la#$&a$e i#teroperability a#d versio#i#$.

    &. Write a program to displa" Welcome to 9S.N-G 7 times in increasing order of their font siIe sing

    9S.N-.Ans: NUPage*anguage+%2+Juto4ventireu"+true+ %odeFile+3efault$as"x$cs+In!erits+73efault+UWN:3D%XP4!tmlP5B*I%+//K%//33 Y0* =$- ransitional//4A++!tt",//&&&$&K$org/R/x!tml=/33/x!tml=transitional$dtd+WN!tmlxmlns+!tt",//&&&$&K$org/=/x!tml+WN!eadrunat+server+WNtitleW5ntitled PageN/titleWN/!eadWN#odyWNcenterW Nformid+form=+runat+server+W

    NdivW NUfor(inti-; iN?;iOO){UW

  • 8/13/2019 Basics of .Net for Kuvempu University

    45/48

    Basics of .NET [BSIT-61 NfontsiQe+NUiUW+Welcome to t!e JSP $A4N/fontWN#r/W NU.UW N/divW

    N/formW N/centerW

    N/#odyWN/!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

    commicatio#.F T!is #amespace i#cl&des t!e Rttp>e

  • 8/13/2019 Basics of .Net for Kuvempu University

    46/48

    Basics of .NET [BSIT-61

    Chapter ;

    1. What is a .N- remoting?

    Ans: .NET >emoti#$ e#ables commicatio# betwee# applicatio#s.F iffere#t applicatio#s ca# commicate 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 commicatio# c!a##els respo#sible for tra#sporti#$ messa$es to a#d from remote

    applicatio#s.

    F It ca# also &se 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. ow is .N- remoting different from we! ser%ices and ,C:?

    Ans: .NET 'emoting ersus $istributed C2M )$C2M* 1

    I# t!e earlier days% I#ter-rocess commicatio# betwee# applicatio#s was !a#dled t!ro&$! istrib&te3ompo#e#t *b'ect odelD3*=. 3* works well a#d t!e performa#ce is adeemoti#$% b&t web services !ave a simplified pro$rammi#$ modea#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 reemoti#$ a#d 5eb services)

    "S.NET based 5eb services ca# o#ly be accessed over RTT. 5!ereas t!e .NET >emoti#$ ca# b

    &sed across a#y protocol.

    5eb services work i# a stateless e#viro#me#t w!ere eac! re

  • 8/13/2019 Basics of .Net for Kuvempu University

    47/48

    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#ts

    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 ot!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

  • 8/13/2019 Basics of .Net for Kuvempu University

    48/48

    Basics of .NET [BSIT-61

    #. -/plain the steps or phases in%ol%ed in implementing .N- remoting applications.

    Ans:

    &. Write a program to displa" ello 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=

    LM

    ?? >et&r# a Rello world messa$ep&blic stri#$ Rello5orldD=

    Lret&r# Rello 5orldZ7,

    M

    M M