02.07.2013 Views

.NET Interview Questions 4 th Edition By Shivprasad ... - A2Z Dotnet

.NET Interview Questions 4 th Edition By Shivprasad ... - A2Z Dotnet

.NET Interview Questions 4 th Edition By Shivprasad ... - A2Z Dotnet

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The compiler inserts <strong>th</strong>e public key into <strong>th</strong>e assembly manifest and reserves space in <strong>th</strong>e PE file<br />

for <strong>th</strong>e full strong name signature. The real public key must be stored while <strong>th</strong>e assembly is built<br />

so <strong>th</strong>at o<strong>th</strong>er assemblies <strong>th</strong>at reference <strong>th</strong>is assembly can obtain <strong>th</strong>e key to store in <strong>th</strong>eir own<br />

assembly reference.<br />

• Because <strong>th</strong>e assembly does not have a valid strong name signature, <strong>th</strong>e verification of<br />

<strong>th</strong>at signature must be turned off. You can do <strong>th</strong>is by using <strong>th</strong>e –Vr option wi<strong>th</strong> <strong>th</strong>e<br />

Strong Name tool. The following example turns off verification for an assembly called<br />

myAssembly.dll.<br />

Sn –Vr myAssembly.dll<br />

• Just before shipping, you submit <strong>th</strong>e assembly to your organization signing au<strong>th</strong>ority<br />

for <strong>th</strong>e actual strong name signing using <strong>th</strong>e –R option wi<strong>th</strong> <strong>th</strong>e Strong Name tool. The<br />

following example signs an assembly called myAssembly.dll wi<strong>th</strong> a strong name using <strong>th</strong>e<br />

sgKey.snk key pair.<br />

Sn -R myAssembly.dll sgKey.snk<br />

(B) What is garbage collection?<br />

Garbage collection is a CLR feature, which automatically manages memory. Programmers forget<br />

to release <strong>th</strong>e objects while coding ... Laziness (Remember in VB6 where one of <strong>th</strong>e good<br />

practices is to set object to no<strong>th</strong>ing). CLR automatically releases objects when <strong>th</strong>ey are no longer<br />

in use and refernced. CLR runs on non-deterministic to see <strong>th</strong>e unused objects and cleans <strong>th</strong>em.<br />

One side effect of <strong>th</strong>is non-deterministic feature is <strong>th</strong>at we cannot assume an object is destroyed<br />

when it goes out of <strong>th</strong>e scope of a function. We should avoid using destructors because before GC<br />

destroys <strong>th</strong>e object it first executes destructor in <strong>th</strong>at case it will have to wait for code to release<br />

<strong>th</strong>e unmanaged resource. This results in additional delays in GC. So it is recommended to<br />

implement IDisposable interface, write cleanup code in Dispose me<strong>th</strong>od, and call<br />

GC.SuppressFinalize me<strong>th</strong>od. Its like instructing GC not to call your constructor. For more details<br />

read why is it preferred to not use finalize for clean up? in OOPS chapter..<br />

(I) Can we force garbage collector to run?<br />

System.GC.Collect () forces garbage collector to run. This is not recommended but can be used if<br />

situations arise.<br />

(B) What is reflection?<br />

All .<strong>NET</strong> assemblies have metadata information stored about <strong>th</strong>e types defined in modules. This<br />

metadata information can be accessed by mechanism called as “Reflection”. System. Reflection<br />

can be used to browse <strong>th</strong>rough <strong>th</strong>e metadata information.<br />

Using reflection, you can also dynamically invoke me<strong>th</strong>ods using System.Type.Invokemember.<br />

Below is sample source code if needed you can also get <strong>th</strong>is code from CD provided, go to<br />

“Source code” folder in “Reflection Sample” folder.<br />

Public Class Form1<br />

Private Sub Form1_Load (<strong>By</strong>Val sender As System. Object, <strong>By</strong>Val e as<br />

System.EventArgs) Handles MyBase.Load<br />

Dim Pobjtype As Type

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!