Vous êtes sur la page 1sur 6

Q1.

Difference between
(i) Is and As
Ans:

The is operator in C# is used to check the object type and it returns a bool value: true if the object is the
same type and false if not.

For null objects, it returns false.

Syntax:

bool isobject = (Object is Type);

The as operator does the same job of is operator but the difference is instead of bool, it returns
the object if they are compatible to that type, else it returns null.

Syntax:

Type obj = Object as Type;

Advantage of 'as' over 'is


In the case of is operator, to type cast, we need to do two steps:

1. Check the Type using is


2. If its true then Type cast

Actually this affects the performance since each and every time the CLR will walk the inheritance hierarchy,
checking each base type against the specified type. To avoid this, use as it will do it in one step. Only for
checking the type should we use the is operator.

Student s = obj as Student;

is equivalent to:

Student s = obj is Student ? (Student)obj : (Student)null;


(ii) Struct and class

S.N Struct Classes


Classes are reference types,
Structs are value types, allocated either on
1 allocated on the heap and
the stack or inline in containing types.
garbage-collected.
Allocations and de-allocations of value
Assignments of large reference
types are in general cheaper than
2 types are cheaper than
allocations and de-allocations of reference
assignments of large value types.
types.
In structs, each variable contains its own In classes, two variables can
copy of the data (except in the case of ref contain reference of the same
3 and out parameter variables), and any object and any operation on one
operation on one variable does not affect variable can affect another
another variable. variable.
(iii) Connected Architecture & Disconnected Architecture

Connected Disconnected
It is connection It is dis_connection
oriented. oriented.
Datareader DataSet
Connected methods gives Disconnected get low in
faster performance speed and performance.
connected can hold the data disconnected can hold
of single table multiple tables of data
connected you need to use a disconnected you cannot
read only forward only data
reader
Data Reader can't Data Set can persist the
persist the data data
It is Read only, we We can update data
can't update the data.

Q4. Discuss the following:


a) Boxing & unboxing

C# Type System contains three Types , they are Value Types , Reference Types and
Pointer Types. C# allows us to convert a Value Type to a Reference Type, and back
again to Value Types . The operation of Converting a Value Type to a Reference Type
is called Boxing and the reverse operation is called Unboxing.

Boxing
1: int Val = 1;
2: Object Obj = Val; //Boxing

The first line we created a Value Type Val and assigned a value to Val. The second
line , we created an instance of Object Obj and assign the value of Val to Obj. From
the above operation (Object Obj = i ) we saw converting a value of a Value Type into
a value of a corresponding Reference Type . These types of operation is called Boxing.

UnBoxing

1: int Val = 1;
2: Object Obj = Val; //Boxing
3: int i = (int)Obj; //Unboxing

The first two line shows how to Box a Value Type . The next line (int i = (int) Obj)
shows extracts the Value Type from the Object . That is converting a value of a
Reference Type into a value of a Value Type. This operation is called UnBoxing.

Boxing and UnBoxing are computationally expensive processes. When a value type is
boxed, an entirely new object must be allocated and constructed , also the cast
required for UnBoxing is also expensive computationally.

b) Serialization

Serialization is the process of converting an object into a stream of bytes in order


to store the object or transmit it to memory, a database, or a file. Its main purpose
is to save the state of an object in order to be able to recreate it when needed. The
reverse process is called deserialization.

Working of serialization: The object is serialized to a stream, which carries not just
the data, but information about the object's type, such as its version, culture, and
assembly name. From that stream, it can be stored in a database, a file, or memory.

Uses for Serialization

Serialization allows the developer to save the state of an object and recreate it as
needed, providing storage of objects as well as data exchange. Through
serialization, a developer can perform actions like sending the object to a remote
application by means of a Web Service, passing an object from one domain to
another, passing an object through a firewall as an XML string, or maintaining
security or user-specific information across applications.
c) MASTER PAGE

ASP.NET master pages allow you to create a consistent layout for the pages in your application.
A single master page defines the look and feel and standard behavior that you want for all of
the pages (or a group of pages) in your application. You can then create individual content
pages that contain the content you want to display. When users request the content pages,
they merge with the master page to produce output that combines the layout of the master
page with the content from the content page.

d) Constructor chaining

*********Do Not write the below link, just click on the link and write from internet*****

http://www.dotnetfunda.com/interviews/show/4054/w
hat-is-constructor-chaining

e) Validators

ASP.NET validation controls validate the user input data to ensure that useless, unauthenticated,
or contradictory data don't get stored.

ASP.NET provides the following validation controls:

RequiredFieldValidator

RangeValidator

CompareValidator

RegularExpressionValidator

CustomValidator

ValidationSummary

BaseValidator Class
The validation control classes are inherited from the BaseValidator class hence they inherit its
properties and methods. Therefore, it would help to take a look at the properties and the methods
of this base class, which are common for all the validation controls:

RequiredFieldValidator Control
The RequiredFieldValidator control ensures that the required field is not empty. It is generally tied
to a text box to force input into the text box.

RangeValidator Control
The RangeValidator control verifies that the input value falls within a predetermined range.

CompareValidator Control
The CompareValidator control compares a value in one control with a fixed value or a value in
another control.

RegularExpressionValidator

The RegularExpressionValidator allows validating the input text by matching against a pattern of
a regular expression. The regular expression is set in the ValidationExpression property.

f) Protected internal access specifier

The protected internal access specifier allows its members to be accessed in derived class,
containing class or classes within same application. However, this access specifier rarely used in
C# programming but it becomes important while implementing inheritance.

Vous aimerez peut-être aussi