You are on page 1of 22

SATHYA TECHNOLOGIES

Methods :
1. What is Method ? A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. 2. What is the static method ? When a method declaration includes a static modifier, that method is said to be a static method and a static method invoked using class name. 3. What is the Instance method? When a method declaration no static modifier, that method is said to be an instance method. An instance method invoked using instance of the class. 4. What is the Difference between static method and instance method ? The difference between static and instance method is that multiple instance of a class can be created and each instance has its own separate method . where as a method is static ,there are no instance of that method and invoke only that one definition of the static method. 5. What is the differenent method parameters? There are 4 method parameters .value parameters !.ref parameters. ".out parameters. 4.param array parameters 6. What is the Name pace ?

!"ass:
#. What is the c"ass ? A class consist of data and behavior . data represents its fields and behavior represents its methods. $. What is the c"ass members?
KannaBabu Page 1

SATHYA TECHNOLOGIES
#lass member are .fields !.methods "properties 4.events $.indexers %.constructors

%. 1&. What is the static c"ass and instance c"ass ? When a class member includes static modifiers that member is called static member and static member invoked using class name.

When a class member no static modifiers that member is called instance method and instance member invoked using ob&ect of the class.

11. What is the 'se of c"ass constr'ctor? The purpose of the class constructor is to initiali'e class fields. A class constructor is automatically called when instance of class is created.

(nheritance
12. What is the inheritance? #reating new classes from existing classes such that the new classes will acquire all the features of existing classes is called inheritane. )ain advantage of inheritance is re'sabi"it( of the code
13. What are the 4 pi""ars of an( ob)ect oriented pro*rammin* "an*'a*e?

1. Abstraction
KannaBabu Page 2

SATHYA TECHNOLOGIES
2. (nheritance 3. *ncapsulation 4. +olymorphism
14. Do str'cts s'pport inheritance?

,o, structs do not support inheritance, but they can implement interfaces.
15. What is the main ad+anta*e of 'sin* inheritance?

#ode reuse
16. Is the fo""owin* code "e*a"?

class ChildClass : ParentClassA, ParentClassB { } ,o, a child class can have only one base class. -ou cannot specify ! base classes at the same time. #. supports single class inheritance only. Therefore, you can specify only one base class to inherit from. /owever, it does allow multiple interface inheritance.
17. What wi"" be the o'tp't of the fo""owin* code?

using System; public class BaseClass { public BaseClass() { Console !rite"ine(#$ am a base class#); } } public class ChildClass : BaseClass { public ChildClass() { Console !rite"ine(#$ am a child class#); } static %oid &ain() { ChildClass CC ' ne( ChildClass(); }} ,'tp't: ( am a base class ( am a child class This is because base classes are automatically instantiated before derived classes. ,otice the output, The 0ase#lass constructor executed before the #hild#lass constructor.

KannaBabu

Page 3

SATHYA TECHNOLOGIES
1. Does !- s'pport m'"tip"e c"ass inheritance? ,o, #. supports single class inheritance only. /owever classes can implement multiple interfaces at the same time. 19. What is the in*"e Inheritance when a single derived class is created from a single base class then the inheritance is called as single inheritance.

2 . .ierarchica" Inheritance

when more than one derived class are created from a single base class, then that inheritance is called as hierarchical inheritance.

21. M'"ti /e+e" Inheritance

when a derived class is created from another derived class, then that inheritance is called as multi level inheritance.

22. .(brid Inheritance

Any combination of single, hierarchical and multi level inheritances is called as hybrid inheritance.

23. What is the Diamond prob"em or M'"tip"e Inheritance prob"em

KannaBabu

Page 4

SATHYA TECHNOLOGIES

polymorphisum
24. What is the po"(morphis'm +olymorphism allows to invoke derived class methods through a base class reference during run2time. (n base class method is declared +irt'a" and in the derived class override the same method.
25. When can a deri+ed c"ass o+erride a base c"ass member?

A derived class can override a base class member only if the base class member is declared as virtual or abstract
26. What is the difference between a +irt'a" method and an abstract method?

A virtual method must have a body where as an abstract method should not have a body
27. !an fie"ds inside a c"ass be +irt'a"?

,o, 3ields inside a class cannot be virtual. 4nly methods, properties, events and indexers can be virtual.
2!. !an (o' access a hidden base c"ass method in the deri+ed c"ass?

-es, /idden base class methods can be accessed from the derived class by casting the instance of the derived class to an instance of the base class .
29. What is the methodo+er"oadin*?

)ethod overloading allows a class to have multiple methods with the same name , but with a different signature. The signature of a method consist of the name of the method and type,kind and number of its formal parameters.ths signature of method does not include the reture type and param modifier.
3 . What is the methodhiddin* ?

31. What is the methodo+erriddin*?

32. What is the difference between method hiddin* and method o+erridin*?

(n the method overriding a base class reference variable pointing to a child class ob&ect will invoke the overridden method in the child class.
KannaBabu Page 5

SATHYA TECHNOLOGIES

In the method hidin* a base c"ass reference +ariab"e pointin* to a chi"d c"ass ob)ect wi"" in+o0e the hidden method in the base c"ass.

properties
33. What are 1roperties in !-. 23p"ain with an e3amp"e?

+roperties in #. are class members that provide a flexible mechanism to read, write, or compute the values of private fields. +roperties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

34. 23p"ain the 3 t(pes of properties in !- with an e3amp"e?

1. 5ead 4nly +roperties6 +roperties without a set accessor are considered read2only. 2. Write 4nly +roperties6 +roperties without a get accessor are considered write only. 3. 5ead Write +roperties6 +roperties with both a get and set accessor are considered read2 write properties.

35. What are the ad+anta*es of properties in !-?

1. +roperties can validate data before allowing a change. 2. +roperties can transparently expose data on a class where that data is actually retrieved from some other source such as a database. 3. +roperties can take an action when data is changed, such as raising an event or changing the value of other fields.

36. What are the 4et and et accessor ?

The code block for the get accessor is executed when the property is read and the code block for the set accessor is executed when the property is assigned a new value.
37. What is a static propert(. 4i+e an e3amp"e?

A property that is marked with a static keyword is considered as static property. This makes the property available to callers at any time, even if no instance of the class exists. (n the example below +( is a static property. using 7ystem8 class #ircle
KannaBabu Page 6

SATHYA TECHNOLOGIES
9 private static double :pi ; ". 48 public static double +( 9 get 9 return :pi8 < < < class )ain#lass 9 public static void )ain=> 9 #onsole.Write?ine=#ircle.+(>8 < <
3!. What is a +irt'a" propert(. 4i+e an e3amp"e?

A property that is marked with virtual keyword is considered virtual property. @irtual properties enable derived classes to override the property behavior by using the override keyword. (n the example below 3ull,ame is virtual property in the #ustomer class. 0ank#ustomer class inherits from #ustomer class and overrides the 3ull,ame virtual property. (n the output you can see the over riden implementation. A property overriding a virtual property can also be sealed, specifying that for derived classes it is no longer virtual. using 7ystem8 class #ustomer 9 private string :first,ame ; string.*mpty8 private string :last,ame ; string.*mpty8 public string 3irst,ame 9 get 9 return :first,ame8 < set 9 :first,ame ; value8
KannaBabu Page 7

SATHYA TECHNOLOGIES
< < public string ?ast,ame 9 get 9 return :last,ame8 < set 9 :last,ame ; value8 < < AA 3ull,ame is virtual public virtual string 3ull,ame 9 get 9 return :last,ame B C, C B :first,ame8 < < < class 0ank#ustomer 6 #ustomer 9 AA 4veriding the 3ull,ame virtual property derived from customer class public override string 3ull,ame 9 get 9 return C)r. C B 3irst,ame B C C B ?ast,ame8 < < < class )ain#lass 9 public static void )ain=> 9 0ank#ustomer 0ank#ustomer4b&ect ; new 0ank#ustomer=>8 0ank#ustomer4b&ect.3irst,ame ; CDavidC8 0ank#ustomer4b&ect.?ast,ame ; C0oonC8
KannaBabu Page !

SATHYA TECHNOLOGIES
#onsole.Write?ine=C#ustomer 3ull ,ame is 6 C B 0ank#ustomer4b&ect.3ull,ame>8 < <
39. What is an abstract propert(. 4i+e an e3amp"e?

A property that is marked with abstract keyword is considered abstract property. An abstract property should not have any implementation in the class. The derived classes must write their own implementation. (n the example below 3ull,ame property is abstract in the #ustomer class. 0ank#ustomer class overrides the inherited abstract 3ull,ame property with its own implementation.

using 7ystem8 abstract class #ustomer 9 private string :first,ame ; string.*mpty8 private string :last,ame ; string.*mpty8 public string 3irst,ame 9 get 9 return :first,ame8 < set 9 :first,ame ; value8 < < public string ?ast,ame 9 get 9 return :last,ame8 < set 9 :last,ame ; value8 < < AA 3ull,ame is abstract
KannaBabu Page 9

SATHYA TECHNOLOGIES
public abstract string 3ull,ame 9 get8 < < class 0ank#ustomer 6 #ustomer 9 AA 4veriding the 3ull,ame abstract property derived from customer class public override string 3ull,ame 9 get 9 return C)r. C B 3irst,ame B C C B ?ast,ame8 < < < class )ain#lass 9 public static void )ain=> 9 0ank#ustomer 0ank#ustomer4b&ect ; new 0ank#ustomer=>8 0ank#ustomer4b&ect.3irst,ame ; CDavidC8 0ank#ustomer4b&ect.?ast,ame ; C0oonC8 #onsole.Write?ine=C#ustomer 3ull ,ame is 6 C B 0ank#ustomer4b&ect.3ull,ame>8 < <
4 . !an (o' 'se +irt'a"5 o+erride or abstract 0e(words on an accessor of a static

propert(? ,o, it is a compile time error to use a virtual, abstract or override keywords on an accessor of a static property.

KannaBabu

Page 1

SATHYA TECHNOLOGIES

structures
41. What is the str'ct're?

Wi"" the fo""owin* code compi"e? using System; public class )*ample { static %oid &ain() { +estStruct + ' ne( +estStruct(); Console !rite"ine(+ i); } } public struct +estStruct { public int i',-; ..)rror: cannot ha%e instance /ield initiali0ers in structs } ,o, a compile time error will be generated stating Cwithin a struct declaration, fields cannot be initiali'ed unless they are declared as const or staticC !an a str'ct ha+e a defa'"t constr'ctor 6a constr'ctor witho't parameters7 or a destr'ctor in !-? ,o !an (o' instantiate a str'ct witho't 'sin* a new operator in !-? -es, you can instantiate a struct without using a new operator !an a str'ct inherit from another str'ct or c"ass in !-? ,o, a struct cannot inherit from another struct or class, and it cannot be the base of a class.
KannaBabu Page 11

SATHYA TECHNOLOGIES
!an a str'ct inherit from an interface in !-? -es 8re str'cts +a"'e t(pes or reference t(pes? 7tructs are value types. What is the base t(pe from which a"" str'cts inherit direct"(? All structs inherit directly from 7ystem.@alueType, which inherits from 7ystem.4b&ect.
42. What is the difference between str'ct're and c"ass? 1. 7truct is value type where as class is reference type 2. 7truct are stored on stack memory where as class are stores on heap memory. 3. @alues types are destroyed immediately after the scope is lost where as for reference

types only the reference variable destroyed after the scope is lost . the ob&ects is later destroyed by garbage collector. 4. tr'ct can9t ha+e destr'ctors where as c"ass ha+e destr'ctors. can inherit from an interface.

5. 7truct can not inherit from anthor class where as a class can. 0oth struct and class

Interface
43. 23p"ain what is an Interface in !-?

An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface
44. !an an Interface contain fie"ds?

,o, an (nterface cannot contain fields.


45. !an (o' create an instance of an interface?

,o, you cannot create an instance of an interface.


46. If a c"ass inherits an interface5 what are the 2 options a+ai"ab"e for that c"ass?

,ption 1: +rovide (mplementation for all the members inheirted from the interface. ,ption 2:6 (f the class does not wish to provide (mplementation for all the members inheirted from the interface, then the class has to be marked as abstract. 8 c"ass inherits from 2 interfaces and both the interfaces ha+e the same method name as shown be"ow. .ow sho'"d the c"ass imp"ement the dri+e method for both
KannaBabu Page 12

SATHYA TECHNOLOGIES
!ar and :'s interface? namespace (nterfaces 9 interface #ar 9 void Drive=>8 < interface 0us 9 void Drive=>8 < class Demo 6 #ar,0us 9 AA/ow to implement the Drive=> )ethod inherited from 0us and #ar < < To implement the Drive=> method use the fully qualified name as shown in the example below. To call the respective interface drive method type cast the demo ob&ect to the respective interface and then call the drive method. using 7ystem8 namespace (nterfaces 9 interface #ar 9 void Drive=>8 < interface 0us 9 void Drive=>8 < class Demo 6 #ar,0us 9 void #ar.Drive=> 9 #onsole.Write?ine=CDrive #arC>8 <
KannaBabu Page 13

SATHYA TECHNOLOGIES
void 0us.Drive=> 9 #onsole.Write?ine=CDrive 0usC>8 < static void )ain=> 9 Demo Demo4b&ect ; new Demo=>8 ==#ar>Demo4b&ect>.Drive=>8 ==0us>Demo4b&ect>.Drive=>8 < < < What do (o' mean b( ;23p"icit"( Imp"emetin* an Interface;. 4i+e an e3amp"e? (f a class is implementing the inherited interface member by prefixing the name of the interface, then the class is C*xplicitly (mplemeting an (nterface memberC. The disadvantage of *xplicitly (mplemeting an (nterface member is that, the class ob&ect has to be type casted to the interface type to invoke the interface member. An example is shown below. using 7ystem8 namespace (nterfaces 9 interface #ar 9 void Drive=>8 < class Demo 6 #ar 9 AA *xplicit implementation of an interface member void #ar.Drive=> 9 #onsole.Write?ine=CDrive #arC>8 < static void )ain=> 9 Demo Demo4b&ect ; new Demo=>8
KannaBabu Page 14

SATHYA TECHNOLOGIES
AADemo4b&ect.Drive=>8 AA *rror6 #annot call explicitly implemented interface method AA using the class ob&ect. AA Type cast the demo ob&ect to interface type #ar ==#ar>Demo4b&ect>.Drive=>8 < < <

47. What is the difference between 8bstract c"ass and Interface?

.Abstract class can have implementation for some of its members but the interface can not have implementation for any of its methods. !.(nterface can not have fields where as Abastract class can have fields. ". An interface can inherit another interface on"( where as an 8bstract c"ass can inherit another abstract c"ass or another interface. 4. abstract c"ass members can ha+e 8ccess modifiers where as interface members can not ha+e access modifiers.
4!. What is the 23ception in !-?

An *xception is a unforeseen error that occur when program is runnig.

4E. . 23p"ain <r(=catch b"oc0 of e3ception hand"in*. 8nswer -ou can enclose code in TryA#atchA3inally block. -ou can catch all exceptions in the catch block. The third part of this block is finally. (t is executed irrespective of the fact that an exception has been raised

Access Modifiers
5 . What are the differenct 8ccess modifiers? KannaBabu Page 15

SATHYA TECHNOLOGIES
There are $ modifiers in #..,et .private !.public ".protected 4.internal $ protected internal
51. What are 8ccess Modifiers 'sed for?

Access )odifiers are used to control the accessibilty of types and members with in the types.
52. What is the pri+ate 8ccess modifier?

+rivate members are available only within the containing type.


53. What is the public Access modifierF

+ublic members are available any where ,there is no restriction


54. What is the protected Access modifierF

+rotected members are available, within the container type and to the types that derives from the containing type.
55. What is the interna" modifiers? 56. !an (o' 'se a"" access modifiers for a"" t(pes?

,o, ,ot all access modifiers can be used by all types or members in all contexts, and in some cases the accessibility of a type member is constrained by the accessibility of its containing type. !an deri+ed c"asses ha+e *reater accessibi"it( than their base t(pes? ,o, Derived classes cannot have greater accessibility than their base types. 3or example the following code is illegal. using 7ystem8 internal class (nternal0ase#lass 9 public void +rint=> 9 #onsole.Write?ine=C( am a 0ase #lass )ethodC>8 <
KannaBabu Page 16

SATHYA TECHNOLOGIES
< public class +ublicDerived#lass 6 (nternal0ase#lass 9 public static void )ain=> 9 #onsole.Write?ine=C( am a +ublic Derived #lass )ethodC>8 < < When you compile the above code an error will be generated stating C(nconsistent accessibility6 base class (nternal0ase#lass is less accessible than class +ublicDerived#lassC.To make this simple, you cannot have a public class 0 that derives from an internal class A. (f this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

Is the fo""owin* code "e*a"? using 7ystem8 private class Test 9 public static void )ain=> 9 < < ,o, a compile time error will be generated stating ;Namespace e"ements cannot be e3p"icit"( dec"ared as pri+ate5 protected5 or protected interna"; !an (o' dec"are str'ct members as protected? ,o, struct members cannot be declared protected. This is because structs do not support inheritance. !an the accessibi"it( of a t(pe member be *reater than the accessibi"it( of its containin* t(pe? ,o, the accessibility of a type member can never be greater than the accessibility of its containing type. 3or example, a public method declared in an internal class has only internal accessibility.

KannaBabu

Page 17

SATHYA TECHNOLOGIES
!an destr'ctors ha+e access modifiers? ,o, destructors cannot have access modifiers. What does protected interna" access modifier mean? The protected internal access means protected 45 internal, not protected A,D internal. (n simple terms, a protected internal member is accessible from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected. What is the defa'"t access modifier for a c"ass5str'ct and an interface dec"ared direct"( with a namespace? internal Wi"" the fo""owin* code compi"e? using 7ystem8 interface (*xample(nterface 9 public void 7ave=>8 < ,o, you cannot specify access modifer for an interface member. (nterface members are always public. !an (o' specif( an access modifier for an en'meration? *numeration members are always public, and no access modifiers can be specifi

DataType Casting What do (o' mean b( castin* a data t(pe? #onverting a variable of one data type to another data type is called casting. This is also called as data type conversion. What are the 2 0inds of data t(pe con+ersions in !-? Imp"icit con+ersions: ,o special syntax is required because the conversion is type safe and no data will be lost. *xamples include conversions from smaller to larger integral types, and conversions from derived classes to base classes. 23p"icit con+ersions: *xplicit conversions require a cast operator. The source and destination variables are compatible, but there is a risk of data loss because the type of the destination
KannaBabu Page 1!

SATHYA TECHNOLOGIES
variable is a smaller si'e than =or is a base class of> the source variable. What is the difference between an imp"icit con+ersion and an e3p"icit con+ersion? 1. *xplicit conversions require a cast operator where as an implicit converstion is done automatically. 2. *xplicit conversion can lead to data loss where as with implicit conversions there is no data loss. What t(pe of data t(pe con+ersion happens when the compi"er enco'nters the fo""owin* code? #hild#lass ## ; new #hild#lass=>8 +arent#lass +# ; new +arent#lass=>8 (mplicit #onversion. 3or reference types, an implicit conversion always exists from a class to any one of its direct or indirect base classes or interfaces. ,o special syntax is necessary because a derived class always contains all the members of a base class. Wi"" the fo""owin* code compi"e? double d ; EEEE. 8 int i ; d8 ,o, the above code will not compile. Double is a larger data type than integer. An implicit conversion is not done automatically bcos there is a data loss. /ence we have to use explicit conversion as shown below. double d ; EEEE. 8 int i ; =int>d8 AA#ast double to int. If (o' want to con+ert a base t(pe to a deri+ed t(pe5 what t(pe of con+ersion do (o' 'se? *xplicit conversion as shown below. AA#reate a new derived type. #ar # ; new #ar=>8 AA (mplicit conversion to base type is safe. @ehicle @ ; # 8 AA *xplicit conversion is required to cast back to derived type. The code below will compile but throw an exception at run time if the right2side ob&ect is not a #ar ob&ect. #ar #! ; =#ar> @8 What operators can be 'sed to cast from one reference t(pe to another witho't the ris0 of throwin* an e3ception?
KannaBabu Page 19

SATHYA TECHNOLOGIES
The is and as operators can be used to cast from one reference type to another without the risk of throwing an exception. If castin* fai"s what t(pe of e3ception is thrown? (nvalid#ast*xception

>a"'es t(pes = ?eference t(pes Data What are the 2 t(pes of data t(pes a+ai"ab"e in !-? 1. @alue Types 2. 5eference Types If (o' define a 'ser defined data t(pe b( 'sin* the str'ct 0e(word5 Is it a a +a"'e t(pe or reference t(pe? @alue Type If (o' define a 'ser defined data t(pe b( 'sin* the c"ass 0e(word5 Is it a a +a"'e t(pe or reference t(pe? 5eference type 8re >a"'e t(pes sea"ed? -es, @alue types are sealed. What is the base c"ass from which a"" +a"'e t(pes are deri+ed? 7ystem.@alueType

Delegates
What is a delegate? A delegate "# a $%&e #a'e 'un($")n &)"n$e*. +#"ng ,e-ega$e# %)u (an &a## .e$/),# a# &a*a.e$e*#. T) &a## a .e$/), a# a &a*a.e$e*0 $) a ,e-ega$e0 $/e #"gna$u*e )' $/e .e$/), .u#$ .a$(/ $/e #"gna$u*e )' $/e ,e-ega$e. T/"# "# 1/%0 ,e-ega$e# a*e (a--e, $%&e #a'e 'un($")n &)"n$e*#. What is the main use of delegates in C#? 2e-ega$e# a*e .a"n-% u#e, $) ,e'"ne (a-- ba(3 .e$/),#. What do you mean by chaining delegates? O* KannaBabu Page 2

SATHYA TECHNOLOGIES
What is a multicast delegate? T/e (a&ab"-"$% )' (a--"ng .u-$"&-e .e$/),# )n a #"ng-e e4en$ "# (a--e, a# (/a"n"ng ,e-ega$e#. Le$ .e g"4e %)u an e5a.&-e $) un,e*#$an, $/"# 'u*$/e*. 1. C*ea$e a ne1 a#&.ne$ 1eb a&&-"(a$")n 2. 2*ag an, ,*)& a bu$$)n ()n$*)- an, -ea4e $/e I2 a# Bu$$)n1. 3. On $/e (),e be/"n, '"-e0 a,, $/e (),e #/)1n be-)1.

When you click the 0utton now, both )ethod and )ethod! will be executed. 7o, this capability of calling multiple methods on a single event is called as chaining delegates. (n the example, we are using *vent/andler delegate, to hook up )ethod and )ethod! to the click event of the button control. 7ince, the *vent/andler delegate is now pointing to multiple methods, it is also called as multicast delegate. Wi"" the fo""owin* code compi"e?

No, the code does not compile. For the code to compile, the signature of Method1 should match the signature of SampleDelegate.

KannaBabu

Page 21

SATHYA TECHNOLOGIES

What is the difference between STRING and STRINGBUILDER ? 1. S$*"ng "# "..u$ab-e 1/e*e a# S$*"ngBu"-,e* "# .u$ab-e 2. S$*"ng "# un,e* SYSTE6 Na.e#&a(e 1/e*e a# S$*"ngBu"-,e* "# S%#$e..Te5$ Na.e#&a(e. 3. S$*"ngBu"-,e* )b7e($# a*e be$$e* &e*')*.an(e $/an #$*"ng )b7e($ 1/en /ea4% #$*"ng .an"&u-a$")n "# "n4)-4e,. 8/a$ "# $/e &a*$"a- (-a##9 T/e &a*$"a- (-a## a--)1# $) #&-"$ a (-a## "n$) 2 )* .)*e '"-e#. A-- $/e#e &a*$# a*e $/en ().b"ne, "n$)

KannaBabu

Page 22

You might also like