In .Net Framework code is sometimes referred to as managed code because the CLR manages its lifetime and execution.The .NET framework provides several core run-time services to the programs that run within it - for example exception handling and security.For these services to work, the code must provide a minimum level of information to the runtime. Such code is called managed code. All C# and Visual Basic.NET code is managed by default
Search Your Question
Tuesday, May 6, 2008
What is the main Differnce between Managed code and unmanaged code ?
Code that runs undercommon language runtime. Managed code must supply the metadata necessary for the runtime to provide services such as memory management, cross-language integration, code access security, and automatic lifetime control of objects. All code based on Microsoft intermediate language (MSIL) executes as managed code.
Code that is created without regard for the conventions and requirements of the common language runtime. Unmanaged code executes in the common language runtime environment with minimal services (for example, no garbage collection, limited debugging etc).
What is garbage collection? How it works in .net Framework?
Garbage collection is a system whereby a run-time component takes responsibility for managing the lifetime of objects and the heap memory that they occupy.
Garbage Collection, which runs in the background collecting unused object references, freeing us from having to ensure we always destroy them.
The garbage collector (GC) in .NET works without intervention of the developer ie a developer does not have to track memory usage and worry about when to free memory.
The Microsoft® .NET CLR (Common Language Runtime) requires that all resources be allocated from the managed heap. The managed heap-objects are automatically freed when they are no longer needed by the application.
GC in .Net is not deterministic that is, the .NET garbage collector works by periodically running through a list of all the objects that are currently being referenced by an application. All the objects that it doesn't find during this search are destroyed and the memory is freed up.
Is the lack of deterministic destruction in .NET a problem? What is the procedure when you have to do a deterministic
Although it cannot be categorise as a problem but It's an issue that affects component design. If in your program there are objects that maintain expensive or scarce resources (e.g. database locks), you need to provide some way to release the resource when it is done.
Microsoft recommend that you provide a method called Dispose() for this purpose. However, this causes problems for distributed objects - in a distributed system who calls the Dispose() method? Some form of reference-counting or ownership-management mechanism is needed to handle distributed objects - unfortunately the runtime offers no help with this.
To do deterministic garbage collection we can call System.GC.Collect();
How Garbage Collection is enforced in .NET? give an example?
we can call System.GC.Collect();
Detailed example can be found at : http://msdn2.microsoft.com/en-us/library/y46kxc5e.aspx
What is the difference between Finalize and Dispose (Garbage collection) ?
Resources like database connections, file and window handles (HWND) often are not managed by the runtime. Therefore, we should provide both an explicit and an implicit way to free these resources.
The garbage collector calls Finalize method at some point after there are no longer any valid references to the object.
However In some cases, if an external resource is scarce or expensive, better performance can be achieved if the programmer explicitly releases resources when they are no longer being used.
To provide explicit control, implement the Dispose() method provided by the IDisposable Interface. The consumer of the object should call this method when it is done using the object.
Dispose can be called even if other references to the object are alive.
Note that even when you provide explicit control by way of Dispose, you should provide implicit cleanup using the Finalize method.
Finalize prevent resources from permanently leaking if the programmer fails to call Dispose.
What is a Heap?
A portion of memory reserved for a program to use for the temporary storage of data structures whose existence or size cannot be determined until the program is running.
GC is necessarily slower than manual memory management. true or false?
FALSE. Modern garbage collectors appear to run as quickly as manual storage allocators On the other hand, the extra code required to make manual memory management work properly (for example, explicit reference counting) is often more expensive than a garbage collector would be.
Monday, May 5, 2008
Can you write a class without specifying namespace? Which namespace does it belong to by default?
Yes, you can, then the class belongs to global namespace which has no name. However For commercial products, its not a good idea.
How many types of attributes are there in .Net?
There are at least two types of .NET attribute.
The first type is a metadata attribute - it allows some data to be attached to a class or method. This data becomes part of the metadata for the class, and (like other class metadata) can be accessed via reflection.
The second type of attribute is a context attribute. Context attributes use a similar syntax to metadata attributes but they are fundamentally different. Context attributes provide an interception mechanism whereby instance activation and method calls can be pre- and/or post-processed.
What are the types of authentication in .net?
We have three types of authentication:
1. Form authentication
2. Windows authentication
3. Passport authentication
This has to be declared in web.config file.
Name any 2 of the 4 .NET authentification methods?
ASP.NET, in conjunction with Microsoft Internet Information Services (IIS), can authenticate user credentials such as names and passwords using any of the following authentication methods:
1) Windows: Basic, digest, or Integrated Windows Authentication (NTLM or Kerberos).
2) Microsoft Passport authentication
3) Forms authentication
4) Client Certificate authentication
Name any 2 of the 4 .NET authentification methods?
ASP.NET, in conjunction with Microsoft Internet Information Services (IIS), can authenticate user credentials such as names and passwords using any of the following authentication methods:
1) Windows: Basic, digest, or Integrated Windows Authentication (NTLM or Kerberos).
2) Microsoft Passport authentication
3) Forms authentication
4) Client Certificate authentication
Can source code be reverse-engineered from IL?
Yes, it is often relatively straightforward to regenerate high-level source from IL.
Saturday, April 26, 2008
How can we find out what the garbage collector is doing?
Lots of interesting statistics are exported from the .NET runtime via the '.NET CLR xxx' performance counters.
We can use Performance Monitor to view them.
How can we stop our code being reverse-engineered from IL?
We can buy an IL obfuscation tool. These tools work by 'optimising' the IL in such a way that reverse-engineering becomes much more difficult. Of course if we are writing web services then reverse-engineering is not a problem as clients do not have access to our IL.
How do you stop a running thread?
There are several options.
First, we can use our own communication mechanism to tell the ThreadStart method to finish. Alternatively the Thread class has in-built support for instructing the thread to stop.
The two principle methods are Thread.Interrupt() and Thread.Abort(). The former will cause a ThreadInterruptedException to be thrown on the thread when it next goes into a WaitJoinSleep state. In other words, Thread.Interrupt is a polite way of asking the thread to stop when it is no longer doing any useful work.
In contrast, Thread.Abort() throws a ThreadAbortException regardless of what the thread is doing. Furthermore, the ThreadAbortException cannot normally be caught (though the ThreadStart's finally method will be executed). Thread.Abort() is a heavy-handed mechanism which should not normally be required.
How does an AppDomain get created?
AppDomains are usually created by hosts. Examples of hosts are the Windows Shell, ASP.NET and IE. When you run a .NET application from the command-line, the host is the Shell. The Shell creates a new AppDomain for every application.