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
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
that garbage collection has to make two rounds in order to remove objects which have
使用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)
meant for cleaning up unmanaged resources, like network connections, files, handles to OS stuff, &c. It works best in conjunction
with the
where the compiler makes sure that
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
that you can use your object in a
Such as
And in your finalizer, you
call
just in case the calling code forgot to dispose of you.
4.What
is the use of DISPOSE method?
to ‘
interface.
Call the
in
and in
suppress the
using
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
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
or the high-level
The
output cache also supports variations of cached
pairs.
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
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.
10.What is the difference between Server.Transferand Response.Redirect?
Data can be persisted across the pages using
from one page to another keeping the page state alive.
As you might suspect,
all of these problems. It does this by performing the transfer on the server without requiring a roundtrip to the client.
非托管资源(文件,数据库,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.Finalizemethod,
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
finalizeis
that garbage collection has to make two rounds in order to remove objects which have
finalizemethods.
使用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
usingblock
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
IDisposableand
Disposeso
that you can use your object in a
usingstatment.
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?
Disposemethod belongs
to ‘
IDisposable’
interface.
Call the
Disposemethod
in
Finalizemethod
and in
Disposemethod,
suppress the
finalizemethod
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
OutputCacheAPI
or the high-level
@ OutputCachedirective.
The
output cache also supports variations of cached
GETor
POSTname/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
ViewStateturns
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.Itemcollection, 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.Transferfixes
all of these problems. It does this by performing the transfer on the server without requiring a roundtrip to the client.
相关文章推荐
- C# and ASP.NET Interview Questions
- Scrolling News Web Control using ASP.Net and C#
- Asp Interview Questions and Answers
- 在ASP.NET访问Excel文件 (VB.NET and C#)
- Information About The Space of MFC and C#,ASP.NET
- MVC architecture in ASP.Net using C# and Microsoft Data Access Application block
- C# Interview Questions and Answers
- 在ASP.NET访问Excel文件(VBandC#)
- ASP.NET Razor - C# and VB Code Syntax
- Most Popular Questions and Answers on ASP.NET Whidbey(from asp.net forums,write by ASP.NET Team)
- [c#] ASP.NET Refactor and Unit Test
- Wrox Professional ASP.NET 4 in C# and VB
- Python Interview Question and Answers
- ASP.NET(c#) MesageBox JS Alert and Other Debug tools
- Asp.net(c#)二进制上传 and 实现多线程断点续传
- Asp.net(c#)二进制上传 and 实现多线程断点续传
- How to create simple and advanced pivot tables in C# and ASP.NET
- 在ASP.NET访问Excel文件 (VB.NET and C#)
- .NET Interview Questions And Answers
- C# and Asp.Net Questions 面试题