Garbage collection is a CLR feature, which automatically manages memory. Programmers forget to release the objects while coding ... Laziness (Remember in VB6 where one of the good practices is to set object to nothing). CLR automatically releases objects when they are no longer in use and refernced. CLR runs on non-deterministic to see the unused objects and cleans them.One side effect of this non-deterministic feature is that we cannot assume an object is destroyedwhen it goes out of the scope of a function. We should avoid using destructors because before GCdestroys the object it first executes destructor in that case it will have to wait for code to release the unmanaged resource. This results in additional delays in GC. So it is recommended to implement IDisposable interface, write cleanup code in Dispose method, and call GC.SuppressFinalize method. Its like instructing GC not to call your constructor.
System.GC.Collect () forces garbage collector to run. This is not recommended but can be used ifsituations arise.
Relection
All .NET assemblies have metadata information stored about the types defined in modules. This
metadata information can be accessed by mechanism called as “Reflection”. System. Reflection
can be used to browse through the metadata information.
Using reflection, you can also dynamically invoke methods using System.Type.Invokemember.
Friday, November 7, 2008
Interoperatability and Threads
What is NameSpace?
Namespace has two basic functionality:-
• NameSpace Logically group types, example System.Web.UI logically groups UI
related features.
• In Object Oriented world, many times it is possible that programmers will use the
same class name. Qualifying NameSpace with class name can avoid this collision.
If you want to view an Assembly how do you go about it?
Twist: What is ILDASM?
When it comes to understanding of internals, nothing can beat ILDASM. ILDASM converts the
whole ‘exe’ or ‘dll’ in to IL code. To run ILDASM you have to go to ‘C:\Program
Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin’. Note that we had v1.1 you have to
probably change it depending on the type of framework version you have.
If you run IDASM.EXE from the path you will be popped with the IDASM exe program as
shown in figure ILDASM. Click on file and browse to the respective directory for the DLL whose
assembly you want to view. After you select the DLL you will be popped with a tree view details
of the DLL as shown in figure ILDASM. On double clicking on manifest, you will be able to
view details of assembly, internal IL code etc as shown in Figure Abvoe ‘Manifest View’.
What is Manifest?
Assembly metadata is stored in Manifest. Manifest contains all the metadata needed to do the
following things (See Figure Manifest View for more details):
• 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 with Microsoft intermediate language (MSIL) code or in a
stand-alone PE file, that contains only assembly manifest information.
What is GAC?
Twist: In what situations will you register .NET assembly in GAC?
GAC (Global Assembly Cache) is where all shared .NET assembly reside. GAC is used in the
following situations:-
• If the application has to be shared among several application.
• If the assembly has some special security, requirements like only administrators can
remove the assembly. If the assembly is private then a simple delete of assembly
the assembly file will remove the assembly.
What is COM?
Microsoft’s COM is a technology for component software development. It is a binary standard,
which is language independent. DCOM is a distributed extension of COM.
What is Reference counting in COM?
Reference counting is a memory management technique used to count how many times an object
has a pointer referring to it. The first time it is created, the reference count is set to one. When the
last reference to the object is nulled, the reference count is set to zero and the object is deleted.
Care must be exercised to prevent a context switch from changing the reference count at the time
of deletion. In the methods that follow, the syntax is shortened to keep the scope of the discussion
brief and manageable.
Can you describe IUKNOWN interface in short?
Every COM object supports at least one interface, the IUnknown interface. All interfaces are
classes derived from the base class IUnknown. Each interface supports methods access data and
perform operations transparently to the programmer. For example, IUnknown supports three
methods, AddRef, Release(), and QueryInterface(). Suppose that pinterf is a pointer to an
IUnknown. pinterf->AddRef() increments the reference count. pinterf->Release() decrements the
reference count, deleting the object when the reference count reaches zero. pinterf-
>QueryInterface (IDesired, pDesired) checks to see if the current interface (IUnknown) supports
another interface, IDesired, creates an instance (via a call to CoCreateInstance ()) of the object if
the reference count is zero (the object does not yet exist), and then calls pDesired->AddRef () to
increment the reference count (where pDesired is a pointer to IDesired) and returns the pointer to
the caller.
Can you explain what DCOM is?
DCOM differs from COM in that it allows for creating objects distributed across a network, a
protocol for invoking that object’s methods, and secures access to the object. DCOM provides a
wrapper around COM, hence it is a backwards compatible extension. DCOM uses Remote
Procedural Calls (RPC) using Open Software Foundation’s Distributed Computing Environment.
These RPC are implemented over TCP/IP and named pipes. The protocol, which is actually being
used, is registered just prior to use, as opposed to being registered at initialization time. The
reason for this is that if a protocol is not being used, it will not be loaded.
Types of Transactions are there in COM + .NET5 transactions types can be used with COM+. Whenever an object is registered with COM+, it
has to abide either to these 5 transaction types.
Disabled: - There is no transaction. COM+ does not provide transaction support for this
component.
Not Supported: - Component does not support transactions. Hence even if the calling component
in the hierarchy is transaction enabled this component will not participate in the transaction.
Supported: - Components with transaction type support will be a part of the transaction. This
will be only if the calling component has an active transaction. If the calling component is not
transaction enabled this component will not start a new transaction.
Required: - Components with this attribute require a transaction i.e. either the calling should
have a transaction in place else, this component will start a new transaction.
Required New: - Components enabled with this transaction type always require a new
transaction. Components with required new transaction type instantiate a new transaction for
themselves every time.
Threading
What is Multi-tasking?
It is a feature of modern operating systems with which we can run multiple programs at same
time example Word, Excel etc.
What is Multi-threading?
Multi-threading forms subset of Multi-tasking. Instead of having to switch between programs,
this feature switches between different parts of the same program. Example you are writing in
word and at the same time word is doing a spell check in background.
What is a Thread?
A thread is the basic unit to which the operating system allocates processor time.
Did VB6 support multi-threading?
While VB6 supports multiple single-threaded apartments, it does not support a free-threading
model, which allows multiple threads to run against the same set of data.
we have multiple threads in one App domain?
One or more threads run in an AppDomain. An AppDomain is a runtime representation of a
logical process within a physical process. Each AppDomain is started with a single thread, but
can create additional threads from any of its threads.
.NET Basics
We just thought we should first start with the most asked question, differences between all
framework versions. Below figure '.NET growth' shows how the framework has evolved. In .NET
1.1 we had the basic framework, web services, CLR, ADO etc...NET 2.0 paid more attention on
increasing the productivity of the developer. They had modules like MARS, Generics, Partial
classes, DPAPI etc...NET 3.0 was more about fulfilling the SOA dreams. They had modules like
WCF, WPF and WWF. We have complete chapter on 3.0 please read it to understand the
fundamentals..NET 3.5 has new query capabilities like LINQ, AJAX which is now an integral
part of 3.5 setup and new protocol support for WS-* specifications
What is an IL?
Twist: - What is MSIL or CIL, What is JIT?
(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.
What is a CLR?
Full form of CLR is Common Language Runtime and it forms the heart of the .NET framework.
All Languages have runtime and it is the responsibility of the runtime to take care of the code
execution of the program. For example, VC++ has MSCRT40.DLL, VB6 has MSVBVM60.DLL,
and Java has Java Virtual Machine etc. Similarly, .NET has CLR. Following are the
responsibilities of CLR
• Garbage Collection: - CLR automatically manages memory thus eliminating
memory leaks. When objects are not referred, GC automatically releases those
memories thus providing efficient memory management.
• Code Access Security: - CAS grants rights to program depending on the security
configuration of the machine. Example the program has rights to edit or create a
new file but the security configuration of machine does not allow the program to
delete a file. CAS will take care that the code runs under the environment of
machines security configuration.
• Code Verification: - This ensures proper code execution and type safety while the
code runs. It prevents the source code to perform illegal operation such as accessing
invalid memory locations etc.
What is CTS?
In order that two language communicate smoothly CLR has CTS (Common Type
System).Example in VB you have “Integer” and in C++ you have “long” these datatypes are not
compatible so the interfacing between them is very complicated. In order that these two different
languages communicate Microsoft introduced Common Type System. So “Integer” data type in
VB6 and “int” data type in C++ will convert it to System.int32, which is data type of CTS. CLS,
which is covered in the coming question, is subset of CTS.
What is a CLS (Common Language Specification)?
This is a subset of the CTS, which all .NET languages are expected to support. It was always a
dream of Microsoft to unite all different languages in to one umbrella and CLS is one-step
towards that. Microsoft has defined CLS, which are nothing but guidelines, that language should
follow so that it can communicate with other .NET languages in a seamless manner.
What is a Managed Code?
Managed code runs inside the environment of CLR i.e. .NET runtime. In short, all IL are
managed code. 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 a Assembly?
• Assembly is unit of deployment like EXE or a DLL.
• An assembly consists of one or more files (dlls, exe’s, html files etc.), and
represents a group of resources, type definitions, and implementations of those
types. An assembly may also contain references to other assemblies. These
resources, types and references are described in a block of data called a manifest.
The manifest is part of the assembly, thus making the assembly self-describing.
• An assembly is completely self-describing. An assembly contains metadata
information, which is used by the CLR for everything from type checking an
security to actually invoking the components methods. As all information is in
the assembly itself, it is independent of registry. This is the basic advantage as
compared to COM where the version was stored in registry.
• Multiple versions can be deployed side by side in different folders. These different
versions can execute at the same time without interfering with each other.
Assemblies can be private or shared. For private assembly deployment, the
assembly is copied to the same directory as the client program that references it. No
registration is needed, and no fancy installation program is required. When thecomponent is removed, no registry cleanup is needed, and no uninstall program is
required. Just delete it from the hard drive.
• In shared assembly deployment, an assembly is installed in the Global Assembly
Cache (or GAC). The GAC contains shared assemblies that are globally accessible
to all .NET applications on the machine.
Subscribe to:
Comments (Atom)