In this article, I have explained basic .Net interview questions and answers will help to build your technical skill set and can help you in your interview.
- What is .NET Framework?
.NET Framework is a foundation on which developers can develop, run, and deploy the following applications:- Console applications
- Web applications (ASP.NET applications)
- Windows Forms applications
- Windows Presentation Foundation (WPF) applications
- Windows services
- Web services
- Service-oriented applications using Windows Communication Foundation (WCF)
- Workflow-enabled applications using Windows Workflow Foundation (WF)
.NET Framework also enables a developer to create sharable components to be used in distributed computing architecture. NET Framework supports the object-oriented programming model for multiple languages, such as VB, C#, and C++. .NET Framework supports multiple programming languages in a manner that allows language interoperability which implies that each language can use the code written in some other language. - What are the main components of .NET Framework?
These are the key components of .NET Framework:- .NET Framework Class Library
- Common Language Runtime
- Dynamic Language Runtimes (DLR)
- Application Domains
- Runtime Host
- Common Type System
- Metadata and Self-Describing Components
- Cross-Language Interoperability
- .NET Framework Security
- Profiling
- Side-by-Side Execution
- What is an IL?
Intermediate Language is also known as MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language). All .NET source code is compiled to IL. IL is then converted to machine code at the point where the software is installed, or at run-time by a Just-In-Time (JIT) compiler.
MSIL contains metadata that is the key to cross language interoperability. Since this metadata is standardized across all .NET languages, a program written in one language can understand the metadata written in a different language. MSIL includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations. - What is CLR (Common Language Runtime)?
CLR is the heart of .Net Framework. CLR provides an environment to execute .NET applications on target machines. CLR can be compared to the Java Virtual Machine (JVM) in Java.
CLR also provides various services to execute processes, such as memory management service and security services. CLR performs various tasks to manage the execution process of .NET applications. These are the responsibilities of CLR:- Automatic memory management
- Garbage Collection
- Code Access Security
- Code verification
- JIT compilation of .NET code
- What is CTS (Common Type System)?
CTS is the component of CLR through which .NET Framework provides support for multiple languages because it contains a type system that is common across all the languages. CTS defines how data types are declared, used and managed in the code at run time. Two CTS-compliant languages do not require type conversion when calling the code written in one language from within the code written in another language.
CTS provide a base set of data types for all the languages supported by .NET Framework. For Example, in VB we have “Integer” and in C# we have “int”, these datatypes are not compatible so the interfacing between them is very complicated. But these two different languages can communicate through Common Type System. “Integer” data type in VB6 and “int” data type in C# will convert to System.Int32, which is data type of CTS. The size is also same for both these data types. - What is CLS (Common Language Specification)?
CLS is a subset of CTS. CLS is set of basic rules/guidelines, which must be followed by each .NET language to be a .NET- compliant language. CLS enables an object or application to interact with the objects of applications of other languages. The classes that follow the rules specified by CLS are termed as CLS-compliant classes. - What is portable executable (PE)?
PE is the file format defining the structure that all executable files (EXE) and Dynamic Link Libraries (DLL) must use to allow them to be loaded and executed by Windows. PE is derived from the Microsoft Common Object File Format (COFF). The EXE and DLL files created using the .NET Framework obey the PE/COFF formats and also add additional header and data sections to the files that are only used by the CLR. - What is Ilasm.exe?
Ilasm.exe is a tool that generates PE files from MSIL code. You can run the resulting executable to determine whether the MSIL code performs as expected. - What is Managed Code?
Managed code is the code that is executed directly by the CLR instead of the operating system. The code compiler first compiles the managed code to intermediate language (IL) code, also called as MSIL code. This code doesn't depend on machine configurations and can be executed on different machines. In managed code, since the execution of the code is done by CLR, the runtime provides different services, such as garbage collection, type checking, exception handling, and security support. These services help provide uniformity in platform and language-independent behavior of managed code applications.
However, if you are using some third party software example VB6 or VC++ component they are unmanaged code, as .NET runtime (CLR) does not have control over the source code execution of these languages. - What is Assembly?
An assembly is a single deployable unit that contains all the information about the implementation of classes, structures and interfaces. The assembly stores all the information about itself. This information is called metadata and includes the name and version number of the assembly, security information, information about the dependencies and a list of the files that constitute the assembly. Assemblies include both executable application files that you can run directly from Windows without the need for any other programs (these have a .exe file extension), and libraries (which have a .dll extension) for use by other applications. - What are the different types of assembly?
There are two kinds of assemblies in .NET:- Private Assembly:
A private assembly is used by a single application, and is stored in the application's directory, or a sub-directory - Shared Assembly:
A shared assembly is stored in the global assembly cache (GAC), which is a repository of assemblies maintained by the .NET runtime. Shared assemblies are also called strong named assemblies and shared by multiple applications. Each shared assembly has a four part name including its face name, version, public key token and culture information. The public key token and version information makes it almost impossible for two different assemblies with the same name or for two similar assemblies with different version to mix with each other.
- Private Assembly:
- What is Manifest?
Assembly metadata is stored in Manifest. Manifest contains all the metadata needed to do the following things:- Version of assembly
- Security identity
- Scope of the assembly
- Resolve references to resources and classes
The assembly manifest can be stored in a PE file either an .exe or .dll with Microsoft intermediate language (MSIL) code or in a stand-alone PE file, that contains only assembly manifest information. - Where is version information stored of an assembly?
Version information is stored in assembly inside the manifest. - How to view a Assembly information?
Assembly information can be viewed by using Ildasm.exe. ILDASM is a tool that converts the whole "exe" or "dll" in to IL code. It takes a PE file containing the MSIL code as a parameter and creates a text file that contains managed code. - What is GAC?
GAC (Global Assembly Cache) stores assemblies specifically designated to be shared by several applications on the computer. GAC is used in the following situations:-- If application is needed to be shared among several application.
- If the assembly has some special security requirements like only specific person (administrators) can remove the assembly. If the assembly is private then a simple delete will remove the assembly.
- How to add and remove an assembly from GAC?
- To Install assembly in Cache use Gacutil. Go to "Visual Studio Command Prompt" and type "gacutil -i <assembly_name>", where (assembly_name) is the DLL name of the project.
- To Uninstall assembly, type gacutil –u <assembly_name> in Visual Studio Command Prompt.
- Is versioning applicable to private assemblies?
Private assembly lie in their individual folders so versioning concept is only applicable to global assembly cache (GAC) but you can still version it to have better version control on the project. - What is the concept of strong names?
While using shared assemblies, in order to avoid name collisions strong names are used. Strong Names helps GAC to differentiate between two versions. Strong Name is only needed when we need to deploy assembly in GAC. It is similar to GUID in COM components.
A strong name consists of a name that consists of an assembly's identity (text name, version number, and culture information), a public key and a digital signature generated over the assembly. The .NET Framework provides a tool called the Strong Name Tool (Sn.exe), which allows verification and key pair and signature generation. - What is Namespace?
Namespace is a logical grouping of related classes and types. Namespaces allow you to organize your classes so that they can be easily accessed in other applications. Namespaces can be used to avoid any naming conflicts between classes, which have the same names. For example, you can use two classes with the same name in an application provided they belong to different namespaces. - What is Difference between Namespace and Assembly?
Following are the differences between namespace and assembly:- Namespace is logical grouping of classes and types. Namespace can span multiple assemblies.
- Assembly is physical grouping of logical units.
- What is Delay signing?
While doing development, you will have to share strong name keys with developer. But it is not a good practice from security point of view. The solution of this problem is that you can assign the key later on and during development you can use delay signing.
To create a strong named assembly and to be used by someone else, we partially build this assembly by providing a Public Key. We write this Public Key in the AssemblyInfo.vb OR .cs file. We also add an attribute by the named <assembly:assemblydelaysignattribute(true)> to the assembly info file. This makes it sure that when we build the assembly, It would be containing the information only about the public key before we deliver it to our client. This is a partial strong named assembly that we have created, and hence it is called Delayed Assembly. - What is Garbage Collection?
Garbage collection prevents memory leaks during execution of programs. While coding, many programmers forget to release objects. Garbage collector is a low-priority process that manages the allocation and deallocation of memory for your application. It checks for the unreferenced variables and objects. If GC finds any object that is no longer used by the application, it frees up the memory from that object. It runs on non-deterministic as it is not possible to determine that when garbage collector will run. - Can we force garbage collector to run?
Yes, we can force garbage collector to run by System.GC.Collect() method. This is not recommended but can be used if really needed. - What is the difference between Dispose() and Finalize()?
CLR uses Dispose and Finalize methods to perform garbage collection of run-time objects of .NET applications.
Finalize method is called automatically by the runtime. CLR has a garbage collector (GC), which periodically checks for objects in heap that are no longer referenced by any object or program. It calls the Finalize method to free the memory used by such objects.
Dispose method is called by the programmer. Dispose is another method to release the memory used by an object. The Dispose method needs to be explicitly called in code to dereference an object from the heap. The Dispose method can be invoked only by the classes that implement the IDisposable interface. - Can we suppress the finalize process inside the garbage collector forcefully in .NET?
By using GC.SuppressFinalize() method we can suppress the finalize process inside the garbage collector forcefully in .NET. - How many types of generations are there in a garbage collector?
Generations are the division of objects on the managed heap used by the garbage collector. This mechanism allows the garbage collector to perform highly optimized garbage collection. There are the three types of generations in a garbage collector:- Generation 0: When an object is initialized, it is said to be in generation 0.
- Generation 1: The objects that are under garbage collection process are considered to be in generation 1.
- Generation 2: Whenever new objects are created and added to the memory, they are added to generation 0 and the old objects in generation 1 are considered to be in generation 2.
- What is Reflection?
Reflection is used in the process of obtaining type information at runtime. Reflection is generally used for the following tasks:- Viewing metadata: Allows viewing attribute information from the code at runtime.
- Performing type discovery: Allows examining the various types in an assembly and instantiate those types.
- Late binding to methods and properties: Allows the developer to call properties and methods on dynamically instantiated objects using type discovery.
- Reflection emit: Allows you to create new types at runtime and then to use those types to perform tasks.
- What are different types of JIT?
There are three types of JIT compilers:- Pre-JIT: Pre-JIT compiles complete source code into native code in a single compilation cycle. This is done at the time of deployment of the application.
- Econo-JIT: Econo-JIT compiles only those methods that are called at runtime. However, these compiled methods are removed when they are not required.
- Normal-JIT: Normal-JIT compiles only those methods that are called at runtime. These methods are compiled the first time they are called, and then they are stored in cache. When the same methods are called again, the compiled code from cache is used for execution.
- What are Value types and Reference Types?
Value types directly contain their data that are either allocated on the stack or allocated in-line in a structure. So value types are actual data.
Reference types store a reference of value's memory address, and are allocated on the heap.
Variables that are value types each have their own copy of the data, and therefore operations on one variable do not affect other variables. Variables that are reference types can refer to the same object; therefore, operations on one variable can affect the same object referred to by another variable. - What is concept of Boxing and Unboxing?
- Boxing: Converting a value type to reference type is called Boxing.
- Unboxing: Converting a reference type to value type is called Unboxing.
int i = 1; object obj = i; // boxing int j = (int) obj; // unboxing - What is the difference between System exceptions and Application exceptions?
Exceptions can be generated programmatically or can be generated by system. All exceptions are derived fromSystem.Exception class.- System Exception: System.SystemException acts as a base class for all the predefined system exceptions.
- Application Exception: System.ApplicationException acts as a base class for all user-defined exceptions.
- What is CODE Access Security?
CAS is part of .NET security model lets us grant or deny execution permissions to an assembly. It determines whether a piece of code is allowed to run and what resources it can use while running. For Example: CAS will allow an application to read but not to write and delete a file or a resource from a folder. - What is Satellite Assembly?
Satellite assembly is used when multilingual (UI) application is created. Satellite assembly is a compiled library that contains localized resources which provides us with the capability of designing and deploying solutions to multiple cultures, rather than hard coding texts, bitmaps etc. - How to prevent .NET DLL to be decompiled?
Any one can easily decompile your DLL back using tools like ILDASM or Reflector. It means any one can easily look in to your assemblies and reverse engineer them back in to actual source code and understand some real good logic, which can make it easy to crack your application.
But we can prevent .NET DLL to be decompiled by obfuscation. - What is Native Image Generator (Ngen.exe)?
Ngen.exe creates compiled processor-specific machine code called native images, which are files and installs them into the native image cache on the local computer. The runtime will use native images from the cache rather than using the JIT compiler to compile the original assembly. Running Ngen.exe on an assembly potentially allows the assembly to load and execute faster, because it restores code and data structures from the native image cache rather than generating them dynamically. - What is Code Document Object Model (CodeDom)?
Code Document Object Model are code generators which are used to minimize repetitive coding tasks, and to minimize the number of human-generated source code lines.
No comments:
Post a Comment