Rolta
Rolta
Rolta
Assemblies are the building blocks of .NET Framework applications; they form the fundamental
unit of deployment, version control, reuse, activation scoping, and security permissions. An
assembly is a collection of types and resources that are built to work together and form a logical
unit of functionality. An assembly provides the common language runtime with the information it
needs to be aware of type implementations.
Alternatively, the elements of an assembly can be contained in several files. These files can be
modules of compiled code (.netmodule), resources (such as .bmp or .jpg files), or other files
required by the application. Create a multifile assembly when you want to combine modules
written in different languages and to optimize downloading an application by putting seldom used
types in a module that is downloaded only when needed.
In the following illustration, the developer of a hypothetical application has chosen to separate
some utility code into a different module and to keep a large resource file (in this case a .bmp
image) in its original file. The .NET Framework downloads a file only when it is referenced;
keeping infrequently referenced code in a separate file from the application optimizes code
download.
Multifile assembly
Note The files that make up a multifile assembly are not physically linked by the file system.
Rather, they are linked through the assembly manifest and the common language runtime
manages them as a unit.
In this illustration, all three files belong to an assembly, as described in the assembly manifest
contained in MyAssembly.dll. To the file system, they are three separate files. Note that the file
Util.netmodule was compiled as a module because it contains no assembly information. When
the assembly was created, the assembly manifest was added to MyAssembly.dll, indicating its
relationship with Util.netmodule and Graphic.bmp.
Assembly Manifest
Every assembly, whether static or dynamic, contains a collection of data that describes how the
elements in the assembly relate to each other. The assembly manifest contains this assembly
metadata. An assembly manifest contains all the metadata needed to specify the assembly's
version requirements and security identity, and all metadata needed to define the scope of the
assembly and resolve references to resources and classes. The assembly manifest can be stored
in either a PE file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a
standalone PE file that contains only assembly manifest information.
The following illustration shows the different ways the manifest can be stored.
Types of assemblies
For an assembly with one associated file, the manifest is incorporated into the PE file to form a
single-file assembly. You can create a multifile assembly with a standalone manifest file or with
the manifest incorporated into one of the PE files in the assembly.
Each assembly's manifest performs the following functions:
Enumerates the files that make up the assembly.
Governs how references to the assembly's types and resources map to the files that contain their
declarations and implementations.
Enumerates other assemblies on which the assembly depends.
Provides a level of indirection between consumers of the assembly and the assembly's
implementation details.
Renders the assembly self-describing.
Assembly Manifest Contents
The following table shows the information contained in the assembly manifest. The first four items
— the assembly name, version number, culture, and strong name information — make up the
assembly's identity.
Information Description
Constructors
Constructors are class methods that are executed when an object of a given type is created.
Constructors have the same name as the class, and usually initialize the data members of the
new object.
A constructor that takes no parameters is called a default constructor. Default constructors are
invoked whenever an object is instantiated by using the new operator and no arguments are
provided to new.
Unless the class is static, classes without constructors are given a public default constructor by
the C# compiler in order to enable class instantiation
A private constructor is a special instance constructor. It is generally used in classes that contain
static members only. If a class has one or more private constructors and no public constructors,
other classes (except nested classes) cannot create instances of this class
A static constructor is used to initialize any static data, or to perform a particular action that needs
performed once only. It is called automatically before the first instance is created or any static
members are referenced.
Copy Code
class Car
{
~Car() // destructor
{
// cleanup statements...
}
}
The destructor implicitly calls Finalize on the base class of the object. Therefore, the previous
destructor code is implicitly translated to the following code:
Copy Code
protected override void Finalize()
{
try
{
// Cleanup statements...
}
finally
{
base.Finalize();
}
}
This means that the Finalize method is called recursively for all instances in the inheritance chain,
from the most-derived to the least-derived.
Note:
Empty destructors should not be used. When a class contains a destructor, an entry is created in
the Finalize queue. When the destructor is called, the garbage collector is invoked to process the
queue. If the destructor is empty, this just causes a needless loss of performance.
The programmer has no control over when the destructor is called because this is determined by
the garbage collector. The garbage collector checks for objects that are no longer being used by
the application. If it considers an object eligible for destruction, it calls the destructor (if any) and
reclaims the memory used to store the object. Destructors are also called when the program
exits.
It is possible to force garbage collection by calling Collect, but most of the time, this should be
avoided because it may create performance issues.
Using Destructors to Release Resources
In general, C# does not require as much memory management as is needed when you develop
with a language that does not target a runtime with garbage collection. This is because the .NET
Framework garbage collector implicitly manages the allocation and release of memory for your
objects. However, when your application encapsulates unmanaged resources such as windows,
files, and network connections, you should use destructors to free those resources. When the
object is eligible for destruction, the garbage collector runs the Finalize method of the object.
Explicit Release of Resources
If your application is using an expensive external resource, we also recommend that you provide
a way to explicitly release the resource before the garbage collector frees the object. You do this
by implementing a Dispose method from the IDisposable interface that performs the necessary
cleanup for the object. This can considerably improve the performance of the application. Even
with this explicit control over resources, the destructor becomes a safeguard to clean up
resources if the call to the Dispose method failed.
Source code written in C# is compiled into an intermediate language (IL) that conforms to the CLI
specification. The IL code, along with resources such as bitmaps and strings, is stored on disk in
an executable file called an assembly, typically with an extension of .exe or .dll. An assembly
contains a manifest that provides information on the assembly's types, version, culture, and
security requirements.
When the C# program is executed, the assembly is loaded into the CLR, which might take
various actions based on the information in the manifest. Then, if the security requirements are
met, the CLR performs just in time (JIT) compilation to convert the IL code into native machine
instructions. The CLR also provides other services related to automatic garbage collection,
exception handling, and resource management.
Serialization. :
Serialization is the process of converting the state of an object into a form that can be persisted or
transported. The complement of serialization is deserialization, which converts a stream into an
object. Together, these processes allow data to be easily stored and transferred.
The .NET Framework features two serializing technologies:
Binary serialization preserves type fidelity, which is useful for preserving the state of an object
between different invocations of an application. For example, you can share an object between
different applications by serializing it to the Clipboard. You can serialize an object to a stream, to
a disk, to memory, over the network, and so forth. Remoting uses serialization to pass objects "by
value" from one computer or application domain to another.
XML serialization serializes only public properties and fields and does not preserve type fidelity.
This is useful when you want to provide or consume data without restricting the application that
uses the data. Because XML is an open standard, it is an attractive choice for sharing data
across the Web. SOAP is likewise an open standard, which makes it an attractive choice.
Delegates (C# Programming Guide)
A delegate is a type that references a method. Once a delegate is assigned a method, it behaves
exactly like that method. The delegate method can be used like any other method, with
parameters and a return value, as in this example:
C#
Copy Code
public delegate int PerformCalculation(int x, int y);
Any method that matches the delegate's signature, which consists of the return type and
parameters, can be assigned to the delegate. This makes is possible to programmatically change
method calls, and also plug new code into existing classes. As long as you know the delegate's
signature, you can assign your own delegated method.
This ability to refer to a method as a parameter makes delegates ideal for defining callback
methods. For example, a sort algorithm could be passed a reference to the method that
compares two objects. Separating the comparison code allows the algorithm to be written in a
more general way.
Delegates Overview
Delegates have the following properties:
Delegates are similar to C++ function pointers, but are type safe.
Delegates allow methods to be passed as parameters.
Delegates can be used to define callback methods.
Delegates can be chained together; for example, multiple methods can be called on a single
event.
Methods don't need to match the delegate signature exactly. For more information, see
Covariance and Contravariance
C# version 2.0 introduces the concept of Anonymous Methods, which permit code blocks to be
passed as parameters in place of a separately defined method.
Garbage Collection
The .NET Framework's garbage collector manages the allocation and release of memory for your
application. Each time you use the newoperator to create an object, the runtime allocates
memory for the object from the managed heap. As long as address space is available in the
managed heap, the runtime continues to allocate space for new objects. However, memory is not
infinite. Eventually the garbage collector must perform a collection in order to free some memory.
The garbage collector's optimizing engine determines the best time to perform a collection, based
upon the allocations being made. When the garbage collector performs a collection, it checks for
objects in the managed heap that are no longer being used by the application and performs the
necessary operations to reclaim their memory.
This section describes how the garbage collector automatically manages the allocation and
release of memory for the managed objects in your application. In addition, it describes the
recommended design pattern to use to properly clean up any unmanaged resources that your
application creates.