Basic .NET, C#.NET, ASP.NET interview questions
- What is an AppDomain? What is a process? What is thread? What is the difference between AppDomain and process?
Process: A computer program is a set of instructions. Operating system executes a computer program by allocating a process for a program. Several processes may be associated with the execution of a single program. A process is an instance of machine code associated with a program. It has memory for instructions, data, a call stack and a heap
AppDomain: An AppDomain is a light-weight process which separates one application in .NET with another. CLR creates an AppDomain when an application is loaded. Each application will have an AppDomain associated. Each AppDomain can have different threads running in it. Each app domain will have its associated code, data and configuration. Hence when one application crashes, it does not affect other.
Thread: Each process can have multiple threads. Multiple threads can share same execution code and resources. A multi-threaded process can perform several tasks concurrently.
- What is a runtime host?
.NET framework supports different type of applications like Web, windows, console etc,. Each type of application needs a runtime host to start it. This runtime host loads the runtime into a process, creates the application with in the process and loads the application code into the process.
Runtime hosts included in .NET framework are
ASP.NET: It loads the runtime that can handle a web request into the process. ASP.NET also creates an application domain for each Web application that will run on a Web server.
Microsoft Internet Explorer: It creates an application domain to run managed controls.
Shell executables: When ever a runtime executable is launched from the shell, this executable invokes the corresponding runtime host.
- What is the difference between manageable and unmanageable code?
Code which targets the .NET framework CLR is manageable meaning CLR can provide its services like type safety, memory management, exceptional handling etc to this type of code. Managed code is always compiled into MSIL. When a .NET application is run this compiled MSIL is compiled to native code using JIT (Just In Time compiler). This JIT generates the native code as per the hardware specification on the system. Since all this process happens under the control of a managed environment CLR, CLR provides all its rich functionality. Managed code provides platform independence since the code is converted to MSIL and then converted to native code depending on the system architecture.
The code that does not target CLR is unmanageable. It cannot run under CLR. This code directly runs under OS control. Applications written in traditional applications like C++, VB, C generate unmanaged code. This targets the computer architecture. Unmanaged code is always compiled to target a specific architecture and will only run on the intended platform. This means that if you want to run the same code on different architecture then you will have to recompile the code using that particular architecture. Unmanaged code is always compiled directly to the native code which is architecture specific. This code cannot be executed on other platforms that are different than the one on which the code was compiled. All the features provided by CLR are unavailable and are to be taken care by the code. Hence this causes memory leaks in traditional applications.
- ExplainValue and reference types?
“System.Object” is the base class from all the .NET classes.
Value types: Value types inherit from the
System.ValueType class, which in turn, inherits from
System.Object. Value types are stored on stack. They are implicitly sealed. Structs and Enumerations are value types and they are always stored on stack. A value type cannot contain a null value. Variables that are value types store data.
Reference types: Variables to reference types referred to as object, store reference to actual data. Actual data is stored on the heap and reference is stored on the stack. This allows the garbage collector to track outstanding references to a particular instance and free the instance when no references remain.
Floating Point types: float, double
Decimal types: decimal
- What is the role of garbage collector in .NET?
Objects created are stored on heap. Since the memory (here heap) is exhaustible, .NET identifies a mechanism to collect the unused memory(heap). GC does an automatic sweep of heap once it is full. GC can only destroy managed objects.
GC will finalize all the objects in the memory that are not being used anymore and thereby freeing the memory allocated to them.
.NET uses a three-generation approach to collecting memory. Newly allocated memory tends to be freed more frequently than older allocations, which tend to be more permanent. Gen 0 (Zero) is the youngest generation and, after a garbage collection, any survivors go on to Gen 1. Likewise, any survivors of a Gen 1 collection go on to Gen 2. Usually garbage collection will occur only on Gen 0, and only if after it has reached some limit. Until memory is exhausted, the cost of allocating each new object is that of incrementing a pointer--which is close to the performance of advancing the stack pointer.
CLR calls the GC when there is a high memory pressure and it is not able to find any exact place to allocate a new object or the applied threshold is reached.
- Can you force Garbage collection if so how?
In applications with significant memory requirements, you can force garbage collection by invoking the GC.Collect method from the program.
- What is CLR?
CLR is a runtime environment provided by .NET framework.
Developers write code in either C# or VB.NET.
.NET compilers convert this high level code into Microsoft Intermediate Language(MSIL). At runtime JIT compiler converts the MSIL into native code specific to the OS. CLR runs MSIL.
CLR provides memory management, exceptional handling, security etc to the .NET code.
- Explain CLS and CTS?
CLS: Common Language specification is a set of rules that are to be followed by a language in order to be .NET complaint. This facilitates cross-language integration. Programs written in one .NET language can interoperate with programs written in another .NET language.
CTS: Common Type System Common Type System (CTS) describes how types are declared, used and managed. CTS facilitates cross-language integration, type safety, and high performance code execution.
- What is Type safety in .NET?
Type-safe code accesses only the memory locations it is authorized to access. For example, type-safe code cannot read values from another object's private fields. It accesses types only in well-defined, allowable ways. If we want to work directly with memory addresses and can manipulate bytes at these addresses then we have to declare that code chunk as unsafe using the unsafe Keyword in C#. So that CLR will not do any extra verification on this code.
Actually during just-in-time (JIT) compilation, an optional verification process examines the metadata and Microsoft intermediate language (MSIL) of a method to be JIT-compiled into native machine code to verify that they are type safe. This process is skipped if the code has permission to bypass verification. For example, the runtime cannot prevent unmanaged code from calling into native (unmanaged) code and performing malicious operations. When code is type safe, the runtime's security enforcement mechanism ensures that it does not access native code unless it has permission to do so. All code that is not type safe must have been granted Security Permission with the passed enum member SkipVerification to run.
- What is an assembly?
An assembly is a basic building block for an application. It can be a DLL or EXE. An assembly contains IL. It consists of metadata about the types inside the assembly.