0% found this document useful (0 votes)
35 views8 pages

C#

CSharp material for interview
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views8 pages

C#

CSharp material for interview
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

C# and VB.

NET

Explain the differences between Server-side and Client-side code? Server side code executes on the server.For this to occur page has to be submitted or posted back.Events fired by the controls are executed on the server.Client side code executes in the browser of the client without submitting the page. e.g. In AS .!E" for webcontrols like asp#button the click event of the button is executed on the server hence the event handler for the same in a part of the code$behind %server$side code&. Along the server$side code events one can also attach client side events which are executed in the clients browser i.e. 'avascript events. How does VB.NET C# achieve pol!"orphis"? olymorphism is also achieved through interfaces. (ike abstract classes) interfaces also describe the methods that a class needs to implement. "he difference between abstract classes and interfaces is that abstract classes always act as a base class of the related classes in the class hierarchy. For example) consider a hierarchy$car and truck classes derived from four$wheeler class* the classes two$wheeler and four$wheeler derived from an abstract class vehicle. So) the class +vehicle+ is the base class in the class hierarchy. ,n the other hand dissimilar classes can implement one interface. For example) there is an interface that compares two ob'ects. "his interface can be implemented by the classes like box) person and string) which are unrelated to each other. C- allows multiple interface inheritance. It means that a class can implement more than one interface. "he methods declared in an interface are implicitly abstract. If a class implements an interface) it becomes mandatory for the class to override all the methods declared in the interface) otherwise the derived class would become abstract. Can you explain what inheritance is and an example of when you might use it. "he savingaccount class has two data members$accno that stores account number) and trans that keeps track of the number of transactions. /e can create an ob'ect of savingaccount class as shown below. savingaccount s 0 new savingaccount % 1Amar1) 2344.44f & * From the constructor of savingaccount class we have called the two$argument constructor of the account class using the base keyword and passed the name and balance to this constructor using which the data member+s name and balance are initialised. /e can write our own definition of a method that already exists in a base class. "his is called method overriding. /e have overridden the deposit% & and withdraw% & methods in the savingaccount class so that we can make sure that each account maintains a minimum balance of 5s. 244 and the total number of transactions do not exceed 64. From these methods we have called the base class+s methods to update the balance using the base keyword. /e have also overridden the display% & method to display additional information) i.e. account number. /orking of currentaccount class is more or less similar to that of savingaccount class. 7sing the derived class+s ob'ect) if we call a method that is not overridden in the derived class) the base class method gets executed. 7sing derived class+s ob'ect we can call base class+s methods) but the reverse is not allowed.

7nlike C88) C- does not support multiple inheritance. So) in C- every class has exactly one base class. !ow) suppose we declare reference to the base class and store in it the address of instance of derived class as shown below. account a6 0 new savingaccount % 1Amar1) 2344.44f & * account a9 0 new currentaccount % 1:yCompany vt. (td.1) 693444.44f& * Such a situation arises when we have to decide at run$time a method of which class in a class hierarchy should get called. 7sing a6 and a9) suppose we call the method display% &) ideally the method of derived class should get called. ;ut it is the method of base class that gets called. "his is because the compiler considers the type of reference %account in this case& and resolves the method call. So) to call the proper method we must make a small change in our program. /e must use the virtual keyword while defining the methods in base class as shown below. public virtual void display% & < = /e must declare the methods as virtual if they are going to be overridden in derived class. "o override a virtual method in derived classes we must use the override keyword as given below. public override void display% & < = !ow it is ensured that when we call the methods using upcasted reference) it is the derived class+s method that would get called. Actually) when we declare a virtual method) while calling it) the compiler considers the contents of the reference rather than its type. If we don+t want to override base class+s virtual method) we can declare it with new modifier in derived class. "he new modifier indicates that the method is new to this class and is not an override of a base class method. How wo#ld !o# i"ple"ent inheritance #sin$ VB.NET C#? /hen we set out to implement a class using inheritance) we must first start with an existing class from which we will derive our new subclass. "his existing class) or base class) may be part of the .!E" system class library framework) it may be part of some other application or .!E" assembly) or we may create it as part of our existing application. ,nce we have a base class) we can then implement one or more subclasses based on that base class. Each of our subclasses will automatically have all of the methods) properties) and events of that base class . including the implementation behind each method) property) and event. ,ur subclass can add new methods) properties) and events of its own $ extending the original interface with new functionality. Additionally) a subclass can replace the methods and properties of the base class with its own new implementation $ effectively overriding the original behavior and replacing it with new behaviors. Essentially inheritance is a way of merging functionality from an existing class into our new subclass. Inheritance also defines rules for how these methods) properties) and events can be merged. In >;.!E" we can use implements keyword for inheritance) while in C- we can use the sign % # & between subclass and baseclass. How is a propert! desi$nated as read-onl!?

In >;.!E"# rivate m roperty!ame as ?ata"ype ublic 5ead,nly roperty roperty!ame%& As ?ata"ype @et 5eturn m roperty!ame

End @et End roperty In Crivate ?ata"ype m roperty!ame* public returntype roperty!ame < get< AAproperty implementation goes here return m roperty!ame* = AA ?o not write the set implementation =

%hat is hidin$ in CSharp ? Biding is also called as Shadowing. "his is the concept of ,verriding the methods. It is a concept used in the ,b'ect ,riented rogramming. E.g. public class ClassA < public virtual void :ethodA%& < "race./rite(ine%1ClassA :ethod1&* = = public class Class; # ClassA < public new void :ethodA%& < "race./rite(ine%1SubClass Class; :ethod1&* = = public class "op(evel < static void :ain%stringCD args& < "ext/riter tw 0 Console.,ut* "race.(isteners.Add%new "ext/riter"race(istener%tw&&* ClassA ob' 0 new Class;%&* ob'.:ethodA%&* AA ,utputs EClass A :ethod1 Class; ob'6 0 new Class;%&* ob'.:ethodA%&* AA ,utputs ESubClass Class; :ethodF = = %hat is the difference between an &'( )*ra$"ent) and an &'( )+oc#"ent.) An G:( fragment is an G:( document with no single top$level root element. "o put it simple it is a part %fragment& of a well$formed xml document. %node& /here as a well$formed xml document must have only one root element. %hat does it "eant to sa! ,the canonical- for" of &'(?

1"he purpose of Canonical G:( is to define a standard format for an G:( document. Canonical G:( is a very strict G:( syntax) which lets documents in canonical G:( be compared directly. 7sing this strict syntax makes it easier to see whether two G:( documents are the same. For example) a section of text in one document might read ;lack H /hite) whereas the same section of text might read ;lack H /hite in another document) and even in another. If you compare those three documents byte by byte) they+ll be different. ;ut if you write them all in canonical G:() which specifies every aspect of the syntax you can use) these three documents would all have the same version of this text %which would be ;lack H /hite& and could be compared without problem. "his Comparison is especially critical when xml documents are digitally signed. "he digital signal may be interpreted in different way and the document may be re'ected.

%h! is the &'( .nfoSet specification different fro" the &"l +/'? %hat does the .nfoSet atte"pt to solve? 1"he G:( Information Set %Infoset& defines a data model for G:(. "he Infoset describes the abstract representation of an G:( ?ocument. Infoset is the generaliIed representation of the G:( ?ocument) which is primarily meant to act as a set of definitions used by G:( technologies to formally describe what parts of an G:( document they operate upon. "he ?ocument ,b'ect :odel %?,:& is one technology for representing an G:( ?ocument in memory and to programmatically read) modify and manipulate a xml document. Infoset helps defining generaliIed standards on how to use G:( that is not dependent or tied to a particular G:( specification or A I. "he Infoset tells us what part of G:( ?ocument should be considered as significant information. Contrast +T+s vers#s &S+s. %hat are their si"ilarities and differences? %hich is preferred and wh!? ?ocument "ype ?efinition %?"?& describes a model or set of rules for an G:( document. G:( Schema ?efinition %GS?& also describes the structure of an G:( document but GS?s are much more powerful. "he disadvantage with the ?ocument "ype ?efinition is it doesnJt support data types beyond the basic 64 primitive types. It cannot properly define the type of data contained by the tag. An Gml Schema provides an ,b'ect ,riented approach to defining the format of an xml document. "he Gml schema support most basic programming types like integer) byte) string) float etc.) /e can also define complex types of our own which can be used to define a xml document. Gml Schemas are always preferred over ?"?s as a document can be more precisely defined using the G:( Schemas because of its rich support for data representation. Spea0in$ of Boolean data t!pes1 what2s different between C# and C C33? "here+s no conversion between 4 and false) as well as any other number and true) like in CAC88. How do !o# convert a strin$ into an inte$er in .NET?

IntK9. arse%string& Can !o# declare a C33 t!pe destr#ctor in C# li0e 4'!Class56? Les) but what+s the point) since it will call FinaliIe%&) and FinaliIe%& has no guarantees when the memory will be cleaned up) plus) it introduces additional load on the garbage collector.

%hat2s different abo#t na"espace declaration when co"parin$ that to pac0a$e declaration in 7ava? !o semicolon. %hat2s the difference between const and readonl!? "he readonly keyword is different from the const keyword. A const field can only be initialiIed at the declaration of the field. A readonly field can be initialiIed either at the declaration or in a constructor. "herefore) readonly fields can have different values depending on the constructor used. Also) while a const field is a compile$time constant) the readonly field can be used for runtime constants as in the following example# public static readonly uint l6 0 %uint& ?ate"ime.!ow."icks* %hat does 8a character do? ,n most systems) produces a rather annoying beep. Can !o# create en#"erated data t!pes in C#? Les. %hat2s different abo#t switch state"ents in C#?

!o fall$throughs allowed. %hat happens when !o# enco#nter a contin#e state"ent inside the for loop? "he code for the rest of the loop is ignored) the control is transferred back to the beginning of the loop. How can !o# sort the ele"ents of the arra! in descendin$ order? ;y calling Sort%& and then 5everse%& methods. %ill finall! bloc0 $et exec#ted if the exception had not occ#rred? Les. %hat2s the C# e9#ivalent of C33 catch 5:61 which was a catch-all state"ent for an! possible exception? A catch block that catches the exception of type System.Exception. Lou can also omit the parameter data type in this case and 'ust write catch <=. Can "#ltiple catch bloc0s be exec#ted?

!o) once the proper catch code fires off) the control is transferred to the finally block %if there are any&) and then whatever follows the finally block. %h! is it a bad idea to throw !o#r own exceptions?

/ell) if at that point you know that an error has occurred) then why not write the proper code to handle that error instead of passing a new Exception ob'ect to the catch block. "hrowing your own exceptions signifies some design flaws in the pro'ect. %hat2s the difference between co""ents1 ; ; co""ents and co""ents?

Single$line) multi$line and G:( documentation comments. How do !o# $enerate doc#"entation fro" the C# file co""ented properl! with a co""and-line co"piler? Compile it with a Adoc switch. Can !o# chan$e the val#e of a variable while deb#$$in$ a C# application? Les) if you are debugging via >isual Studio.!E") 'ust go to Immediate window. %hat2s the i"plicit na"e of the para"eter that $ets passed into the class2 set "ethod? >alue) and it+s datatype depends on whatever variable we+re changing. How do !o# inherit fro" a class in C#? lace a colon and then the name of the base class. !otice that it+s double colon in C88. +oes C# s#pport "#ltiple inheritance? !o) use interfaces instead. So how do !o# retrieve the c#sto"i<ed properties of a .NET application fro" &'( .confi$ file? Can !o# a#to"ate this process? InitialiIe an instance of AppSettings5eader class. Call the @et>alue method of AppSettings5eader class) passing in the name of the property and the type expected. Assign the result to the appropriate variable. In >isual Studio yes) use ?ynamic roperties for automatic .config creation) storage and retrieval. %h! is it not a $ood idea to insert code into .nitiali<eCo"ponent "ethod when wor0in$ with Vis#al St#dio? "he designer will likely through it away) most of the code inside InitialiIeComponent is auto$ generated. %here do !o# add an event handler? It+s the Attributesproperty) the Add function inside that property. e.g. btnSubmit.Attributes.Add%11on:ouse,ver11)11someClientCode%&*11& %hat are =a$$ed arra!? First lets us answer the Muestion that what an array is. "he dictionary meaning of array is an orderly arrangement or seMuential arrangement of

elements. In computer science term# An array is a data structure that contains a number of variables) which are accessed through computed indices. "he variables contained in an array) also called the elements of the array) are all of the same type) and this type is called the element type of the array. An array has a rank that determines the number of indices associated with each array element. "he rank of an array is also referred to as the dimensions of the array. An array with a rank of one is called a single$dimensional array. An array with a rank greater than one is called a multi$ dimensional array. Specific siIed multidimensional arrays are often referred to as two$ dimensional arrays) three$dimensional arrays) and so on. Now let us answer What are jagged arrays? A 'agged array is an array whose elements are arrays. "he elements of 'agged array can be of different dimensions and siIes. A 'agged array is sometimes called as Earray$of$arraysF. It is called 'agged because each of its rows is of different siIe so the final or graphical representation is not a sMuare. /hen you create a 'agged array you declare the number of rows in your array. Each row will hold an array that will be on any length. ;efore filling the values in the inner arrays you must declare them. Nagged array declaration in C-# For e.g. # int CD CD myNaggedArray 0 new int CKDCD* ?eclaration of inner arrays# myNaggedArrayC4D 0 new intC2D * AA First inner array will be of length 2. myNaggedArrayC6D 0 new intCOD * AA Second inner array will be of length O. myNaggedArrayC9D 0 new intCKD * AA "hird inner array will be of length K. !ow to access third element of second row we write# int value 0 myNaggedArrayC6DC9D* !ote that while declaring the array the second dimension is not supplied because this you will declare later on in the code. Nagged array are created out of single dimensional arrays so be careful while using them. ?onJt confuse it with multi$dimensional arrays because unlike them 'agged arrays are not rectangular arrays. For more information on arrays# http#AAmsdn.microsoft.comAlibraryAdefault.asp.url0AlibraryAen$usAcsrefAhtmlAvclrfarrayspg.asp %hat is a dele$ate1 wh! sho#ld !o# #se it and how do !o# call it ? A delegate is a reference type that refers to a Shared method of a type or to an instance method of an ob'ect. ?elegate is like a function pointer in C and C88. ointers are used to store the address of a thing. ?elegate lets some other code call your function without needing to know where your function is actually located. All events in .!E" actually use delegates in the background to wire up events. Events are really 'ust a modified form of a delegate. It should give you an idea of some different areas in which delegates may be appropriate#

"hey enable callback functionality in multi$tier applications as demonstrated in the examples above. Po#pQPAo#pQ "he CacheItem5emoveCallback delegate can be used in AS .!E" to keep cached information up to date. /hen the cached information is removed for any reason) the associated callback is exercised and could contain a reload of the cached information. Po#pQPAo#pQ 7se delegates to facilitate asynchronous processing for methods that do not offer asynchronous behavior. Events use delegates so clients can give the application events to call when the event is fired. Exposing custom events within your applications reMuires the use of delegates.

How does the &"lSeriali<er wor0? GmlSerialiIer in the .!E" Framework is a great tool to convert Gml into runtime ob'ects and vice versa .f !o# define inte$er variable and a ob=ect variable and a str#ct#re then how those will be plotted in "e"or!. Integer ) structure R System.>alue"ype $$ Allocated memory on stack ) infact integer is primitive type recogniIed and allocated memory by compiler itself . Infact ) System.IntK9 definition is as follows # CC-D CSerialiIableD public struct IntK9 # IComparable) IFormattable) IConvertible So ) itJs a struct by definition ) which is the same case with various other value types . ,b'ect R ;ase class ) that is by default reference type ) so at runtime NI" compiler allocates memory on the EBeapF ?ata structure . 5eference types are defined as class ) derived directly or indirectly by System.5eference"ype

You might also like