Tuesday, July 17, 2012

.NET Framework basics



.Net Framework

1. What is mean by .Net Framework?

The .NET framework is a collection of all the tools and utilities required to execute the .NET managed applications on a particular platform.

2. What is mean by CLR?

Common Language Runtime is the core component of .Net framework. It is similar to the Java Virtual Machine or JVM in Java, which handles the execution of code and provides useful services for the implementation of the program. It provides a number of services, including the following
  • management (loading and execution)
  • Application memory isolation
  • Verification of type safety
  • Conversion of IL to native code
  • Access to metadata (enhanced type information)
  • Managing memory for managed objects
  • Enforcement of code access security
  • Exception handling, including cross-language exceptions
  • Interoperation between managed code, COM objects, and pre-existing DLLs (unmanaged code and data)
  • Automation of object layout
  • Support for developer services (profiling, debugging, and so on)

3. What is difference between managed and unmanaged code?

The managed code is always executed by a managed runtime execution environment like CLR for .Net. Metadata information of the code will be exchanged with runtime, so runtime environment can guarantee what the code is going to do and provide the necessary security checks before executing any piece of code
Code that is directly executed by the Operating System is known as un-managed code. Example applications written in VB 6.0, C++, C, etc are unmanaged code that typically targets the processor architecture and is always dependent on the computer architecture. In unmanaged code the memory allocation, type safety, security, etc needs to be taken care of by the developer.

4. What is mean by MSIL?

MSIL or IL stands for Microsoft Intermediate Language; if you compile managed code, the compiler translates your source code into Microsoft intermediate language. MSIL is platform independent language which can be converted to native code while installing software or at runtime by using Just-in compiler.

5. What is mean by CTS?

Common type system defines how types are declared, used, and managed in the runtime, and is also an important part of the runtime's support for cross-language integration. CTS is responsible for defining types that can be used across the .Net Languages. CTS Provides the data types, values, object types. This helps developers to develop applications in different languages.
For example, an integer variable in C# is written as int, whereas in VB.Net it is written as integer. Therefore in .Net Framework you have single class called System.Int32 to interpret these variables.

6. What is mean by CLS?

Common Language Specification is the subset of CTS; it is specification that defines the set rules and guidelines that all supporting language should follow. It integrate in such a way that programs written in any language can interoperate with one another.

7. What is mean by JIT?

Just In Time(JIT) compilers which compile the IL code to native executable code (.exe or .dll) for the specific machine and OS. JIT are slightly different from traditional compilers as they compile the IL to native code only when desired e.g., when a function is called, IL of function's body is converted to native code; just in time of need. If same function is called next time, the CLR uses the same copy of native code without re-compiling. As JIT are aware of processor and OS exactly at runtime, they can optimize the code extremely efficiently resulting in very robust applications.

8. What are different types of JIT?

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 functions that are called at runtime. However, these compiled functions are removed when they are not required.
Normal-JIT - Normal-JIT compiles only those functions that are called at runtime and they are stored in cache. If same function is called next time, the CLR uses the same copy of compiled code without re-compiling.

9. What is mean by Assembly?

  • Assemblies are self-describing installation units, consisting of one or more files.
  • Assemblies are the deployment units of .Net applications. .Net application consists of one or more assemblies.
  • An assembly may also contain references to other assemblies and it include metadata that describes all the types that are defined in the assembly with information about it members-methods, properties, events and fields.
  • One assembly could be a single Dll or exe that includes metadata, or it can be made of different files e.g resource files, modules and an exe.
  • Assembly manifests is a part of the metadata, it describes the assembly with all the information that's needed to reference it and lists all its dependencies.

10. What are the features of Assembly?

  • Assemblies are self-describing, it includes metadata that describes the assembly. It does not required to register key as like COM component.
  • Version dependencies are recorded inside an assembly manifest. The version of the referenced assembly that will be used can be configured by the developer and the system administrator.
  • Two different version of same assembly can be used inside single process.

11. What are different type's assemblies?

Private assembly- Private assembly is used within your application and it is installed at the same time as the application itself. It will be located in the same directory as the application or subdirectories thereof.
Shared assembly- Shared assemblies are used by several application. Shared assembly must have version number and unique name and it is usually installed in GAC (Global assembly catch). It reduces the need for disk space and memory space.

12. What are parts of assembly manifests?

  • Identity - Name, version, culture and public key
  • A list of files - Files belonging to the assembly, it can have one or more files.
  • Lists of referenced assemblies - all assemblies referenced by the assembly are documented inside the manifest.
  • Set of permission requests- these are the permission needed to run the assembly.
  • Exported types - It describes the structures, class with properties, method and events. It is used to create instance of the class.

13. What is mean by Namespace?

Namespace Logically group classes, it avoids name clashes between classes.
Example : most of the general purpose .net base classes are in a namespace called System. The base class Array is in this namespace is accessed with full name System.Array.

14. What is difference between Assembly and Namespace?

  • Assembly is physical grouping of classes. Namespace logically groups classes.
  • Single assembly can have different namespaces
  • Sample namespace can be used in different assembly. E.g the assembly mscorlib and system contain the namespace System.Threading

15. What is the difference between an executable assembly and a class library?

An executable assembly exists as the .exe file while a class library exists as the .dll file. Executable assembly represent executable applications having some entry (e.g., Main() method in C#). A class library, on the other hand, contains components and libraries to be used inside various applications. A Class library cannot be executed and thus it does not have any entry point.

16. What is ILDASM?

ILDASM(Intermediate Language DisAssembler ), this is inbuild tool to view content and manifest of the assembly. We can run the ILDASM by running following exe "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\ildasm.exe"

Note : Currently I am using V4, file path will vary for different version of SDK installation.

17. What is mean by Manifest?

Manifest is used to describe the information about the assembly, it contains following information.
  • Assembly name - Aids in versioning and visibility scope.
  • Version information - The version number is integrated into the assembly's identity.
  • Types - Boundaries and scopes of methods, classes, properties, events and attributes.
  • Locale - Information describing language/culture.
  • Reference - provides information for type references in an assembly and other referenced assemblies.
  • Cryptographic Hash - Public key encoded hash acting as version/security check.
  • Security Permissions - The permissions within the assembly determine the permissions that can be granted for all aspects of the assembly contents.

18. How will you created shared assembly?

Shared assembly can be created by signing the assembly. Sets to created shared assembly
  • Create new class library project using visual studio
  • Navigate to the property page of the class library
  • Select "Signing" tab
  • Select "Sign the assembly" check-box
  • Now select < New >... from "Choose a strong name key file" dropdown
  • Enter new Signing key file name and click Ok
  • Next the build the project. Now the shared assembly is ready to use in different project.

19. What is the use of Shared Assembly?

If you want to use the same assembly in different projects, we can create a shared assembly and placed inside the GAC(Global assembly Catch). So that assembly is access by all the application. Private assembly also be used in different projects, but we need to copy the private assembly files to different application folder. But if we are using Shared assembly, the assembly file remains in single location.
Shared assembly is highly secured, only administrator can uninstall the shared assembly.

20. What is GAC?

GAC(Global assembly catch) is used to store .Net assembly. It is located in "C:\Windows\assembly"
  • Assembly located in GAC is shared by multiple applications
  • Adding an Assembly to GAC
  • "gacutil -i (assembly_name)", where (assembly_name) is the DLL name of the project.

21. What is mean by Delay signing?

During development process, usually private key will not be exposed to the developer due to security reason. In this kind of scenario, we will go with delay signing.Delay signing allows you to place a shared assembly in the GAC by signing the assembly with just the public key. This allows the assembly to be signed with the private key at a later stage, when the development process is complete and the component or assembly is ready to be deployed. This process enables developers to work with shared assemblies as if they were strongly named, and it secures the private key of the signature from being accessed at different stages of development.
E.g
VB.Net(Assemblyinfo.vb)
< Assembly: AssemblyKeyFileAttribute("myKey.snk") > 

 < Assembly: AssemblyDelaySignAttribute(True) >
C#(Assemblyinfo.cs)
[assembly: AssemblyKeyFileAttribute("myKey.snk")]
[assembly: AssemblyDelaySignAttribute(true)]


22. What is mean by Satellite assembly?

When we write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.

23. What is portable executable (PE)?

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.

24. What is mean by Garbage collection?

Garbage collection is a CLR features used to automatically manages memory. CLR automatically release the objects which are not longer used or referenced. Developer who forget to release the dispose the objects will be cleared by GC. But it is not known when GC will be called by CLR to clean the memory. So better we can dispose the objects once it is used.

25. What are the different levels of GC is available?

Generation 0 , Generation 1, Generation 2

26. How Garbage collector will get memory from OS?

When execution engine starts, GC will initialize segment of memory for its operation. GC reserves memory in segment, each segment is 16MB. When we run out of segments we reserve a new segment. If a segment of memory is not in use, it will be deleted.

27. What is mean by LOH?

LOH-(Large Object Heap). If size of the object are very high(>64KB) then it will be stored in different segment of memory called as LOH. GC will treat the large objects differently from small objects.

28. What are situations GC will be called?

  1. If user forcefully calls System.GC.Collect
  2. System is in low memory situation
  3. Memory allocation exceeds the Generation0 threshold

29. What is mean by value type and Reference type?

Value type- Value type stores their value directly to memory address. Value type's values are allocated on the stack memory.
Reference type - Reference type stores the reference to the value's memory address. Reference type values are allocated on head memory.

30. What is mean by Boxing and Unboxing?

Boxing - Converting value type variable to reference type is called as boxing
UnBoxing - Converting reference type variable to value type is called as unboxing
            int vType = 35;
            object rType;
            //Boxing process
            rType = vType;
            //Unboxing process
            vType =(int) rType;

31. How will you decide when to use value type and reference type?

All depends upon need.

32. What is difference between System exception and Application exception?

All exceptions are derived from the Exception base class. Where Exception class is derived from the Object class. Both System and Application exception are derived from exception class but it has difference between them. System exceptions are thrown by the CLR where as Application exceptions are thrown by Application.
System Exception Application Exception
System exceptions are thrown by CLR Application exceptions are thrown by Application
E.g OutOfMemoryException, NullReferenceException,etc E.g User defined exception are created to throw application's exception and user defined exceptions are derived from ApplicationException

33. What is Reflection?

.Net compilers store metadata information(types defined) about the assemblies inside the assembly itself. Using this metadata we can load an assembly dynamically (at runtime), get information about its containing types, instantiate these types and call methods.
"Reflection" is a mechanism using which we can load an assembly dynamically and call its method. The System.Reflection is the root namespace that contains classes to implement the reflection. The Assembly class is the one which is used to represent the assembly in .Net environment.
Example:
static void Main(string[] args)
        {
            // Load an assembly from file
            Assembly myAssembly = Assembly.LoadFrom("MyService.dll");
            // Get the types contained in the assembly and print their names
            Type[] types = myAssembly.GetTypes();
            foreach (Type type in types)
            {
                Console.WriteLine(type.FullName);
                //Get the members(methods) present inside each type
                foreach (MemberInfo member in type.GetMembers())
                {
                    Console.WriteLine("   "+member.Name);
                }
            }
            Console.ReadLine();
        }
OutPut:

No comments: