Sunday, April 29, 2012

.NET BASICS


Common Language Runtime (CLR) :-
It is the runtime that converts a MSIL code into the host machine language code, which is then executed appropriately.
The CLR is the execution engine for .NET Framework applications. It provides a number of services, including:
•  Code management (loading and execution)
•  Application memory isolation
•  Thread management
•  Verification of type safety
•  Conversion of IL to native code.
•  Access to metadata (enhanced type information)
•  Managing memory for managed objects (memory management)
•  Enforcement of code access security
•  Exception handling, including cross-language exceptions
•  Interoperation between managed code, COM objects, and pre-existing DLL's (unmanaged code and data) 

Microsoft Intermediate Language (MSIL) :-
MSIL is the Microsoft Intermediate Language. All .NET compatible languages will get converted to MSIL. MSIL also allows the .NET Framework to JIT compile the assembly on the installed computer.

Common Type System (CTS) :-
.Net allows developers to write program logic in at least 25 languages. The classes written in one language can be used by other languages in .Net. This service of .Net is possible through CTS which ensure the rules related to data types that all language must follow. It provides set of types that are used by all .NET languages and ensures .NET language type compatibility.

Common Language Specification (CLS) :-
It is nothing but a guideline that language has to follow so that it can communicate with other .Net language in seamless manner.

Managed Code :-
Managed code is code that is written to target the services of the Common Language Runtime. In order to target these services, the code must provide a minimum level of information (metadata) to the runtime. All C#, Visual Basic .NET, and JScript .NET code is managed by default. Visual Studio .NET C++ code is not managed by default, but the compiler can produce managed code by specifying a command-line switch (/CLR).

Namespace and assembly :-
Namespace => a Collection of names wherein each name is Unique. They form the logical boundary for a Group of classes. Namespace – must be specified in Project-Properties.
Assembly =>  a software Output Unit;It’s a unit of Deployment & a unit of versioning. Assemblies contain MSIL code. Assemblies are Self-Describing. [metadata,manifest]. An assembly is the primary building block of a .NET Framework application. It is a collection of functionality that is built, versioned, and deployed as a single implementation unit (as one or more files).
In other word, Assembly is physical grouping of logical units and Namespace is logically groups classes. Namespace can span multiple assembly.

.Net Architecture :-
The order starting from the bottom
1. CLR (Common Language Runtime)
2. .Net framework base classe
3. ASP.Net Web Form / Windows Form

Manifest :-
Assembly metadata is stored in manifest. It contains,
Version of assembly
Security identity
Scope of assembly etc.

Difference between Metadata and Manifest :-
Manifest describes the assembly itself. Assembly name, version number, culture information. strong name, list of all files, type reference and reference assembly. While the Metadata describes the contents within the assembly. Like classes, interfaces, namespaces, base class, scope, properties and their parameters etc.

Public vs private assemblies:-
Public assembly are the dll/exe file that can be used in different application. The main advantage of public assemblies is code reusability. These can be used in different machine on different computers. These are also called as shared assemblies. Private assembly is the assembelyinfo.cs or assembelyinfo.vb file within an application. An application must have one private assembly, outside this application there is no scope of private assembly.

Global Assembly Cache (GAC) :-
It’s a space (directory C:\winnt\assembely) on the server where all the shared assemblies are registered and that can be used in the application for code reuse.
Path : By default C:\\assembely e.g c:\winnt\assembely or c:\windows\assembely

Machine.Config :-
It’s a base configuration file for all .NET assemblies running on the server. It specifies settings that are global to a particular machine.

Strong Name :-
Strong name ensures the uniqueness of assembly on the server. It needed when we need to deploy assembly in GAC. Strong Name helps GAC to differentiate between two versions. A strong name includes information about Assembly version, Public/Private Key token, Culture information and Assembly name.
A strong name means generating public key in order to provide unique name to the assembly.

Different types a variable can be defined and their scopes :-
Public - Can be accessed anywhere
Private - anywhere in the same class
Protected - within the class and the class that inherits this class
Friend - Members of the class within the assembly
Protected friend - member of assembly or inheriting class

Reflection :-
All .Net assemblies have metadata information stored about the types defined in modules. This metadata information can be accessed by mechanism called as Reflection.

DLL HELL:-
Previously (when using VB) we can have a situation that we have to put same name dll file in a single directory, but the dlls are of different versions. This is known as dll hell.

COM, COM+ and DCOM :-
COM (Component Object Model) A standard that is used to for communication between OS and the software. COM is used to create reusable software components.
COM+ : COM+ is an extension of Component Object Model (COM). COM+ is both an OOP architecture and a set of operating system services.
DCOM: an extension of the Component Object Model (COM) that allows COM components to communicate across network boundaries. Traditional COM components can only perform interprocess communication across process boundaries on the same machine. DCOM uses the RPC mechanism to transparently send and receive information between COM components (i.e., clients and servers) on the same network.

Boxing and unboxing :-
Implicit (manual) conversion of value type to reference type of a variable is known as BOXING, for example integer to object type conversion. Conversion of Boxed type variable back to value type is called as UnBoxing.

Garbage collection :-
Garbage Collection is Automatic Memory Manager for the .Net framework. It manages the memory allocated to the .NET framework. CLR takes cares about .NET framework. When a variable is defined, its gets a space in the memory and when the program control comes out of that function the scope of variable gets ended, so the garbage collection acts on and memory will releases.

Unmanaged Code :-
Unmanaged code is what you use to make before Visual Studio .NET 2002 was released. Visual Basic 6, Visual C++ 6. It compiled directly to machine code that ran on the machine where you compiled it—and on other machines as long as they had the same chip, or nearly the same.

Size of datatype int in .NET :- 32 bits

Size of char :- 16 bits (Unicode)

Where value-type variables allocated in the computer RAM :- Stack

Where reference-type variables go in the RAM :-
The references go on the stack, while the objects themselves go on the heap. However, in reality things are more elaborate.

Value-type variables vs reference-type variables in terms of garbage collection :-
The value-type variables are not garbage-collected, they just fall off the stack when they fall out of scope, the reference-type objects are picked up by GC when their references go null.

Why need to box a primitive variable :-
To pass it by reference or apply a method that an object supports, but primitive doesn’t.

Enforce garbage collection in .NET :- System.GC.Collect();
What happens when you encounter a continue statement inside the for loop :-
The code for the rest of the loop is ignored, the control is transferred back to the beginning of the loop.

Compilation process for .NET code :-
Source code is compiled and run in the .NET Framework using a two-stage process. First, source code is compiled to Microsoft intermediate language (MSIL) code using a .NET Framework-compatible compiler, such as that for Visual Basic .NET or Visual C#. Second, MSIL code is compiled to native code.

Where would you use an iHTTPModule :-
One of ASP.NET’s most useful features is the extensibility of the HTTP pipeline, the path that data takes between client and server. You can use them to extend your ASP.NET applications by adding pre- and post-processing to each HTTP request coming into your application. For example, if you wanted custom authentication facilities for your application, the best technique would be to intercept the request when it comes in and process the request in a custom HTTP module.

Diffgram :-
A DiffGram is an XML format that is used to identify current and original versions of data elements. The DataSet uses the DiffGram format to load and persist its contents, and to serialize its contents for transport across a network connection. When a DataSet is written as a DiffGram, it populates the DiffGram with all the necessary information to accurately recreate the contents, though not the schema, of the DataSet, including column values from both the Original and Current row versions, row error information, and row order.

Authentication and Authorization :-
Authentication is the process of validating a user on the credentials(username and password) and authorization performs after authentication. After Authentication a user will verified for performing the various tasks, It access is limited it is known as authorization.

Satellite Assemblies :-
Satellite assemblies hold the cultural information. Cultural refers the cultural information about the region in the application is going to use. Satellite assemblies provide an application the multilingual support. Satellite assemblies contain alternate sets of resources to be used in the application for different cultures.

Create AppDomain :-
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.
The logical and physical boundary created around every .NET application by the Common Language Runtime (CLR). The CLR can allow multiple .NET applications to be run in a single process by loading them into separate application domains.
AppDomains can also be explicitly created by .NET applications. Here is a C# sample which creates an AppDomain, creates an instance of an object inside it, and then executes one of the object's methods.
using System;
using System.Runtime.Remoting;
public class CAppDomainInfo : MarshalByRefObject
{
    public string GetAppDomainInfo()
    {
        return "AppDomain = " + AppDomain.CurrentDomain.FriendlyName;
    }
}
public class App
{
    public static int Main()
    {
        AppDomain ad = AppDomain.CreateDomain("Andy's new domain", null, null);
        ObjectHandle oh = ad.CreateInstance("appdomaintest", "CAppDomainInfo");
        CAppDomainInfo adInfo = (CAppDomainInfo)(oh.Unwrap());
        string info = adInfo.GetAppDomainInfo();
        Console.WriteLine("AppDomain info: " + info);
        return 0;
    }
}

Early & Late Binding :-
Early binding is to know the type of an object at compile time. The compiler have all the needed element at compile time to build the call into the executable code (resolution of calls at compile time)
- With late binding, the type of an object is known only at runtime. It will need extra instructions to find out where is the method to be called (if it exists) before calling it (resolution of calls at runtime).
Early binding is when the actual object behind the pointer is resolved at compile time.
Late binding is when the actual object behind the pointer is resolved at run time.

Deep Copy vs Shallow Copy :-
Shallow copying is creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type --> a bit-by-bit copy of the field is performed; for a reference type --> the reference is copied but the referred object is not; therefore the original object and its clone refer to the same object.
In C# and VB.NET, shallow copy is done by the object method MemberwiseClone().
Deep copy is creating a new object and then copying the nonstatic fields of the current object to the new object. If a field is a value type --> a bit-by-bit copy of the field is performed. If a field is a reference type --> a new copy of the referred object is performed.
Note: the classes to be cloned must be flagged as [Serializable].

Mutable type vs immutable type :-
Immutable types are types whose instance data, fields and properties, does not change after the instance is created. Most value types are immutable, but the mutable type is a type whose instance data, fields and properties, can be changed after the instance is created. Most Reference Types are mutable.

Sort the elements of the array in descending order :-
By calling Sort() and then Reverse() methods.

.NET datatype that allows the retrieval of data by a unique key :- HashTable

Debug class vs Trace class :-
Debug class for debug builds, use Trace class for both debug and release builds.

Three test cases to go through in unit testing :-
Positive test cases (correct data, correct output), negative test cases (broken or missing data, proper handling), exception test cases (exceptions are thrown and caught properly).

Role of inetinfo.exe, aspnet_isapi.dll and aspnet_wp.exe in the page loading process :-
inetinfo.exe is the Microsoft IIS server running, handling ASP.NET requests among other things. When an ASP.NET request is received (usually a file with .aspx extension), the ISAPI filter aspnet_isapi.dll takes care of it by passing the request to the actual worker process aspnet_wp.exe.

Create a resource-only assembly :-
Resources are nonexecutable data in an application and the data can be updated without recompiling application. Resource assemblies can be created as follows:
Add resource files to an empty project.
Built the project.
The resource will get compiled into assembly.

Retrieve resources using ResourceManager class :-
ResourceManager class is used to retrieve resources at run time.
Create a ResourceManager with resource file name and the resource assembly as parameters.
After having created, you can use ResourceManager.GetString method to retrieve a string.
Use the ResourceManager.GetObject method to retrieve images and objects from a resource file.

Install assembly to the Global Assembly Cache :-
Followings are the steps to install assembly to the GAC.
Sign assembly with a strong name using strong name utility, sn.exe.
Open the AssemblyInfo file for your project.
Use the AssemblyKeyFileAttribute to specify the path to the key file for your project.
Build your assembly. Install the assembly to GAC by using gacutil utility e.g. gacutil -i abc.dll

Purpose of the finally block :-
The code in finally block is guaranteed to run, irrespective of whether an error occurs or not.

Runtime host :-
The runtime host is the environment in which the CLR is started and managed.

Thread :-
Thread is a basic unit to which the operating system allocate processor time.
Thread.Join() in threading :-
Two versions of Thread.Join,
Thread.join()
Thread.join(Integer) this returns a Boolean value
You have "Thread1" and "Thread2" and while executing 'Thread1" you call "Thread2.Join()". So, "Thread1" will wait until "Thread2" has completed its execution and the again invoke "Thread1".
Thread.Join(Integer) ensures that threads do not wait for a long time. If it exceeds a specific time which is provided in integer the waiting thread will start.


Stop a long running thread :- Thread.Abort()

Sleep vs Suspend in Threading :-
Suspend allows you to block a thread until another thread calls Thread.Resume. The difference between Sleep and Suspend is that the latter does not immediately place a thread in the wait state. The thread does not suspend until the .NET runtime determines that it is in a safe place to suspend it. Sleep will immediately place a thread in a wait state.

Thread sleep for infinite period :-
Thread.Sleep (System.Threading.Timeout.Infinite)

Thread.Sleep() in threading :-
Thread's execution can be caused by calling the Thread.Sleep method. This method takes an integer value that determines how long the thread should sleep. Example Thread.CurrentThread.Sleep(2000).

Reference current thread of the method :-
Thread.CurrentThread

AddressOf operator do in background :-
AddressOf operator creates a delegate object to the BackgroundProcess method.

Change priority of the thread :-
Threadname.Priority = ThreadPriority.Highest
Following are different levels of Priority provided by .NET :-
. ThreadPriority.Highest
. ThreadPriority.AboveNormal
. ThreadPriority.Normal
. ThreadPriority.BelowNormal
. ThreadPriority.Lowest


Code Access security :-
CAS is part of .NET security model that determines whether or not a piece of code is allowed to run and what resources it can use while running.

Daemon thread :-
The thread which is run in background. Exp, garbage collector.

Thread vs Process :-
A thread is a path of execution that run on CPU, a process is a collection of thread that share the same virtual memory.

Application Domain :-
In application domain multiple application can run in same process without influencing each other. If one of the application domains throws error it does not affect the other application domains.

Types of threading models :-
Single Threading: This is the simplest and most common threading model where a single thread corresponds to your entire application’s process.

Apartment Threading (STA): This allows multiple threads to exist in a single application. In single threading apartment (STA), each thread is isolated in it’s own apartment. The process may contain multiple threads (apartments) however when an object is created in a thread (i.e. apartment) it stays within that apartment. If any communication needs to occur between different threads (i.e. different apartments) then we must marshal the first
thread object to the second thread. Free Threading: The most complex threading model. Unlike STA, threads are not confined to their own apartments. Multiple treads can make calls to the same methods and same components at the same time.

No comments:

Post a Comment