您的位置:首页 > 编程语言 > ASP

C# and ASP.NET Interview Question and Answers

2014-02-14 15:22 363 查看
1. What is the significance of Finalizemethod in .NET?

非托管资源(文件,数据库,COM)不能释放,需要手动调用object.Finalize()方法。

But unmanaged resources (example: Windows API created objects, File, Database connection objects, COM objects,
etc.) are outside the scope of .NET Framework. We have to explicitly clean our resources. For these types of objects, .NET Framework provides
Object.Finalize
method,
which can be overridden and clean up code for unmanaged resources can be put in this section?

2,Why is it preferred to not use finalizefor clean up?

The problem with
finalize
is
that garbage collection has to make two rounds in order to remove objects which have
finalize
methods.

使用Finalize方法导致GC进行进行两轮操作才把实现Finalize的对象释放掉。

object1, object2, object3(has overriden finalizemethod);

First round, GC remove object1, object2, push object3 into finalization queue

Second round, GC remove object3 from the finalization queues.

Therefore, as for Non.NET resources not use Finalize() rather use DISPOSE();

3.Finalize
vs Dispose

Finalizers are run by the Garbage Collection before an object that is eligible
for collection is reclaimed.(GC
controls,means you never knows when this will happen)

Dispose()
is
meant for cleaning up unmanaged resources, like network connections, files, handles to OS stuff, &c. It works best in conjunction
with the
using
block
where the compiler makes sure that
Dispose()
will
be called immediately once you are done with an object – and also ensures that you cannot work with the object anymore once it's disposed.(developer can control the moment)

The standard practice is to implement
IDisposable
and
Dispose
so
that you can use your object in a
using
statment.

Such as
using(var
foo = new MyObject()) { }
.

And in your finalizer, you
call
Dispose
,
just in case the calling code forgot to dispose of you.

4.What
is the use of DISPOSE method?

Dispose
method belongs
to ‘
IDisposable

interface.

Call the
Dispose
method
in
Finalize
method
and in
Dispose
method,
suppress the
finalize
method
using
GC.SuppressFinalize
.
Below is the sample code of the pattern. This is the best way we do clean our unallocated resources and yes not to forget we do not get the hit of running the Garbage collector twice.

public class CleanClass : IDisposable

{

public void Dispose()

{

using (var foo = new MyObject()); // clean unallocated resources by developers

        // Call GC.SupressFinalizeto take this object off the finalization
// queue and prevent finalization code for this object from
// executing a second time.


GC.SuppressFinalize(this);

}

protected override void Finalize()

{

Dispose();

}

}

5.What is an interface and what is an abstract class? Please expand
by examples of using both. Explain why.

Interface is not a class, all methods are abstract without implementation.

abstract class, besides this, it has its own concrete methods and fields.

6.How does output caching work in ASP.NET?

Output caching is a powerful technique that increases request/response
throughput by caching the content generated from dynamic pages. Output caching is enabled by default, but output from any given response is not cached unless
explicit action is taken to make the response cacheable.

Page Output Cache本身是Enable的,但是需要把ResonseOutput标记为cacheable.

To
make a response eligible for output caching, it must have a valid expiration/validation policy and public cache visibility. This can be done
using either the low-level
OutputCache
API
or the high-level
@ OutputCache
directive.

The
output cache also supports variations of cached
GET
or
POST
name/value
pairs.

<%@ OutputCacheDuration="60" VaryByParam="None" providerName="DiskCache" %>

7.What is connection pooling and how do you make your application use it?

ConnectionString的作用是访问Database,重用Connection Pool,提高性能。

Opening database connection is a time consuming operation.

Connection pooling increases the performance of the applications by reusing the active database connections instead of creating new connection for every request.

Connection pooling behaviour is controlled by the connection string parameters.

Following are the 4 parameters that control most of the connection pooling behaviour:

Connect Timeout
Max Pool Size
Min Pool Size
Pooling

8.What
are different methods of session maintenance in ASP.NET?

There are three types:

In-process storage (asp.net process itself)
Session State Service (external process)
Microsoft SQL Server

9.What does the "EnableViewState" property do? Why would I want it on or off?
不要写任何代码就可以访问SeverControls的信息,但是代价比较高。

Enable
ViewState
turns
on the automatic state management feature that enables server controls to re-populate their values on a round trip without requiring you to write any code. This feature is not free however,
since the state of a control is passed to and from the server in a hidden form field.

<input type="hidden" name="__VIEWSTATE">


10.What is the difference between Server.Transferand Response.Redirect?

Server.Transfer()
: client is shown as it is on the requesting page only, but all the content is of the requested page.
Data can be persisted across the pages using
Context.Item
collection, which is one of the best ways to transfer data
from one page to another keeping the page state alive.

Response.Dedirect()
: client know the physical location (page name and query string as well).
As you might suspect,
Server.Transfer
fixes
all of these problems. It does this by performing the transfer on the server without requiring a roundtrip to the client.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: