of 34

CC

Published on January 2017 | Categories: Documents | Downloads: 4 | Comments: 0
40 views

Comments

Content


C# PROGRAMS
At a Glance
1.
/* BASE:- this is used to pass the value from derived class constructor to the
base class constructor
*/
namespace base_k_usa!e
"
class A
"
public int a# public int b#
public A$%
" &
public A$int a' int b%
"
this.a ( a#
this.b ( b#
&
public void sub$%
"
int s ( a - b#
)onsole.*rite+ine$,subtraction of to numbers "-& and "1& results
".&,'a'b's%#
&
&
class B:A
"
public int a# public int b# public int c#
public B$int a' int b' int c%
: base$a' b% // B/ usin! this base method the value of a and b
from this contructor is transmitted o the constructor of the base class

"
this.a ( a#
this.b ( b#
this.c ( c#
&
public int add$%
"
int 0 ( a 1 b 1 c#
return 0#
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
B b ( ne B$16'7'8%# // )onstructor initiali0ed at the ob9ect
creation step
int :(b.add$%#
)onsole.*rite+ine$,sum is ,1:%#
b.sub$%# // the value from the derived class constr. ill !et
passed to base class constr. ith that base method usa!e
&
&
&
2.
namespace ;indlar!est
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"

)onsole.*rite+ine$,Enter an/ ten elements to compare for lar!est
number,%#
lar!e$%#

&

public static void lar!e$%
"
int45 input (" -' -' -' -' -' -' -' -' -' - &#
for $int i ( -# i < =# i11%
"
int a ( )onvert.>[email protected] $)onsole.Aead+ine$%%#
input4i5 ( a#
&
for $int k ( 1# k<=#k11 %
"
if $input4-5 B input4k5%
" &
else
"
// int temp#
// temp ( input4-5#
input4-5 ( input4k5#
// input4k5 ( temp#
&
& )onsole.*rite+ine$,+ar!est no. is, 1 input4-5%#
&

&
&
3.
class outk
"
public int add$int a' int b'out int o% // method havin! ar!ument and
return value ith out ke/ord in input ar!ument
"
o ( a 1 b#
return o#
&
&
namespace outke/ordus
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
int m# // variable declared for out variable
outk ok ( ne outk$%#
int 0([email protected]'C'out m%# // variable m declared ith ke/ord out
to !et from the function
)onsole.*rite+ine$0%#
&
&
&
4.
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
int45 list (" 1'.'@'8'6'7'C'D'=&#
)onsole.*rite+ine$,Even numbers,%#
for $int i ( -# i < =# i11%
"
if $list4i5E. (( -%
"
)onsole.*rite+ine$list4i5%#
&
& )onsole.*rite+ine$,Fdd numbers,%#
for $int i ( -# i < =# i11%
"
if $list4i5 E.G( -%
"
)onsole.*rite+ine$list4i5%#
&
&
&
&
5.
class 3atri:A
"
public int4'5 mata ( ne [email protected]' @5# //Heclarin! the matri: A to have @
ros and @ columns
public int i# public int 9#
public void matri:a$%
"
)onsole.*rite+ine$,Enter the 3atri: A elementsIn,%#
"
for $i ( -# i < @# i11%
"
for $9 ( -# 9 < @# 911%
"
mata4i' 95 ( )onvert.>[email protected]$)onsole.Aead+ine$%%#
&
&
&
&
&
class 3atri:B : 3atri:A // ?nterfacin! 3atri: A to 3atri: B
"
public int4'5 matb ( ne [email protected]' @5# //Heclarin! the matri: B to have @
ros and @ columns
public int i# public int 9#
public void matri:b$%
"
matri:a$%#
)onsole.*rite+ine$,Enter the 3atri: B elementsIn,%#
"
for $i ( -# i < @# i11%
"
for $9 ( -# 9 < @# 911%
"
matb4i' 95 ( )onvert.>[email protected]$)onsole.Aead+ine$%%#
&
&
&
&
&
class 3atri:Add : 3atri:B // ?nterfacin! the matri: B to this class
"
public int4'5 matc ( ne [email protected]' @5#//Heclarin! the matri: ) to have @
ros and @ columns
public int 9# public int i#
public void matadd$%
"
matri:b$%#
)onsole.*rite+ine$,3atri: A isIn,%#
for $i ( -# i < @# i11%
"
for $9 ( -# 9 < @# 911%
"
)onsole.*rite$,It, 1 mata4i' 95%#
& )onsole.*rite+ine$%# )onsole.*rite+ine$%#
& )onsole.*rite+ine$,3atri: B isIn,%#
for $i ( -# i < @# i11%
"
for $9 ( -# 9 < @# 911%
"
)onsole.*rite$,It, 1 matb4i' 95%#
& )onsole.*rite+ine$%# )onsole.*rite+ine$%#
& )onsole.*rite+ine$,3atri: ) isIn,%#
for $i ( -# i < @# i11%
"
for $9 ( -# 9 < @# 911%
"
matc4i' 95 ( mata4i' 95 1 matb4i' 95#
)onsole.*rite$,It, 1 matc4i' 95%#
& )onsole.*rite+ine$%# )onsole.*rite+ine$%#
&
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
3atri:Add ma ( ne 3atri:Add$%#
// mm.matri:a$%#accessin! the method b/ ob9ect
// mm.matri:b$%#
ma.matadd$%#// accessin! the parent classes throu!h the derived
class ob9ect b/ havin! the methods included in the class b/ interface
&
&
6.
class 3atri:A
"
public int4'5 mata ( ne [email protected]' @5# public int i#public int 9#
public void matri:a$%
"
)onsole.*rite+ine$,Enter the 3atri: A elementsIn,%#

"
for $i ( -# i < @# i11%
"
for $9 ( -# 9 < @# 911%
"
mata4i'95 ( )onvert.>[email protected]$)onsole.Aead+ine$%%#

&
&
&
&
&
class 3atri:B:3atri:A
"
public int4'5 matb ( ne [email protected]' @5# public int i# public int 9#
public void matri:b$%
"
)onsole.*rite+ine$,Enter the 3atri: B elementsIn,%#

"
for $i ( -# i < @# i11%
"
for $9 ( -# 9 < @# 911%
"
matb4i' 95 ( )onvert.>[email protected]$)onsole.Aead+ine$%%#
&
&
&
&
&
class 3atri:3ult:3atri:B // ?nterfacin! the matri: A to this class
"
public int4'5 matc ( ne [email protected]' @5# public int 9# public int i#
public void matmul$%
"
)onsole.*rite+ine$,3atri: A is,%#
for $i ( -# i < @# i11%
"
for $9 ( -# 9 < @# 911%
"

)onsole.*rite$,It,1mata4i' 95%#
& )onsole.*rite+ine$%# )onsole.*rite+ine$%#
& )onsole.*rite+ine$,3atri: B is,%#
for $i ( -# i < @# i11%
"
for $9 ( -# 9 < @# 911%
"

)onsole.*rite$,It,1matb4i' 95%#
& )onsole.*rite+ine$%# )onsole.*rite+ine$%#
& )onsole.*rite+ine$,3atri: ) is,%#
for $i ( -# i < @# i11%
"
for $ 9 ( -# 9 < @# 911%
"

matc4i'95(mata4i'95*matb4i'95#
)onsole.*rite$,It,1matc4i' 95%#
& )onsole.*rite+ine$%# )onsole.*rite+ine$%#
&
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
3atri:3ult mm ( ne 3atri:3ult$%#
mm.matri:a$%#
mm.matri:b$%#
mm.matmul$%#
&
&
7.
class parame:
"
public static void parra/$params int45 m%
" )onsole .*rite+ine$,arra/ elements,%#
foreach $int i in m%
" )onsole.*rite+ine$i%# &
&
&
namespace paramsusa!e
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
int45 : (" 1'.'@&#
parame:. parra/$:%#
parame:.parra/$%#
parame:.parra/$1--'.--%#
&
&
&
8.
class )lass?
"
public int :# public int /#
public )lass?$%
" )onsole.*rite+ine$J, )lass?:
Hefault )onstructor is emplo/ed in this class
)lass??:
addition process done and the result is
,%# &

&
class )lass???:)lass?
"
public )lass???$%
" )onsole.*rite+ine$,)lass ??? accessed,%# &
&
class )lass?? : )lass???
"
public )lass??$int a' int b%
" this.: ( a# this./ ( b# &
public void adder$%
" int c ( : 1 /#
)onsole.*rite+ine$c%#
&
&
namespace ?nheritancee:
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
)lass?? c. ( ne )lass??$6'6%#
c..adder$%#
&
&
&
9. SUM OF DIGITS
class SoHprocess
"
public void SoHcal$int a%
"
int b# int sum ( -#
hile $a G( -%
"
b ( a E 1-#
a ( a / 1-#
sum ( sum 1 b#
& )onsole.*rite+ine$,>he sum of di!its in !iven number is , 1 sum%#
&
&
namespace sumofdi!itsclass
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
int m#
)onsole.*rite+ine$,Enter a number,%#
strin! ah ( )onsole.Aead+ine$%#
m ( [email protected]$ah%#
SoHprocess sod ( ne SoHprocess$%#
sod.SoHcal$m%#
&
&
&
10.
namespace print_no_in_rev_order
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
int45 input ("1'.'@'8'6'7'C'D'='-&#
"
for $int i ( -# i < 1-# i11%
"
input4i5(?nt17 .2arse $ )onsole.Aead+ine$%%#
&
// Arra/.Sort$input%#// ascendin! sort
Arra/.Aeverse$input%#
foreach $int 9 in input%
"
)onsole .*rite+ine $9%#
&
&
&
&
&
11.
class ?nput
"
public int :# public int /#
public ?nput$%
" &
/* public ?nput$int a' int b%
"
this.: ( a#
this./ ( b#
&*/
public void printer$%
"
for $int i ( 1# i <( 6--# i11%
"
)onsole.*rite+ine$i%#
if $i (( 1--%
"
)onsole.*rite+ine$,1-1 to 1== skipped,%#
i ( .--#
&
else if $i (( @--%
"
)onsole.*rite+ine$,@-1 to @== skipped,%#
i ( 8--#
&
&
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
?nput i ( ne ?nput$%#
i.printer$%#
&
&
12. SWAPPING OF TWO NUMBERS
class Sap?nput
" public int s# public int t#
public Sap?nput$% // Hefault constructor
" &
public Sap?nput$int :' int /% // )onstructor ith to ar!uments
"
this.s ( :#
this.t ( /#
&
KSap?nput$% // Hestructor
" &
public void sapper$%
"
int temp#
)onsole.*rite+ine$J,Before sappin! :(, 1 s 1 ,InItIt/(, 1 t%#
temp ( s#
s ( t#
t ( temp#
)onsole.*rite+ine$J,After sappin! :(, 1 s 1 ,InItIt/(, 1 t%#
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
Sap?nput si ( ne Sap?nput$6-'6%#
si.sapper$%#
&
&
13.
class methodre!ion
"
// public int a# public int b#
public void add$int :' int / 'out int c%
"
c(:1/#
)onsole.*rite+ine$c%#
&
public void sub$int a' int b%
"
int 0(a-b#
)onsole.*rite+ine$0%#
&
&
class 3ain2ro!ram
"
static void 3ain$strin!45 ar!s%
"
methodre!ion mr ( ne methodre!ion$%#
int o#
)onsole.*rite+ine$,Enter 1 to add and . to sub,%#
int m()onvert.>o?nt17 $)onsole.Aead+ine$%%#
sitch $m%
"
case 1:
mr.add$6'6'out o%# // invokin! the method add from the
class methodre!ion
break#
case .:
mr.sub$1-' 6%# // invokin! the method sub from the class
methodre!ion
break#
default:
break#
&
&
&
14. SORTING NUMBERS IN ASCENDING ORDER
class Sort
"
/* public Sort$int a' int b' int c'int d%
" this.a ( a# this.b ( b# this.c ( c# this.d ( d# &*/
public void sorter$int45 n%
"
for $int i ( -# i < 8# i11%
"
for $int 9 ( i11# 9 <8 # 911%
"
if $n4i5 B n495%
"
int t ( n4i5#
n4i5 ( n495#
n495 ( t#
&
else
"
&
& )onsole.*rite+ine$n4i5%#
&
&
&
namespace sortin!numbers
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
Sort s ( ne Sort$% #
int45 num (" ..'1-'7C'8&#
)onsole.*rite+ine$,Before sortin!, %#
foreach $int ar in num%
" )onsole.*rite+ine$1ar%#
&
)onsole.*rite+ine$,>he ascendin! sorted numbers are,%#
s.sorter$num%#

&
&
&
15. TWO DIMENSIONAL ARRAY DECLARING AND INITIALIATION
class >oHArra/
"
public int4'5arr(ne int4.'.5""1'1&'".'.&&#// ?nitiali0in! the to
dimensional arra/
public strin!4'5 name ( ne strin!4.' .5 " " ,venkatesh,',venkat, &' "
,venu,',vee, & &#
public void arra/dis$%
"
foreach$ob9ect i in arr%
")onsole .*rite+ine$i%#&
&
public void arra/dis1$%
" foreach $ob9ect i in name %
)onsole .*rite+ine $i%#
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
>oHArra/ td ( ne >oHArra/$%#
td.arra/dis$%#
td.arra/dis1$%#
&
&
16. FINDING LARGEST NUMBER
public class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
)onsole.*rite+ine$,Enter an/ ten elements to compare for lar!est
number,%#
lar!e$%#

&

public static void lar!e$%
"
int45 input (ne input41-5#
for $int i ( -# i < =# i11%
"
int a ( )onvert.>[email protected] $)onsole.Aead+ine$%%#
input4i5 ( a#
&
for $int k ( 1# k<=#k11 %
"
if $input4-5 B input4k5%
" input4-5 ( input4k5#&
else
" &
& )onsole.*rite+ine$,Smallest no. is, 1 input4-5%#
&

&
17. BAN! ACCOUNT MGMT USING CONSTRUCTOR
class Acc3!mt
"
public int amt#
public Acc3!mt$int :% // )onstructor ith one ar!ument
"
this.amt ( :#
&
public strin! name ( ,Lenkatesh,#
public void benM$% // method for balance enMuir/
"
)onsole.*rite+ine$,InName : "-&In Balance : "1&,'name 'amt %#
&
public void ithdral$int % // method for mone/ ithdral
"
if $ B amt%
)onsole.*rite+ine$,Not sufficient balance /ou have onl/ As:
"-& in /our account,' amt%#
else
"
int b ( amt - #
)onsole.*rite+ine$,Balance after /our last transaction is, 1
b%#
&
&
public void deposit$% // method for deposition
"
)onsole.*rite+ine$,Enter the amount to deposit,%#
int d ( ?nt17.2arse$)onsole .Aead+ine $%%#
int b ( d 1 amt#
)onsole.*rite+ine$,Oour balance after /our deposition of As "-& is
"1&,'d'b%#
&
&
// Start of 3ain 2ro!ram
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
Acc3!mt am ( ne Acc3!mt$1----%# // passin! the value to the
constructor b/ ob9ect
)onsole.*rite+ine$,ItItIt*elcome to 3/ BankInIn,%#
start:
)onsole.*rite+ine$,Select /our choice,%#

)onsole.*rite+ine$J,
1.Balance EnMuir/
..)ash *ithdral
@.Heposition
8.E:it,%#
int i(?nt17.2arse$)onsole.Aead+ine $%%#
sitch $i%
"
case 1:
am.benM$%#
)onsole.*rite+ine$,*ould /ou like to have an other
transaction. ?f /es press O or press an/ ke/ to e:it,%#
strin! s()onsole.Aead+ine$%#
int n ( strin!.)ompare$s',O,%#
int nn ( strin!.)ompare$s' ,/,%#
if$n((- PP nn((-%
!oto start#
else
break#
case .:
)onsole.*rite+ine$,Enter the amount to ithdra ,%#
int ( ?nt17.2arse$)onsole .Aead+ine $%%#
am.ithdral$%#
)onsole.*rite+ine$,*ould /ou like to have an other
transaction. ?f /es press O or press an/ ke/ to e:it,%#
strin! s1 ( )onsole.Aead+ine$%#
int n1 ( strin!.)ompare$s1' ,O,%#
int n11 ( strin!.)ompare$s1' ,/,%#
if $n1 (( - PP n11((-%
!oto start#
else
break#
case @:
am.deposit$%#
)onsole.*rite+ine$,*ould /ou like to have an other
transaction. ?f /es press O or press an/ ke/ to e:it,%#
strin! s. ( )onsole.Aead+ine$%#
int n. ( strin!.)ompare$s.' ,O,%#
int n.1 ( strin!.)ompare$s.' ,/,%#
if $n. (( - PP n.1((-%
!oto start#
else
break#
case 8:
break#
default:
)onsole .*rite+ine $,Sorr/ ron! option select the correct
choice,%#
!oto start#
&
&
&
18. FACTORIAL USING CLASS
class factorial
"
public void fact$int a%
"
int b ( a# int c ( a#
"
for $int i ( 1# i < c# i11%
"
b ( b - 1#
a ( a * b#
//a(a*$b-1%#
//b--#
&
& )onsole.*rite+ine$,;actorial for:, 1 b 1 ,is, 1 a%#
&
&
namespace facte:
"
class 3Ain2!m
" public static void 3ain$strin! 45 ar!s%
" factorial fact(ne factorial $%#
)onsole.*rite+ine$,Ehter a value to find the factorial,%#
strin! it()onsole .Aead+ine $%#

int i ( [email protected]$it%#
)onsole.*rite+ine$%#
fact.fact$i%#
&
&
&
19. SUM OF S"UARE OF DIGITS
class sMuaredi!it
"
int :#
public sMuaredi!it$int a%
" this. : ( a# &
public void process$%
"
int ss ( -#
hile $: G( -%
"

int / ( : E 1-#
: ( : / 1-#

ss ( ss 1 $/*/%#
& )onsole.*rite+ine$ss%#
&
&
namespace sMuareofdi!it
"
class 3ain2!m
"
public static void 3ain$strin!45 ar!s%
"
sMuaredi!it sd ( ne sMuaredi!it$66%#
sd.process$%#
&
&
&
20. SCOPE AND #ISIBILTY OF PRI#ATE AND PUBLIC MEMBERS
// Shoin! the scope of private and public members in class
namespace scope_of_pub_pri_mem
"
class A
"
int a# int b# int c# // private members declares and hence the scope
of the members are ithin this class A onl/ and the method usin! this private
members cannot be inherited nor accessed b/ the ob9ect of the derived class
public A$int :' int /%
"
this.a ( :# this.b ( /#
&
public int add$%
"
return c ( a 1 b#
&
&

class B
"
public void sub$ int :' int /%
"
//add$%# //>he method cannot be called here since the members
involved in this are private
int c#
//return c ( : - /#
c ( : - /#
)onsole.*rite+ine$,Subtraction result : ,1c%#
&
&
class ):B // class B is inherited in to this class
"
public int a# public int b# public int c#
public void mul$int a'int b%
" sub$a' b%# // the members a and b since declared as public it is
passed to the method sub$% hich is in class B
c(a*b#
)onsole .*rite+ine $,3ultiplication result: ,1c%#
&
&

class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
A a ( ne A$6'6%#
int m(a.add$%#
)onsole.*rite+ine$,Addition result : , 1 m%#

B b ( ne B$%#
// b.sub$%#// cannot be accessed b/ ob9ect b of class B since it
takes the input ar!uments from the class )
// int 0(b.sub$86'6%#
// )onsole.*rite+ine$,Subtraction result : ,10%#
// b.add$%# //cannot be accessed b/ the ob9ect b since it cannot
pass an/ ar!uments to the private members of the class
) c(ne ) $%#
c.mul$16'C%#
&
&
&
20. SWAPPING OF TWO NUMBERS WIT$OUT USING A TEMPORARY #ARIABLE
namespace sapof.numithouttempvaar
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
int a# int b#
a ( )onvert.>o?nt17$)onsole.Aead+ine$%%#
b ( ?nt17.2arse$)onsole.Aead+ine$% %#
)onsole.*rite+ine$,Before sappin! a("-& and b("1&,' a' b%#

a ( a 1 b#
b ( a-b#
a ( a-b#

)onsole.*rite+ine$,After sappin! a("-& and b("1&,' a' b%#
&
&
&
21. SIMPLE ARRAYLIST E%AMPLE
usin! S/stem#
usin! S/stem.)ollections#// Aemove the !eneric from it to make arra/list to
ork
usin! S/stem.>e:t#
namespace Arra/+iste:
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
Arra/+ist a ( ne Arra/+ist$%#// Heclarin! the arra/list
a.Add$,a,%# // Addin! elements to arra/list
a.Add$,b,%#
a.Add$,c,%#
foreach $strin! s in a%
"
)onsole.*rite+ine$s%#
&
&
&
&
22. ARRAYLIST E%AMPLE USING CLASS
usin! S/stem#
usin! S/stem.)ollections#// Aemove the !eneric from it to make arra/list to
ork
usin! S/stem.>e:t#
// Arra/+ist class us!e: Accessed b/ the ob9ect of the main class
namespace Arra/+iste:.
"
class Arra/+istE:ample // )lass defined for declarin! the arra/ list
"
public void !etdata$%
"
Arra/+ist al ( ne Arra/+ist$%# //Arra/+ist declaration
al.Add$,venkatesh,%# // Addin! elements to arra/list
al.Add$1%# // Addin! elements to arra/list
al.Add$,name,%# // Addin! elements to arra/list
foreach $ob9ect i in al%
"
)onsole.*rite+ine$i%#
&
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
Arra/+istE:ample ale ( ne Arra/+istE:ample$%#
ale.!etdata$%#
&
&
&
23. STRING SORTING USING ARRAY LIST
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
Arra/+ist name ( ne Arra/+ist$%#// Arra/ list declaration
name.Add$,Lenkatesh,%# // Addin! elements startin! from the inde:
-
name.Add$,Babu,%#
name.Add$,Qumar,%#
name.Add$,Aamesh,%#
)onsole .*rite+ine $,InBefore Sortin!In,%#
foreach $strin! 9 in name%
"
)onsole.*rite+ine$9%#
&
)onsole.*rite+ine$,InAfter sortin!In,%#
name.Sort$%#
foreach $strin! i in name%
"
)onsole.*rite+ine$i%#
&
&
&
24. STRING SORTING USING ARRAY
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
strin!45 name (" ,venkat,',kumar,',0ebastin,',karna,&#
)onsole.*rite+ine$,Before Sortin! ,%#
foreach $strin! 9 in name%
"
)onsole.*rite+ine$9%#
&
)onsole.*rite+ine$,After Sortin! ,%#
Arra/.Sort$name%#
foreach $strin! i in name%
"
)onsole.*rite+ine$i%#
&
&
&
25.
// 2AFRAA3 >F SSF* SF* >F A))ESS >SE 3E3BEA *S?)S ?S HE)+AAEH AS A S>A>?)
3E3BEA
class Stat3emAcc
"
public static void disp$%
" )onsole.*rite+ine$,Static member is accessed,%# &
&
namespace staticmemberaccess
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
Stat3emAcc.disp$%#
&
&
&
26.
class strin!arra/
"
public static void 3ain$%
" strin!45 name(ne strin!45 ",3on,' ,>ue,' ,*ed,&#
"foreach$ob9ect i in name%
")onsole.*rite+ine$i%#&
&
&
&
27.
// S>AT)>TAE is a value t/pe
// ?t cannot be inherited
// S>AT)>TAE can have constructor and methods as like )+ASS.
// ?t does not support default constructor
// ?t does not support ?NSEA?>AN)E
namespace Structure.
"
public struct baseclass
"
public int c# public int :# public int /#
// public baseclass$% // default constructor not possible in
structures
//" &
public baseclass$int a' int b%
"
// this.: ( a# UthisU ke/ord is reMuired for assi!nin! for the
variable hich are alike
// this./ ( b#
// this.c ( a 1 b#
: ( a#
/ ( b#
c ( a 1 b#
&
public void disp$baseclass b%
"
baseclass m#
m.c ( :1b.c#
m.: ( : 1 b.:#
m./ ( / 1 b./#
)onsole.*rite+ine$,c("-&In:("1&In/(".&,'m.c'm.:'m./%#
&
&
class 3ain2ro!ram
"
static void 3ain$strin!45 ar!s%
"
baseclass b ( ne baseclass$6' 8%#// structure initiali0ation
b.disp$b%#
)onsole.*rite+ine$b.c%#
&
&
&
28.
namespace structe:@
"
struct details
"public strin! name# public strin! adr# public int phn#public int pin#
public strin! ct/#
public details$strin! n' strin! adr' int phn' int pin' strin! ct/%
"
this.name ( n#
this.adr ( adr#
this.phn ( phn#
this.pin ( pin#
this.ct/ ( ct/#
&
public void !etdet$details d%
"
// )onsole.*rite+ine$, Name: "-&InAddr: "1&In )it/: ".&In 2in no. :
"@&In 2hone no. :"8&In,'d.name 'd.adr'd.ct/ 'd.pin 'd.phn %#
)onsole.*rite+ine$, Name: "-&InAddr: "1&In )it/: ".&In 2in no. :
"@&In 2hone no. :"8&In,' name' adr' ct/' pin' phn%#
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
)onsole.*rite+ine$,Enter /our name,%#
strin! a ( )onsole.Aead+ine$%#
)onsole.*rite+ine$,Address,%#
strin! b ( )onsole.Aead+ine$%#
)onsole .*rite+ine $,cit/,%#
strin! c ( )onsole.Aead+ine$%#
)onsole.*rite+ine$,2in no,%#
int d ( [email protected]$)onsole .Aead+ine $%%#
)onsole.*rite+ine$,phone no.,%#
int e ( [email protected]$)onsole .Aead+ine $%%#
details dt (ne details $a'b'e'd'c%#
dt.!etdet$dt%#
&
&
&
Output:
Enter /our name
Lenkatesh
Address
!eetha na!ar
cit/
trich/
2in no
7.--1C
phone no.
-
Name: Lenkatesh
Addr: !eetha na!ar
)it/: trich/
2in no. :7.--1C
2hone no. :-
29.
namespace Structure_e:ample
"
struct E:ample
"
public int :# public int /#
/* public E:ample$%  Structures cannot have a default constructor
" &*/
public E:ample$int a' int b% // But structures can have the
constructor ith ar!uments
"
this.: ( a#
this./ ( b#
&
public E:ample structaccess$E:ample e% // Structure emplo/ed as a
datat/pe for both the input and output ar!uments
"
E:ample e:# // ob9ect created for the structure
e:.: ( : 1 e.:# // Accessin! the variable b/ ob9ect
e:./ ( / 1 e./#
return e:# // returnin! the ob9ect
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
E:ample el ( ne E:ample$6' 6%# // invokin! the constructor class
ith input
E:ample v#
v(el.structaccess$el%#
)onsole.*rite+ine$v.: 1 v./%#
&
&
&
30.
class RetArra/+ist
"//public strin! list#
public void !etdata$%
"
Arra/+ist list ( ne Arra/+ist$%#
list.Add$,?nde: -: ?tem1,%#
list.Add$,?nde: 1: ?tem.,%#
list.Add$,?nde: .: [email protected],%#
list.Add$,?nde: @: ?tem8,%#
disp$list%#
)onsole.*rite+ine$,)ount, 1 list.)ount%#
)onsole.*rite+ine$,Aemovin! the item ftom the inde: @,%#
[email protected]%#
disp$list%#
)onsole.*rite+ine$,)ount,1list .)ount %#
)onsole.*rite+ine$,?nsertin! a ne item at inde: .,%#
list.?nsert$.' ,ne item inserted here,%#
disp$list%#
)onsole.*rite+ine$,)ount, 1 list.)ount%#
)onsole.*rite+ine$,2rintin! the arra/ list in reverse order,%#
list.Aeverse$%#
disp$list%#
)onsole.*rite+ine$,)ount, 1 list.)ount%#
)onsole.*rite+ine$,Sortin! the arra/ list,%#
list.Sort$%#
disp$list%#
)onsole.*rite+ine$,Aemovin! the inde: b/ ran!e from - to .,%#
list.AemoveAan!e$-' .%#
)onsole.*rite+ine$,)learin! all contents from arra/ list,%#
)onsole.*rite+ine$,All contents are cleared successfull/,%#
list.)lear$%#
disp$list%#
&
/* public void cleararra/list$%
"
!etdata$%#

&*/
public static void disp$Arra/+ist list%
"
foreach $ob9ect i in list%
"
)onsole.*rite+ine$i%#
&
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
RetArra/+ist ! ( ne RetArra/+ist$%#
!.!etdata$%#

&
&
31.
class sortdescend
"
public void Hscendin!Sort$params int45 :%
"for$int i(-#i<C#i11%
"
for $int 9 ( i 1 1# 9 < C# 911%
"
if $:4i5 < :495%
"
int temp ( :4i5#
:4i5 ( :495#
:495 ( temp#
&
else
" &
& )onsole.*rite+ine$:4i5%#
&
&
&
namespace descendin!sort
"
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
sortdescend sd ( ne sortdescend$%#
int45 m (" [email protected]'11'C7'1'=='67'C='1--&#
)onsole.*rite+ine$,Before sortin!,%#
foreach $ob9ect i in m%
" )onsole.*rite+ine$i%# &
)onsole.*rite+ine$,After sortin!,%#
sd.Hscendin!Sort$m%#
//sd.Hscendin!Sort$.'@'8'6'7'C'D'=%#
&
&
&
32.
namespace dele!atessamplep!m
"
class 2ro!ram
"
dele!ate int delmtd$int a'int b%#
static void 3ain$strin!45 ar!s%
"
delmtd dm ( ne delmtd$add%#
int a(6#int b(7#
int r(dm$a'b%#
)onsole.*rite+ine$r%#
&
static int add$int a' int b%
"
int 0(a1b#
return 0#
//)onsole.*rite+ine$0%#
&
&
&
33.
// As a dele!ate method usa!e it can be used as a substitute for the method
hich looks similar as dele!ate method ith the same data/pe of return value
and no. of input ar!uments
namespace Hele!ate_3ultipleaccess
"
class 2ro!ram
"
public dele!ate int delmulacc$int a' int b'int c%# //delclarin! the
dele!ate
static void 3ain$strin!45 ar!s%
"
delmulacc dma ( null# // ?nitiali0in! the dele!ate to null value
dma ( ne delmulacc$add%#

// dma 1( ne delmulacc$sub%# ?> shos error since the return t/pe and no. of
input ar!uments doesnot matchith the dele!ate

dma1( ne delmulacc$mult %#

//dma 1( ne delmulacc$div%# it is erroneous since the datat/pe of the
dele!ate and this function mismatches

int s ( 16# int t ( D# int u ( 1-#

/* 3ethod ? in accessin! methods b/ dele!ate class
// int r ( dma$s't'u%#
// )onsole.*rite+ine$r%#
*/
// 3ethod ?? in accessin! the methods b/ dele!ate class
delmet$6'8'C'dma%#
&
static void delmet$int s'int t' int u'delmulacc dma1% // dma1 is the
ob9ect created as like dma created earlier
"
int r ( dma1$s't'u%#
)onsole.*rite+ine$r%#
&
static int add$int a' int b'int c%
"
return a 1 b1c#
&
static void sub$int a' int b%
"
)onsole.*rite+ine$,a1b(, 1 $a 1 b%%#
&
static int mult$int a' int b' int c%
"
return a * b*c#
//)onsole.*rite+ine$,a*b(,1$a*b%%#
&
static float div$float a' float b'float c%
"
return $a 1 b%/c#
&
&
&
34.
class 2ro!ram
"
static void disp$%
" )onsole.*rite+ine$,Static member is accessed,%# &
public static void 3ain$strin!45 ar!s%
"
disp$%# // static member is accesses b/ its class not b/ the ob9ect of
main class
&
&
35.
class >oHArra/
"
public int4'5arr(ne int4.'.5""1'1&'".'.&&#// ?nitiali0in! the to
dimensional arra/
public strin!4'5 name ( ne strin!4.' .5 " " ,venkatesh,',venkat, &' "
,venu,',vee, & &#
public void arra/dis$%
"
foreach$ob9ect i in arr%
")onsole .*rite+ine$i%#&
&
public void arra/dis1$%
" foreach $ob9ect i in name %
)onsole .*rite+ine $i%#
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
>oHArra/ td ( ne >oHArra/$%#
td.arra/dis$%#
td.arra/dis1$%#
&
&
36.
// ?nterface is used for implementin! multiple inheritance
// ?n the interface it onl/ contains the defition for variables used and
methods emplo/ed in the derived class
namespace interface_e:
"
interface A
"
int m // Hefinin! the variables ith !et"& set"& propert/
" !et# set#&
int n
" !et# set#&
// Hefinin! the methods
void dispa$%#
void add$int m' int n%#
&
class B : A
"
public int :# public int /#
void A.dispa$% // Accessin! the method dispa from
interface A
"
)onsole.*rite+ine$,?Um ?nterface A,%#
&
public B$int a' int b% // )onstructor to !et and set values to
m and n
"
this.:(a#
this./(b#
&
int A.m // settin! value of : from constructor to
m
"
!et " return :# &
set":(value #&
&
int A.n // settin! value of : from constructor to
n
"
!et "return /#&
set "/(value #&
&
void A.add$int m' int n% //Accessin! the method add from
interface A ith input ar!uments m and n from interface A
"
int 0(m1n#
)onsole .*rite+ine $,sum is,10%#
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
B m ( ne B$6'6%# // creatin! ob9ect for derived class as usual
A a($A%m# // creatin! ob9ect to interface ith ref. to
derived class ob9ect
a.dispa$%# // accessin! the interface A methhod usin!
interface ob9ect. Note interface methods and members cannot be accessed b/ the
derived class ob9ects
a.m ( 6#
a.n ( 7#
a.add$a.m'a.n%#
)onsole.*rite+ine$,m(,1a.m1,Inn(,1a.n%#
&
&
&
37.
interface A
"
int m1
" !et# set#&

&
interface B:A
"
ne void m1$%# // hidin! the method m1 b/ puttin! ne before of it
&
class ) : B
"
private int :#
int A.m1 // assi!nin! the value to m1 int he interface m1 from
:
"
!et " return :# &
set " : ( value# &
&
void B.m1$%
"
)onsole.*rite+ine$,31,%#
&
&

class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
) c(ne )$% #
B m11 ( $B%c# //creatin! ob9ect for interface B ithrespect to the
derived class )
A m1. ( $A%c# //creatin! ob9ect for interface A ithrespect to the
derived class )
m1..m1 ( [email protected]# // Accessin! the method m1 of b/ ob9ect of A
m11.m1$%# // Accessin! the method m1 of b/ ob9ect of B
)onsole.*rite+ine$m1..m1%#
&
&

@D.
// 3T+>2+E ?NSEA?>AN)E
namespace )[email protected]
"
interface A
"
int m1
" !et# set#&

&
interface B
"
ne void m1$%# // hidin! the method m1 b/ puttin! ne before of it
&
class ) : B'A // 3ultiple inheritance
"
private int :#
int A.m1 // assi!nin! the value to m1 int he interface m1 from
:
"
!et " return :# &
set " : ( value# &
&
void B.m1$%
"
)onsole.*rite+ine$,31,%#
&
&

class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
) c(ne )$% #
B m11 ( $B%c# //creatin! ob9ect for interface B ithrespect to the
derived class )
A m1. ( $A%c# //creatin! ob9ect for interface A ithrespect to the
derived class )
m1..m1 ( [email protected]# // Accessin! the method m1 of b/ ob9ect of A
m11.m1$%# // Accessin! the method m1 of b/ ob9ect of B
)onsole.*rite+ine$m1..m1%#
&
&

&
39. MULTIPLE IN$ERITANCES&
interface A
"
int m
"
!et#
set#
&
int n
"
!et#
set#
&
&
interface B
"
void add$%#
&
class ):B'A // ?NSEA?>EH *?>S 3T+>?2+E ?N>EA;A)ES A V B
"private int a# private int b#
public )$int a' int b%
"
this.a ( a#
this.b ( b#
&
int A.m
"
!et " return a# & // returns the value to m but as a variable a
set " a ( value# &
&
int A.n
"
!et " return b# & // returns the value to n but as a variable b
set " b ( value# &
&

void B.add$%
"
int 0 ( a 1 b# // here a is m and b is n. the constructor values
are assi!ned to m and n
)onsole.*rite+ine$,a(,1a%#
)onsole.*rite+ine$,b(,1b%#
)onsole.*rite+ine$,0(,10%#
&
&
class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
) c ( ne )$16'6%#
A a ( $A%c#
B b ( $B%c#
// a.m ( 6# m is !iven 6
// a.n ( 7# n is !iven 7 tr/in! ith this m and n values ill
overrite the constructor value and /ield the reults as 11
b.add$%#
)onsole.*rite+ine$,m(,1a.m1,Inn(,1a.n%#// to sho the value of m
and n
&
&
Futput:
a(16
b(6
0(.-
m(16
n(6
40.
class Arra/_initiali0in!_ith_si0e
"
static void 3ain$strin!45 ar!s%
"
int45 no(ne int 465# // Arra/ declared ith si0e of
five
)onsole.*rite+ine$,Enter 6 elements,%#
for $int i ( -# i < 6# i11%
"
no4i5 ( int.2arse$)onsole .Aead+ine $%%#
&
foreach $int 9 in no%
"
)onsole.*rite+ine$,>he arra/ elements are ,19%#
&
&
&
Futput:
Enter 6 elements to find its sMuare root
7
8
D
@
=
SMuare root of 7 is [email protected]
SMuare root of 8 is .
SMuare root of D is ..D.D8.C1.8C871=
SMuare root of @ is [email protected]
SMuare root of = is @
41. DELEGATES IN CLASS
public dele!ate void sampleHele!ate$ %#
class sample)lass1
"
public static void sample3ethod$ %
"
)onsole.*rite+ine$,E:ecutin! sample3ethod of sample)lass1,%#
&
&
class sample)lass.
"
static void 3ain$ %
"
sampleHele!ate dele1 ( ne
sampleHele!ate$sample)lass1.sample3ethod%#
dele1$%#
&
&
Output:
E:ecutin! sample3ethod of sample)lass1
42. BASE !EYWORD USAGE
class A
"
public int :# public int /#
public A$int a' int b%
"
this.: ( a#
this./ ( b#
&
public void displa/a$%
"
)onsole.*rite+ine$,a("-& and b("1&,' :' /%#
&
&
class B:A
"
public int :# public int /# public int 0#
public B$int a' int b' int c%
: base$a' b% // value passes to its base class A
"
this.: ( a#
this./ ( b#
this.0 ( c#
&
public void displa/b$%
"
)onsole.*rite+ine$,a("-& and b("1& and c(".&,' :' /'0%#
&
&
class ):B
"
public int m# public int n# public int o# public int p#
public )$int a' int b' int c' int d%:base$a'b'c% // value passes to
its base class B
"
this.m ( a#
this.n ( b#
this.o ( c#
this.p ( d#
&
public void displa/c$%
"
)onsole.*rite+ine$,a("-& and b("1& and c(".& and d("@&,' m' n'
o'p%#
&
&

class 2ro!ram
"
static void 3ain$strin!45 ar!s%
"
) c ( ne )$6'@'C'1-%#
c.displa/c$%#
c.displa/b$%#
c.displa/a$%#
&
&
Output:
a(6 and b(@ and c(C and d(1-
a(6 and b(@ and c(C
a(6 and b(@

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close