You are on page 1of 25

Deccansoft Software Services Language Basics

Chapter-3

1
!"#$%&
!" #$%&%'%() +,', -./)0 ,12 3,$%,45) +)65,$,'%71
8" 9,125%1: ;()$<57= >?)6@0
A" >,0'%1: 4)'=))1 7'?)$ 2,', './)0
B" C7D%1: E F147D%1:
G" H1I& ,12 >710',1'
J" ;/)$,'7$0
K" >71'$75 L',')&)1'0
M" N7$@%1: =%'? O$$,.0
P" N7$@%1: =%'? Q)'?720

(&)*# +, -.$/#$/0
8l Ml 1l vL 8l Ml 1l vL u uA1A A1A1 1?LS Anu ?LS Anu v vA8l A8LL A8l A8LL u uLCLA8A1l Cn LCLA8A1l Cn 3
v vA8l A8LL A8l A8LL u uLCLA8A1l Cn LCLA8A1l Cn S S?n1Ax ?n1Ax 5
P PAnuLl nC AnuLl nC C CvL8lLCW vL8lLCW C CPLCkS PLCkS 6
C CAS1 AS1l nC 8L1WLLn C1PL8 uA l nC 8L1WLLn C1PL8 uA1A 1?LS 1A 1?LS 8
8 8Cxl nC Cxl nC & & u un8Cxl nC n8Cxl nC 9
L LnuM Anu nuM Anu C CCnS1An1 CnS1An1 10
C CL8A1C8S L8A1C8S 11
C CCn18CL Cn18CL S S1A1LMLn1S 1A1LMLn1S 12
W WC8kl nC Wl 1P C8kl nC Wl 1P A A88A?S 88A?S 18
W WC8kl nC Wl 1P C8kl nC Wl 1P M ML1PCuS L1PCuS 22










Deccansoft Software Services Language Basics
Chapter-3

2


Deccansoft Software Services Language Basics
Chapter-3

3
r| m| t| ve Data1ypes and r| m| t| ve Data1ypes and Var| ab| e Dec| arat| on Var| ab| e Dec| arat| on
Integra| 1ypes:
Data1ype S|ze . Net (C1S) Comments
byLe 1 SysLem.8yLe 0 - 233 lL ls unslgned
SbyLe 1 SysLem.S8yLe -128 Lo 127 - Slgned
ShorL 2 SysLem.lnL16
uShorL 2 SysLem.ulnL16
lnL 4 SysLem.lnL32
ulnL 4 SysLem.unL32
long 8 SysLem.lnL64
ulong 8 SysLem.ulnL64

I|oat|ng 1ypes (the pos|t|on of dec|ma| po|nt |s not f|xed):
lloaL 4 SysLem.Slngle Pas up Lo 8 dlglLs afLer declmal
uouble 8 SysLem.uouble Pas up Lo 16 dlglLs afLer declmal

ueclmal* 16 SysLem.ueclmal Pas 28 dlglLs afLer declmal and lLs flxed
* Lven Lhough Lhe declmal daLaLype as declmal polnL lL's noL floaLlng because Lhe number of dlglLs afLer
declmal are flxed l.e. 28 dlglLs.
Cther Data1ypes:
Char 2 SysLem.Char uses unlcode CharseL and Lhus supporLs mulLlple
languages
str|ng ** SysLrlng.SLrlng uses unlcode CharseL
bool 1 SysLem.8oolean 1rue / lalse
ob[ect ** SysLem.Cb[ecL Cenerlc uaLaLype
**All Lhe above daLa Lypes are value 1ypes excepL Str|ng and Cb[ect, whlch are keference
Why Data types:
! 8ased on Lhe daLa Lype Lhe slze of varlable and Lhe formaL ln memory ls declded
! 8ased on Lhe daLa Lype Lhe compller ls golng Lo valldaLe Lhe expresslons maklng Lhe language Lype
safe.
Memory ln an appllcaLlon ls dlvlded lnLo Lhree parLs.
1. G|oba| Memory: used by all global varlables. 1hese varlables are allocaLed memory when Lhe
appllcaLlon beglns and wlll remaln ln memory LhroughouL Lhe llfe of Lhe appllcaLlon.
Deccansoft Software Services Language Basics
Chapter-3

4
2. Stack Memory: used by local varlables of a meLhod. When a meLhod ls lnvoked a sLack of memory ls
allocaLed Lo and Lhe sLack ls cleared when Lhe meLhod reLurns
3. neap Memory: All dynamlc memory requlremenLs of an appllcaLlon are fulfllled from Lhe heap
memory. AfLer allocaLlng some memory from heap Lo a varlable, once lLs [ob ls compleLed Lhe memory
musL be reLurned back Lo heap so LhaL Lhe same can be reused for anoLher varlable.
1he value Lype of varlable has value where as Lhe value of a reference Lype ls reference Lo value
(ob[ecL) on heap.
1he value (ob[ecL) of a reference Lype ls always allocaLed heap memory.
value Lypes are allocaLed memory based on Lhe scope of Lhe varlable, lf lLs Clobal varlable lLs allocaLed
memory ln Clobal Memory Area, lf lLs Local varlable or arameLer lLs allocaLed memory on sLack and lf
lL's a member of an ob[ecL lLs allocaLed memory on Peap.
Va|ue 1ypes dlrecLly hold Lhe value.
Lx: All 8aslc 1ypes, SLrucLures & Lnum
keference 1ypes hold Lhe reference Lo Lhe value on
PLA.
Lx: SLrlng, Cb[ecL, Class, Arrays, uelegaLes




Var = Value Value
Var
Ref
type
ValueType
Deccansoft Software Services Language Basics
Chapter-3

5
Var| ab| e Dec| arat| on Syntax Var| ab| e Dec| arat| on Syntax
lnL a,b,
lnL a=10, b=20,
A |oca| var|ab|e declared musL be expllclLly lnlLlallzed before used ln an expresslon oLherwlse glves an
compllaLlon error.
A varlable declared ln a block ls local Lo Lhe block ln whlch lL ls declared.
A varlable declared ln ouLer block cannoL be re-declared ln Lhe lnner block.
Cast|ng:
ConverLlng daLa from one form Lo anoLher form.
lf 8PS expresslon and LPS varlable are noL of same daLa Lype Lhen casLlng ls requlred.
CasLlng can be lmpllclL casLlng or expllclL casLlng.
Imp||c|t Cast|ng: lf every posslble value of 8PS expresslon ls valld for a varlable on LPS varlable.
Lxp||c|t Cast|ng: lf an 8PS expresslon ls asslgned Lo LPS and lf Lhere ls a posslblllLy of daLa loss Lhen expllclL
casLlng ls needed.

rogram to caste |nteger to byte
uslng SysLem,
class rogram
[
publlc sLaLlc vold Maln()
[
lnL n = 236,
byLe b,
byLe b1, b2, b3,
b1 = b2 = 10, //ln v8 lL ls noL valld
b1 = b2 + b3, //CompllaLlon Lrror
//lf elLher byLe, shorL or char varlables are used ln an expresslon Lhey are auLomaLlcally ralsed
Lo Lhe rank of lnL.
b1 = (byLe)(b2 + b3),
}
}
Code: 3.1 C#





Deccansoft Software Services Language Basics
Chapter-3

6
nand| | ng Cverf| ow Checks nand| | ng Cverf| ow Checks

Code sn|ppet for hand||ng overf|ow checks
unchecked //Cverflow checks are noL done.
[
b = (byLe) n,
Console.WrlLeLlne(b),
}
checked //Cverflow checks are done.
[
b = (byLe) n,
Console.WrlLeLlne(b),
}

1o Lnab|e ] D|sab|e Cverf|ow checks:
ro[ecL " roperLles " Complle " Scroll and Cllck on Advanced Complle CpLlons " Check lnLeger Cverflow
Checks
12"3 456
Deccansoft Software Services Language Basics
Chapter-3

7


Convert|ng f|oat & dec|ma| datatypes
double dbl = 0.6u, //0.6
floaL f = 0.6l,
declmal dec = 0.6M,
//|ong to f|oat - Same as v8
long lng = 10L,
f = lng, //because range of long ls smaller Lhan floaL
lng = (long) f,
//floaL and declmal requlres casLlng
dec = 10, //lnLeger Lo declmal
//f = dec, //lnvalld
//dec = f, //lnvalld
f = (floaL) dec,
dec = (declmal) f,
//declmal" should be expllclLly casLed Lo every oLher Lype lf requlred.














Note:
ou cannot use the checked and unchecked keywords to contro| f|oat|ng po|nt (non-|nteger)
ar|thmet|c. 1he checked and unchecked keywords contro| on|y |nteger ar|thmet|c. I|oat|ng po|nt
ar|thmet|c never throws Cverf|owLxcept|on.
Deccansoft Software Services Language Basics
Chapter-3

8
Cast| ng between Cast| ng between other data other data types types

Cast|ng between other datatypes
//|nt to char.
n = c,
/*c = n, lnvalld */
c = (char) n,
//bool - lnL - anydaLaLype expllclL or lmpllclL casLlng ls noL allowed elLher dlrecLlon
//str|ng to |nt
s = "100",
n = (lnL) s, //lnvalld - SLrlng cannoL be casLed
n = lnL.arse(s), //lf falles Lhrows, lormaLLxcepLlon
bool bln = lnL.1ryarse(s, ouL n),
//n = (lnL) s, //ulrecL casLlng of sLrlng Lo lnL ls noL allowed.
//|nt to str|ng
s = n.1oSLrlng(),
//s = (sLrlng) n //CasLlng of lnL Lo sLrlng ls noL allowed

rogram to r|nt the Char equ|va|ent of the Asc|| va|ue read from the keyboard
uslng SysLem,
class rogram
[
sLaLlc vold Maln(sLrlng[] args)
[
sLrlng sLr,
sLr = Console.8eadLlne(),
lnL n = 100,
lf (lnL.1ryarse(sLr, ouL n))
Console.WrlLeLlne((char)n),
else
Console.WrlLeLlne("lnvalld number"),
}
}
Code: 3.2 C#







Deccansoft Software Services Language Basics
Chapter-3

9
8ox| ng & Unbox| ng 8ox| ng & Unbox| ng
8ox|ng ls Lhe Lerm used Lo descrlbe Lhe LransformaLlon from Va|ue 1ype to reference type (Cb[ecL). 1he
runLlme creaLes a Lemporary reference-Lype box for Lhe ob[ecL on Lhe heap.
Un8ox|ng ls Lhe Lerm used Lo descrlbe Lhe LransformaLlon from reference type (Cb[ect) to va|ue type. We
use Lhe Lerm casL here, as Lhls has Lo be done expllclLly.
ob[ecL ob[ = n, //8ox|ng
n = (lnL) ob[, //Unbox|ng
1 When a value ls boxed Lo an ob[ecL Lype, Lhe ob[ecL Lype varlable cannot be used ln any mathemat|ca|
operat|ons.
2 When Lhe value of ob[ecL Lype varlable cannoL be asslgned Lo varlable on LPS, an LxcepLlon of Lype
lnvalldCasLLxcepLlon ls Lhrown.
3 Lxcesslve usage of Cb[ecL daLa Lype makes Lhe language Loosely 1yped" and also because of frequenL
casLlng requlremenL whlle 8oxlng and unboxlng performance ls also degraded.
4 8oxlng / unboxlng should be used only ln slLuaLlons where unLll runLlme we don'L know Lhe Lype of
daLa we are golng Lo deal wlLh.







Deccansoft Software Services Language Basics
Chapter-3

10
Lnum and Constant Lnum and Constant

Cast|ng between other datatypes
enum WeekDay : lnL //Weekuay ls subLype of lnL
[
Sun=1, Mon, 1ues, Wed=3, 1hus, lrl, SaL
}
Lnum can be subLype of any lnLegral Lype only.

n = 2, //n ls lnL
Weekuay wd = Weekuay.SaL,
wd = (Weekuay) n, //LxpllclL
//n = wd, //lnvalld
n = (lnL) wd, //LxpllclL - ln v8.nL1 lL's noL requlred

//ConsLanL ueclaraLlon
const double l = 3.14,










Deccansoft Software Services Language Basics
Chapter-3

11
Cperators Cperators

ArlLhmeLlc +, -, * , / , (mod) (ower operaLor ls mlsslng)
Loglcal CperaLors ^ (xC8), !(nC1) && (v8-AndAlso) , || (v8-CrLlse)
1ernary CperaLor ?:
SLrlng concaLenaLlon + (v8-&)
lncremenL, decremenL ++ , --
8lLwlse << , >>, & , | , ~ (complemenL/negaLlon)
8elaLlonal = = , != , < , > , <= , >=
AsslgnmenL = , += , -= , *= , /= , = , &= , | = , ^= , <<= , >>=
1ype lnformaLlon ls , slzeof , Lypeof, as
lndlrecLlon and Address * , -> , [] , &

ulvldlng a lnLegral 1ype wlLh zero (lnLegral ulvlslon CperaLor) Lhrows D|v|de8y2eroLxcept|on
lloaLlng olnL ulvlslon by zero |s not a runLlme excepLlon buL Lhe value would be elLher os|t|veInf|n|ty
(lf numeraLor ls greaLer Lhan zero) or Negat|veInf|n|ty (lf numeraLor ls less Lhan zero) or NaN (lf
numeraLor ls also zero)

Us|ng Cond|t|ona| operator
n = 10,
lnL m = n++, //m=n, n=n+1 - ost Increment
Console.WrlLeLlne(n + " " + m),
m = ++n, //n=n+1, m=n - re Increment
Console.WrlLeLlne(n + " " + m),

//?: Cond|t|ona| Cperator (v8: llf)
lnL max,
max = n > m ? n : m, //?: CperaLor





Deccansoft Software Services Language Basics
Chapter-3

12
Co Contro| ntro| Statements Statements

If e|se-statement syntax:
lf (8ooleanLxpresslon)
[
sLaLemenL,
}
else lf (8oolean-Lxpresslon)
[
sLaLemenL,
}
else
[
sLaLemenL,
}

rogram to pr|nt |f the command ||ne argument prov|ded |s an odd number or even number
uslng SysLem,
uslng SysLem,
class rogram
[
publlc sLaLlc vold Maln(sLrlng[] args)
[
lnL n,
lf (args.LengLh == 0)
Console.WrlLeLlne("lease provlde a number"),
else lf (!lnL.1ryarse(args[0], ouL n))
Console.WrlLeLlne("noL a number"),
else lf (lnL.arse(args[0]) 2 == 0)
Console.WrlLeLlne("Lven number"),
else
Console.WrlLeLlne("Cdd number"),
}
}
Code: 3.3 C#





Note:
1o execute above program correct|y, supp|y va||d |ntegers |n command ||ne arguments. k|ght c||ck
on ro[ect -> Se|ect ropert|es -> C||ck on Debug tab and enter few va|ues |n "Command ||ne
arguments" sect|on seperated by spaces. Ior th|s examp|e we have used the va|ues: 2 3
Deccansoft Software Services Language Basics
Chapter-3

13
nere |s the output of execut|ng above code:

12"3 457

Sw|tch statement syntax:
sw|tch (expr) //expr can only lnLegral Lype / char / sLrlng
[
case 0: //value of case musL be a consLanL.
sLaLemenLs,
goto defaulL, // or break can be used.
case 1:
case 2:
sLaLemenLs,
break, //break musL be preseL afLer every case wlLh sLaLemenLs
defau|t:
sLaLemenLs,
break, //break musL be presenL afLer defaulL also.
}

1ask:
Wr|te a program to pr|nt grade of the person based on the marks scored.

wh||e...|oop statement syntax:
whlle (8ooleanLxpresslon)
[
SLaLemenLs,
}
do
[
SLaLemenLs
}
whlle (8ooleanLxpresslon)
}


Deccansoft Software Services Language Basics
Chapter-3

14
rogram to r|nt the tab|e of any number read from the keyboard.
uslng SysLem,
class rogram
[
publlc sLaLlc vold Maln()
[
lnL n,
Console.WrlLe("1able of: "),
n = lnL.arse(Console.8eadLlne()),
sLrlng s = "",
for (lnL l = 1, l <= 10, l++)
s += n + "*" + l + "=" + n * l + "\n",
Console.WrlLeLlne(s),
}
}
Code: 3.4 C#

nere |s the output of execut|ng above code:

12"3 454
for statement syntax:
for ( lnlLlallzer, condlLlon, lLeraLor )
[
sLaLemenLs,
}

Samp|e program |mp|ement|ng for |oops - Lxamp|e 1
uslng SysLem,
class rogram
[
sLaLlc vold Maln(sLrlng[] args)
[
for (lnL l = 0, l < 3, l++)
Deccansoft Software Services Language Basics
Chapter-3

15
[
for (lnL [ = 0, [ < 3, [++)
[
lf (l == [)
break,
Console.WrlLeLlne(l + " " + [),
}
}
}
}
Code: 3.S C#

nere |s the output of execut|ng above code:

12"3 458

Samp|e program |mp|ement|ng for |oops - Lxamp|e 2
uslng SysLem,
class rogram
[
sLaLlc vold Maln(sLrlng[] args)
[
for (lnL l = 0, l < 10, l++)
[
lf (l == 7) break,
lf (l == 3) conLlnue,
Console.WrlLeLlne(l),
}
}
}
Code: 3.6 C#

nere |s the output of execut|ng above code:
Deccansoft Software Services Language Basics
Chapter-3

16

12"3 459

rogram to show the yram|d of Numbers
uslng SysLem,
class rogram
[
sLaLlc vold Maln(sLrlng[] args)
[
lnL k = 0,
for (lnL l = 0, l < 4, l++)
[
for (lnL [ = 0, [ <= l, [++)
Console.WrlLe(k++ + "\L"),
Console.WrlLeLlne(),
}
}
}
Code: 3.7 C#

nere |s the output of execut|ng above code:

12"3 45:

Deccansoft Software Services Language Basics
Chapter-3

17

Ioreach statements syntax:
foreach (uaLa1ype ldenLlfler ln <Array or CollecLlon>)
[
embedded-sLaLemenLs,
}

rogram to pr|nt a|| command ||ne arguments
uslng SysLem,
class rogram
[
sLaLlc vold Maln(sLrlng[] args)
[
foreach (sLrlng s ln args)
Console.WrlLeLlne(s),
}
}
Code: 3.8 C#


Deccansoft Software Services Language Basics
Chapter-3

18
Work| ng w| th Arrays Work| ng w| th Arrays
1 Arrays are reference types and Lhus are allocaLed memory on heap.
2 Lvery elemenL of an array ls auLomaLlcally lnlLlallzed Lo a defaulL value based on lLs daLa Lype.
3 1hey are always dynamlc because we can SL1 Lhe slze of Lhe arrays aL runLlme.
4 Slze of array can never be changed aL runLlme.
S 1rylng Lo access an elemenL of array wlLh lnvalld lndex Lhrows lndexCuLof8angeLxcepLlon LxcepLlon.
6 All arrays lrrespecLlve of Lhelr Lype are by defaulL lnherlLed from SysLem. Array class

S|ng|e-D|mens|ona| Arrays
lnL [] myArray = new lnL [3],
sLrlng []mySLrlngArray = new sLrlng[3],

When you lnlLlallze an array upon declaraLlon, lL ls posslble Lo use Lhe followlng shorLcuLs:
lnL[] myArray = [1, 3, 3, 7, 9},
sLrlng[] weekuays = ["Sun", "SaL", "Mon", "1ue"},

lL ls posslble Lo declare an array varlable wlLhouL lnlLlallzaLlon, buL you musL use Lhe new operaLor when you
asslgn an array Lo Lhls varlable. Ior examp|e:
lnL[] myArray,
myArray = new lnL[] [1, 3, 3, 7, 9}, // Ck
myArray = [1, 3, 3, 7, 9}, // Lrror
weekdays = new sLrlng[] [Sunday", Monday", 1uesday"},

Mu|t|-D|mens|ona| Arrays
lnL[,] myArray = new lnL[4,2],
Also, Lhe followlng declaraLlon creaLes an array of Lhree dlmenslons, 4, 2, and 3:
lnL[,,] myArray = new lnL [4,2,3],
?ou can lnlLlallze Lhe array upon declaraLlon as shown ln Lhe followlng example:
lnL[,] myArray = new lnL[,] [[1,2}, [3,4}, [3,6}, [7,8}},
?ou can also lnlLlallze Lhe array wlLhouL speclfylng Lhe rank:
lnL[,] myArray = [[1,2}, [3,4}, [3,6}, [7,8}},
lf you choose Lo declare an array varlable wlLhouL lnlLlallzaLlon, you musL use Lhe new operaLor Lo asslgn an
array Lo Lhe varlable. lor example:
lnL[,] myArray,
myArray = new lnL[,] [[1,2}, [3,4}, [3,6}, [7,8}}, // Ck
myArray = [[1,2}, [3,4}, [3,6}, [7,8}}, // Lrror

rogram to use arrays:
uslng SysLem,
class rogram
[
sLaLlc vold Maln(sLrlng[] args)
[
lnL[] ar = new lnL[] [ 1, 2, 3, 4 },
Console.WrlLeLlne(ar.LengLh),
Console.WrlLeLlne(ar.8ank), //rlnLs number of ulmenslons ln array.
foreach (lnL n ln ar)
Deccansoft Software Services Language Basics
Chapter-3

19
Console.WrlLeLlne(n),
}
}
Code: 3.9 C#


nere |s the output of execut|ng above code:

12"3 45;
rogram: 1o read a ||st of numbers separated by space and pr|nt the Average of a|| those
numbers.
uslng SysLem,
class rogramlorMaxCfAnynumbers
[
sLaLlc vold Maln(sLrlng[] args)
[
sLrlng sLr = Console.8eadLlne(),
sLrlng[] ar = sLr.SpllL(' '),
lnL sum = 0,
for (lnL l = 0, l < ar.LengLh, l++)
[
sum += lnL.arse(ar[l]),
Console.WrlLeLlne(ar[l]),
}
Console.WrlLeLlne("Average: " + 1.0 * sum / ar.LengLh),
}
}
Code: 3.10 C#


Deccansoft Software Services Language Basics
Chapter-3

20
nere |s the output of execut|ng above code:

12"3 45<

rogram: 1o read |ength and data for an array from keyboard pr|nt the same.
uslng SysLem,
class rogramlorMaxCfAnynumbers
[
sLaLlc vold Maln()
[
Console.WrlLe("LnLer Lhe array lengLh: "),
lnL n,
n = lnL.arse(Console.8eadLlne()),
lnL[] ar = new lnL[n],
for (lnL l = 0, l < n, l++)
[
Console.WrlLe("LnLer Lhe " + l + "Lh value: "),
ar[l] = lnL.arse(Console.8eadLlne()),
}
for (lnL l = 0, l < ar.LengLh, l++)
[
Console.WrlLe(ar[l] + " "),
}
}
}
Code: 3.11 C#



Deccansoft Software Services Language Basics
Chapter-3

21
nere |s the output of execut|ng above code:

12"3 45=




Deccansoft Software Services Language Basics
Chapter-3

22
Work| ng w| th Methods Work| ng w| th Methods
Method Cver|oad|ng:
1 Pavlng Lwo or more meLhods wlLh same name and dlfferenL parameLers.
2 arameLers musL be dlfferenL elLher ln Lhelr daLa Lype or ln Lhelr counL.
3 MeLhod cannoL be overloaded based on 8eLurn 1ype or 8yval/8y8ef or arameLer names.
4 Call Lo Lhe Cverloaded meLhod ls resolved aL complle Llme and ls done based on daLa Lype and counL of
argumenLs passed Lo Lhe meLhod.
S Whlle resolvlng Lhe called meLhod, Lhe compller searches for a dlrecL maLch of argumenLs and
parameLers. Cnly lf a dlrecL maLch ls noL avallable lL would Lhen use nearesL maLch for resolvlng Lhe call.

Method over|oad|ng
uslng SysLem,
class rogram
[
publlc sLaLlc vold Maln()
[
lnL res = Add(10, 2),
Console.WrlLeLlne(res.1oSLrlng()),
sLrlng sLr,
sLr = Add("ueccan", "sofL"),
Console.WrlLeLlne(sLr),
}
sLaLlc lnL Add(lnL a, lnL b)
[
reLurn Add(a, b, 0),
}
sLaLlc lnL Add(lnL a, lnL b, lnL c)
[
reLurn a + b + c,
}
sLaLlc sLrlng Add(sLrlng s1, sLrlng s2)
[
reLurn s1 + s2,
}
}
Code: 3.12 C#


Deccansoft Software Services Language Basics
Chapter-3

23
nere |s the output of execut|ng above code:

12"3 456>
arams arameters:
!" ;15. #,$,&)')$0 =?%6? ,$) 7< './) ,$$,. R7< ,1. 2,', './)S 6,1 4) 2)65,$)2 ,0 /,$,&0
8" T< /,$,&)')$ %0 2)65,$)2 ,0 /,$,&0 )%'?)$ , $)<)$)16) '7 '?) ,$$,. 6,1 4) /,00)2 ,0 ,$:I&)1' 7$ U 7$
&7$) %12%(%2I,5 (,5I)0 6,1 4) /,00)2 '7 %'"
A" ;15. 71) /,$,&)')$ 7< , &)'?72 6,1 4) 2)65,$)2 ,0 /,$,&0 /,$,&)')$"
B" T' &I0' 4) 5,0' /,$,&)')$ %1 '?) 5%0' 7< /,$,&)')$0 <7$ , :%()1 &)'?72"
G" T< =) ?,() 7'?)$ /,$,&)')$0 %1 '?) 5%0' '?). &I0' 4) 4)<7$) '?) /,$,&0 /,$,&)')$ ,12 '?). 6,117' 4)
2)65,$)2 ,0 7/'%71,5"

uslng SysLem,
class rogram
[
publlc sLaLlc vold Maln()
[
lnL res = Add(10, 2),
lnL[] mar = [ 1, 2, 3, 4 },
res = Add(mar),
Console.WrlLeLlne(res.1oSLrlng()),
res = Add(),
res = Add(1, 2, 3, 4, 3),
res = Add(1, 2, 3, 4, 3, 6),
res = Add(1, 2, 3, 4, 3, 6, 7),
Console.WrlLeLlne(res.1oSLrlng()),
}

sLaLlc lnL Add(params lnL[] ar)
[
lnL sum = 0,
foreach (lnL n ln ar)
sum += n,
reLurn sum,
}
}
Code: 3.13 C#
nere |s the output of execut|ng above code:
Deccansoft Software Services Language Basics
Chapter-3

24

12"3 4566

ass by va|ue and reference examp|e
uslng SysLem,
class rogram
[
publlc sLaLlc vold Maln()
[
lnL n1, n2, n3,
n1 = n3 = 10,
loo(n1, ouL n2, ref n3),
Console.WrlLeLlne(n1 + " " + n2 + " " + n3),
}
sLaLlc vold loo(lnL a, ouL lnL b, ref lnL c)
[
a++,
b = 20,
c++,
}
}
Code: 3.14 C#

nere |s the output of execut|ng above code:

12"3 4564

Deccansoft Software Services Language Basics
Chapter-3

25
1he argumenL n2" ls passed by reference Lo b", l.e. boLh b" and n2" reference Lo same memory and
hence change made Lo b" ls also reflecLed ln n2".
Cut parameter must be lnlLlallzed ln Lhe meLhod and are generally used ln slLuaLlons where we wanL Lo
reLurn more Lhan one value from Lhe meLhod.
lf a meLhod has reLurn Lype anyLhlng oLher Lhan vold", all code paLhs ln lL must reLurn a value.
1he example below compllaLlon error because lf a==0 noLhlng ls menLloned as reLurn value:
sLaLlc lnL loo(lnL a)
[
lf (a != 0)
reLurn a,
}

Summary
ln Lhls secLlon we have covered mosL of Lhe C# language sLrucLures LhaL lncludes: uaLaLypes declaraLlon &
lnlLlallzaLlon, condlLlonal and looplng sLrucLures, worklng wlLh arrays & meLhods and enumeraLed daLaLypes.

You might also like