C# and ASP.NET Interview Question and Answers

1. What is the significance of Finalize method 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 providesObject.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 finalize for 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 finalize method);

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 thatDispose() 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 usingGC.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.SupressFinalize to 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.

<%@ OutputCache Duration="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:

  1. Connect Timeout
  2. Max Pool Size
  3. Min Pool Size
  4. Pooling

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

There are three types:

  1. In-process storage (asp.net process itself)
  2. Session State Service (external process)
  3. 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.Transfer and 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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值