.Net Framework Full Notes Free Downloads Any:
What is .NET?
.NET - is the Microsoft Web services strategy to connect information, people, systems, and devices
through software. Integrated across the Microsoft platform, .NET technology provides the ability to quickly build, deploy, manage, and use connected, security-enhanced solutions with Web services. .NET-connected solutions enable businesses to integrate their systems more rapidly and in a more agile manner and help them realize the promise of information anytime, anywhere, on any device.
Which languages can be used to program in .NET?
Microsoft provides these languages for programming .NET - C#, VB.NET, JS.NET, C++.NET.
C++.NET may be used as a Managed Code Language by using managed extensions. This is done using an _gc postfix. A managed C++ class can inherit from VB.NET classes, C# classes, JS.NET classes. A managed class can inherit from only one class. .NET does'nt allow multiple inheritance in managed classes.
Any Language that has a compiler that can compile the language code to MSIL is a .NET compliant language.
Below is an alphabetical list of languages supported by .NET
APL - It is a language for describing procedures in the processing of information. It is a very powerful language. It may be used to describe mathematical procedures.
C++ - A widely known language. One of the oldest Object Oriented Languages, an advanced version of the C language. Microsoft has its own Visual C++ compiler that includes special tools and libraries for development on Windows platform. C++ is an object-oriented programming language
that is viewed by many as the best language for creating large-scale applications. C++ is a superset of the C language.
C# - Pronounced C Sharp. It is a complete Object-Oriented programming language from Microsoft built into the .NET Framework. First created in the late 1990’s was part of Microsoft’s whole .NET strategy.
COBOL - Expanded as Common Business Oriented Language. It is a widely used high level language for developing Business Applications.
Component Pascal - Its a Pascal derived programming language for development of programming components.
Eiffel - It is an Object-Oriented programming language which emphasizes the production of robust software.
Forth - It is both a programming language & a programming environment. It supports shell programming to a high level.
Fortran - Stands for Formula Translator. Its a high level programming language used for scientific computations. It supports plenty of compact notations.
Haskell - It is a standardized functional programming language with non-strict semantics, named after the logician Haskell Curry. It was created by a committee formed in the 1980s for the express purpose of defining such a language.The latest semi-official language standard is Haskell 98, intended to specify a minimal, portable version of the language for teaching and as a base for future extensions.
Java - It is an object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems. The language, initially called Oak (named after the oak trees outside Gosling's office), was intended to replace C++, although the feature set better resembles that of Objective C. Java should not be confused with JavaScript, which shares only the name and a similar C-like syntax. Sun Microsystems currently maintains and updates Java regularly.
Microsoft JScript - A scripting language developed by Microsoft to enable Web page designers to design interactive sites. Although it shares many of the features and structures of the full Java language, it was developed independently. Jscript can interact with HTML source code, better enabling Web authors to spice up their sites with dynamic content.
Mercury - Mercury is a functional/logical programming language based on Prolog, but more useful for real-world programming.
Mondrian - It is a simple functional scripting language for Internet applications. It is a functional language specifically designed to inter-operate with other languages in an OO environment. Current versions of Mondrian run on .NET. Mondrian also supports ASP.NET, allowing you to embed functional language code in web pages along with C# code.
Oberon - It is a programming language very much like Modula-2 in syntax but with several interesting features. It's based on OOP concepts and also provides a Windows-based GUI.
Pascal - A high-level, highly structured, general-purpose programming language. Named after Blaise Pascal.
Perl - Stands for Practical Extraction and Report Language. It is a language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information.
Python - It is an interpreted, interactive, Object-Oriented programming language. Python combines remarkable power with very clear syntax. It has modules, classes, exceptions, very high level dynamic data types, and dynamic typing.
RPG - Stand for Report Program Generator. It is used for generation of reports from data files, including matching record and sub-total reports. RPG is one of the few languages created for punch card machines that is still in common use today.
Scheme - It is a statically scoped programming language. It was designed to have an exceptionally unambigous and simple semantics and few different ways to form expressions. A vast variety of programming paradigms, including imperative, functional, and message passing styles, find convenient expression in Scheme.
Smalltalk - It is a simple language that uses a simple sub set of human languages, nouns and verbs. Smalltalk was the first, and remains one of the few, pure object systems, which simply means that everything in a Smalltalk program is an object.
Standard ML - It is a safe, modular, strict, functional, polymorphic programming language with compile-time type checking and type inference, garbage collection, exception handling, immutable data types and updatable references, abstract data types, and parametric modules.
Microsoft Visual Basic - Most widely used language in the world today! Used for developing Windows based Applications, Windows Services, Remoting Applications, Web Services and Web Applications(using ASP.NET).
Which versions of .NET have been released so far?
The final version of the .NET 1.0 SDK & runtime were made publically available on 15 - Jan -2002. At the same time, the final version of Visual Studio.NET was made available to MSDN subscribers.
.NET 1.1 was released in April 2003, with bugs fixed. Visual Studio 2003 supports development of applications in version 1.1.
.NET 2.0 was launched in October 2005 for MSDN subscribers, and officially released in Nov 2005.
On - Jun - 2006, .NET 3.0 was launched. This version was earlier called WinFX. Visual Studio 2005 supports development of .NET 2.0 and .NET 3.0 applications. .NET 3 is comprised of the following:
Windows Communication Foundation
Windows Presentation Foundation
Windows Workflow Foundation
Windows Cardspace
Which tools can be used for .NET Development?
The .NET Framework SDK is free and includes command-line compilers for C++, C#, and VB.NET and various other utilities to aid development.
SharpDevelop is a free IDE for C# and VB.NET.
Microsoft Visual Studio Express editions are cut-down versions of Visual Studio, for hobbyist or novice developers and are available for FREE Download at Microsoft site. Note that .NET 2.0 Framework gets downloaded along with Visual Studio Express & All versions above Visual Studio Express. Download Visual Studio Express
There are different versions for C#, VB, web development etc. Microsoft Visual Studio Standard 2005 is around $300, or $200 for the upgrade.
Microsoft VIsual Studio Professional 2005 is around $800, or $550 for the upgrade. At the top end of the price range are the Microsoft Visual Studio Team Edition for Software Developers 2005 with MSDN Premium and Team Suite editions. Visual Web Developer Express is available as a free download.
Explain CLI, CIL, CTS, Metadata, CLS, IL and VES in .NET
CLI - Common Language Infrastructure. Microsoft has a piece of shared source, its the public implementation of ECMA Common Language Infrastructure. This shared code is code-named "Rotor". It has around 3 million lines of code. Those who are interested in development of a language that targets the .NET Framework, may extensively make use of this CLI. The following topics are covered in the Shared Source CLI :
* The CLI type system
* Component packing & assemblies
* Type Loading & JIT Compilation
* Managed code & Execution Engine (CLR)
* Description of Garbage Collection process & memory management
* The Platform Adaptation Layer (PAL): a portability layer for Win32®, Mac OS® X, and FreeBSD
Its been written by the Microsoft Team that has developed the .NET Framework.
Note: A compiled managed assembly is comprised of IL, Metadata and Manifest.
CIL Stands for Common Intermediate Language. Its actually a low level human readable language implementation of CLI. All .NET-aware languages compile the source ode to an intermediate language called Common Intermediate Language using the language specific compiler. It is also possible to build .NET assemblies directly using CIL using the ilasm.exe compiler. This compiler is shipped along with the .NET Framework 2.0 SDK. CIL is the only language that allows access to each aspect of the CTS. CIL is the definition of the fundamentals of the .NET framework.
CTS - stands for Common Type Specification. It is at the core of .NET Framework's cross-language integration, type safety, and high-performance code execution. It defines a common set of types that can be used with many different language syntaxes. Each language (C#, VB.NET, Managed C++, and so on) is free to define any syntax it wishes, but if that language is built on the CLR, it will use at least some of the types defined by the CTS.
Metadata - is code that describes the compiled IL. A .NET language compiler will generate the metadata and store this in the assembly containing the CIL. Metadata describes all class members and classes that are defined in the assembly, and the classes and class members that the current assembly will call from another assembly. The metadata for a method contains the complete description of the method, including the class (and the assembly that contains the class), the return type and all of the method parameters. When the CLR executes CIL it will check to make sure that the metadata of the called method is the same as the metadata that is stored in the calling method. This ensures that a method can only be called with exactly the correct number of parameters and exactly the correct parameter types.
CLS - Common Language Specification. A type that is CLS compliant, may be used across any .NET language. CLS is a set of language rules that defines language standards for a .NET language and types declared in it. While declaring a new type, if we make use of the [CLSCompliant] attribute, the type is forced to conform to the rules of CLS.
IL - Intermediate Language, is the compiled form of the .NET language source code. When .NET source code is compiled by the language specific compiler (say we compile C# code using csc.exe), it is compiled to a .NET binary, which is platform independent, and is called Intermediate Language code. The .NET binary also comprises of metadata.
Its important to note here that metadata describes the IL, whereas manifest describes the assembly.
VES - Virtual Execution System. The Virtual Execution System(VES) provides an environment for executing managed code. It provides direct support for a set of built-in data types, defines a hypothetical machine with an associated machine model and state, a set of control flow constructs, and an exception handling model. To a large extent, the purpose of the VES is to provide the support required to execute the Common Intermediate Language instruction set.
What is CLR in .NET?
Common Language Runtime - It is the implementation of CLI. The core runtime engine in the Microsoft .NET Framework for executing applications. The common language runtime supplies managed code with services such as cross-language integration, code access security, object lifetime management, resouce management, type safety, pre-emptive threading, metadata services (type reflection), and debugging and profiling support. The ASP.NET
Framework and Internet Explorer are examples of hosting CLR.
The CLR is a multi-language execution environment. There are currently over 15 compilers being built by Microsoft and other companies that produce code that will execute in the CLR.
The CLR is described as the "execution engine" of .NET. It's this CLR that manages the execution of programs. It provides the environment within which the programs run. The software version of .NET is actually the CLR version.
What is a Class Library in .NET?
Class library is the another major entity of the .NET Framework. This library gives the program access to runtime environment. The class library consists of lots of prewritten code that all the applications
created in .NET aware languages and Visual Studio .NET will use. The code for all the elements like forms, controls and the rest in VB .NET applications actually comes from the class library.
Explain Managed code, managed class and managed data in .NET
Managed Code - The .NET framework provides lots of core runtime services to the programs that run within it. For example - security & exception handling. Such a code has a minimum level of information. It has metadata associated with it. Such a code is called Managed Code. VB.NET, C#, JS.NET code is managed by default. In order to make C++ code managed, we make use of managed extensions, which is nothing but a postfix _gc after the class name.
Managed Data - Data that is allocated & freed by the .NET runtime's Garbage collector.
Managed Class - A class whose objects are managed by the CLR's garbage collector. In VC++.NET, classes are not managed. However, they can be managed using managed extensions. This is done using an _gc postfix. A managed C++ class can inherit from VB.NET classes, C# classes, JS.NET classes. A managed class can inherit from only one class. .NET doesn’t allow multiple inheritance in managed classes.
What is an assembly in .NET? What is ILDASM?
Assembly - An assembly may be an exe, a dll, an application having an entry point, or a library. It may consist of one or more files. It represents a group of resources, type definitions, and implementation of these types. They may contain references to other assemblies. These resources, types & references are compacted in a block of data called manifest. The manifest is a part of the assembly, which makes it self-describing. Assemblies also increase security of code in .NET. An assembly maybe shared(public) or private. The assembly, overall comprises of 3 entities: IL, Manifest, Metadata. Metadata describes IL, whereas Manifest describes the assembly.
An assembly may be created by building the class(the .vb or .cs file), thereby producing its DLL.
ILDASM - The contents of an assembly may be viewed using the ILDASM tool, that comes with the .NET SDK or the Visual Studio.NET. The ildasm.exe tool may also be used in the command line compiler.
What is Reflection in .NET?
Reflection - The process of getting the metadata from modules/assemblies. When .NET code is compiled, metadata about the types defined in the modules is produced. These modules are in turn packaged as assembled. The process of accessing this metadata in called Reflection.
The namespace System. Reflection contains classes that can be used for interrogating the types for a module/assembly. We use reflection for examining data type sizes for marshalling across process & machine boundaries.
Reflection is also used for:
1) To dynamically invoke methods (using System.Type.InvokeMember)
2) To dynamically create types at runtime (using System.Reflection.Emit.TypeBuilder).
What are the different types of assemblies in .NET?
An assembly may be Public or Private. A public assembly is also called a Shared Assembly.
Difference between Public and Private assembly
A Satellite Assembly - is an assembly that contains only resources, and no code. The resources are location specific. A satellite assembly is associated with a main assembly, the one that actually contains the code.
How to share an assembly? How to make it strongly named? What is SN.EXE?
In order to share an assembly with many applications, it must have a strong name.
In order to convert a private assembly to a shared assembly, i.e. to create a strongly named assembly, follow the steps below...
1) Create a strong key using the sn.exe tool. This is used to created a cryptographic key pair. The key pair that is generated by the Strong Name tool can be kept in a file or we can store it our your local machine's Crytographic Service Provider (CSP). For this, goto the .NET command interpreter, and type the following...
sn -k C:\samplekey.snk
This will create a strong key and save it to the location C:\samplekey.snk
2) If the key is stored in a file, just like we have done above, we use the attribute AssemblyKeyFileAttribute. This belongs to the namespace System.Reflection.AssemblyKeyFileAttribute.
If the key was in the CSP, we would make use of System.Reflection.AssemblyKeyNameAttribute.
Go to the assemblyinfo.vb file of your project. Open this file. Make the following changes in this file...
<assembly: assemblykeyfileattribute ("C:\samplekey.snk")>
We may write this in our code as well, like this...
Imports System.Reflection
<assembly: assemblykeyfileattribute("C:\samplekey.snk")>
Namespace Strong Name
Public class Sample
End Class
End Namespace
3) Build your project. Your assembly is now strongly named.
Installing the Shared assembly in GAC...
Go to .NET command interpreter, use the tool gacutil.exe
Type the following...
gaudily /i sampleclass.dll To uninstall it, use... actual /u sampleclass.dll Visual Studio.NET provides a GUI tool for viewing all shared assemblies in the GAC.
Folder of GAC: C:\Windows\Assembly OR C:\Winnt\Assembly
What is GAC?
A shared assembly has version constraints. It is stored in the Global Assembly Cache (GAC). GAC is a repository of shared assemblies maintained by the .NET runtime. The shared assemblies may be used by many applications. To make an assembly a shared assembly, it has to be strongly named.
To know more on how to share an assembly, create a strongly named assembly
GAC is located at C:\Windows\Assembly OR C:\Winnt\Assembly
Can garbage collector control the activities of a thread?
Garbage Collection - Garbage collection is a heap-management strategy where a run-time component takes responsibility for managing the lifetime of the memory used by objects. This concept is not new to .NET - Java and many other languages/runtimes have used garbage collection for some time. The garbage collector runs periodically. It runs through a list of objects that are currently being referenced by an application. All objects that it does not find during this search are ready to be destroyed (using the finalize method) and hence free the memory. However, the runtime gets notified of the object that has been destroyed, only in the next round of the garbage collector's periodic cycle.
In the class System.GC, there is a method called collect( ). This forces the garbage collector to collect all unreferenced objects immediately, thereby giving the developer some control over the garbage collector.
There is a concurrent setting that can be set through the applications's .config file. This specifies whether or not the garbage collector performs its activities on a specified thread or not.
We can view the performance monitor to view the activities of the garbage collector.
What is IDisposable interface in .NET?
IDisposable interface - We implement this interface to a class when we have to work with unmanaged types. For example, an IntPtr member representing an operating system's file handler is actually unmanaged, and in order to destroy it, we make use of the Dispose method by implementing the IDisposable interface. In this case, we override the Finalize method. Note that we make use of Dispose method on those objects which have an uncertain life period, and thus the garbage collector does not finalize them automatically.
We also make use of Dispose method while working with managed code, for example, an object os System.IO.FileStream may have an uncertain life, and in order to dispose it, we use the dispose method. Such types of objects are not accessed by the Garbage Collector's Finalize.
What is Serialization in .NET? Soap Formatter, Binary Formatter, XmlFormatter?
Serialization - The process of converting an object into a stream of bytes. This stream of bytes can be persisted. Federalization is an opposite process, which involves converting a stream of bytes into an object. Serialization is used usually during remoting (while transporting objects) and to persist file objects & database objects.
What is CAS in .NET?
Code Access Security (CAS) - CAS is the part of the .NET security model that determines whether or not code is allowed to run, and what resources it can use when it is running. For example, it is CAS that will prevent a .NET web applet from formatting your hard disk.
The CAS security policy revolves around two key concepts - code groups and permissions. Each .NET assembly is a member of a particular code group, and each code group is granted the permissions specified in a named permission set. For example, using the default security policy, a control downloaded from a web site belongs to the 'Zone - Internet' code group, which adheres to the permissions defined by the 'Internet' named permission set. (Naturally the 'Internet' named permission set represents a very restrictive range of permissions.) To view codegroups on our system, use the following command on .NET command interpretor... caspol -lg Note the hierarchy of code groups - the top of the hierarchy is the most general ('All code'), which is then sub-divided into several groups, each of which in turn can be sub-divided. Also note that (somewhat counter-intuitively) a sub-group can be associated with a more permissive permission set than its parent. If we want to trust a particular website giving it full rights to our system...Use caspol. For example, suppose we trust code from www.mydomain.com and we want it have full access to our system, but we want to keep the default restrictions for all other internet sites. To achieve this, we would add a new code group as a sub-group of the 'Zone - Internet' group,
like this: caspol -ag 1.3 -site www.mydomain.com FullTrust To change the permission, we use the -cg attribute.To turn off caspol, use caspol -s off
What is a class attribute in .NET?
Class Attributes - Is a kind of property attached with a class. It allows some data to be attached to a class or method. This data becomes part of the metadata for the class, and (like other class metadata) can be accessed via reflection. An example of a metadata attribute is [serializable], which can be attached to a class and means that instances of the class can be serialized.
What is a thread? How to use and create a thread in .NET?
Threads - When we want to run one or more instances of a method, we make use of threading. Suppose we have a method like this...
Private Sub OnGoingProcess()
Dim i As Integer = 1
Do While True
ListBox1.Items.Add("Repeatitions: " + i)
i += 1
Loop
End Sub
Dim t As Thread
t = New Thread(AddressOf Me.OnGoingProcess)
t.Start()
The AddressOf operator creates a delegate object to the BackgroundProcess method. A delegate within VB.NET is a type-safe, object-oriented function pointer. After the thread has been instantiated, you begin the execution of the code by calling the Start() method of the thread. After the thread is started, you have some control over the state of it by using methods of the Thread object. You can pause a thread's execution by calling the Thread.Sleep method. This method takes an integer value that determines how long the thread should sleep. If you wanted to slow down the addition of items to the listbox in the example above, place a call to the sleep method in this code:
Private Sub OnGoingProcess()
Dim i As Integer = 1
Do While True
ListBox1.Items.Add("Repeatitions: " + i)
i += 1
Thread.CurrentThread.Sleep(2000)
Loop
End Sub
You can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep (System.Threading.Timeout.Infinite). To interrupt this sleep you can call the Thread.Interrupt method. Similar to Sleep and Interrupt are Suspend and Resume. 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. Lastly, Thread.Abort stops a thread from executing. In our simple example, we would want to add another button on the form that allows us to stop the process. To do this all we would have to do is call the Thread.Abort method as follows:
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
t.Abort()
End Sub
This is where the power of multithreading can be seen. The UI seems responsive to the user because it is running in one thread and the background process is running in another thread. The cancel button immediately responds to the user's click event and processing stops. The next example shows a rather simple situation. Multithreading has many complications that we have to work out when we program. One issue that we will run into is passing data to and from the procedure passed to the constructor of the Thread class. That is to say, the procedure we want to kick off on another thread cannot be passed any parameters and we cannot return data from that procedure. This is because the procedure wepass to the thread constructor cannot have any parameters or return value. To get around this, wrap our procedure in a class where the parameters to the method are written as fields of the class. A simple example of this would be if we had a procedure that calculated the square of a number:
Function Square(ByVal Value As Double) As Double Return Value * Value End Function To make this procedure available to be used in a new thread we would wrap it in a class: Public Class SquareClass Public Value As Double Public Square As Double Public Sub CalcSquare() Square = Value * Value End Sub End Class
Use this code to start the CalcSquare procedure on a new thread. following code:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim oSquare As New SquareClass()
t = New Thread(AddressOf oSquare.CalcSquare)
oSquare.Value = 30
t.Start()
End Sub
Notice that after the thread is started, we do not inspect the square value of the class, because it is not guaranteed to have executed once you call the start method of the thread. There are a few ways to retrieve values back from another thread. The easiest way is to raise an event when the thread is complete. We will examine another method in the next section on thread synchronization
. The following code adds the event declarations to the SquareClass.
Public Class SquareClass
Public Value As Double
Public Square As Double
Public Event ThreadComplete(ByVal Square As Double)
Public Sub CalcSquare()
Square = Value * Value
RaiseEvent ThreadComplete(Square)
End Sub
End Class
Catching the events in the calling code has not changed much from VB6, you still declare the variables WithEvents and handle the event in a procedure. The part that has changed is that you declare that a procedure handles the event using the Handles keyword and not through the naming convention of Object_Event as in VB6. Dim WithEvents oSquare As SquareClass
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
oSquare = New SquareClass()
t = New Thread(AddressOf oSquare.CalcSquare)
oSquare.Value = 30
t.Start()
End Sub
Sub SquareEventHandler(ByVal Square As Double) _
Handles oSquare.ThreadComplete
MsgBox("The square is " & Square)
End Sub
The one thing to note with this method is that the procedure handling the event, in this case SquareEventHandler, will run within the thread that raised the event. It does not run within the thread from which the form is executing.
What is globalization in .NET?
Globalization refers to the process with which an application or software is designed and developed so as to make it run across all platforms and all sites with minimum or no modification to the software application. The software is very easy to customize so as to suit to the location-specific conditions and it is also capable of providing information based on the varied inputs and the location-specific operating system.
There are two processes in Globalization and they are customisation or localisation of the application and internationalizing the application codes so as to meet the standards of the local culture and other related matters.
What are Generations in Garbage Collector?
Generations in the Garbage Collector is a way of enhancing the garbage collection performance. In .NET, all resources are allocated space (memory) from the heap. Objects are automatically freed from the managed heap when they are no longer required by the application.
When objects are no longer being used by the application, the .NET runtime's garbage collector performs the task of collection, to determine the status of the objects. Necessary operations are performed to relieve the memory, in case the object is no longer in use. This is identified by the GC by examining the metadata of the object. For this, the GC has to know the location of the roots that represent the object. Roots are actually the location of the object on the managed heap. There are two types of memory references, strong & weak. When a root references an object, it is said to be a strong reference as the object is being pointed to by application code. The other type of object, that is not being referenced by the application code is called the weak reference, and this may be collected by the GC. However, this may still be accessed by the application code if required. But for the application to access the weakly referenced object, this has to be converted to a strong reference (and note that this has to be done before the GC collects the weakly referenced object).
So what are Generations in the GC? Its a feature of the GC that enhances its performance. There are 3 Generations...0,1,2.
Generation 0 - When an object is initialized, its in generation 0. These are new objects that have never been played around with by the GC. As and when more objects get created, the process of Garbage Collection is invoked by the CLR.
Generation 1 - The objects that survive the garbage collection process are considered to be in generation 1. These are the old objects.
Generation 2 - As more new objects get created and added to the memory, the new objects are added to generation 0, the generation 1 old objects become older, and so are considered to be in generation 2. Generation 2 is the highest level generation in the garbage collection process. Any further garbage collection process occuring causes the level 1 objects promoted to level 2, and the level 2 objects stay in level 2 itself, as this generation level is the highest level.
So what is the importance & use of the generations process? Its actually the priority the GC gives to objects while freeing objects from the heap. During every GC cycle, the objects in the Generation 0 are scanned first -> Followed by Generation 1 and then 2. This is because the generation 0 objects are usually short term objects, that need to be freed. The newer an object, the shorter its life is. The older an object, longer its life is.
This process also helps in categorizing the memory heap as to where the de-allocation needs to be done first and where next.
How to call COM components from .NET? What is interoperability?
COM components & .NET components have a different internal architecture. For both of them to communicate with each other, inter-operation feature is required, this feature is called interoperability. Enterprises that have written their business solutions using the old native COM technology need a way for re-using these components in the new .NET environment.
.NET components communicate with COM using RCW (Runtime Callable Wrapper (RCW).
To use a COM component,
* Right click the Project & click on Add References.
* Select the COM tab
* Select the COM component
Another way of using a COM component is using the tblimp.exe tool (Type Library Import).
Using the COM component directly in the code may be achieved by using System.Runtime.InteropServices namespace. This contains class TypeLib Converter which provides methods to convert COM classes and interface in to assembly metadata. If the COM component does not have a Type Library, then custome wrappers need to be created. Once the COM wrapper is created, it has to be registered in the registry.
How to call .NET component from COM?
In case a .NET component needs to be used in COM, we make use of COM Callable Wrapper (CCW). Following are the different approaches to implement it:
* Explicitly declare interfaces.
* The second way to create CCW using InteropServices attributes.Here interfaces are created automatically.
Following are different type of class attributes :
None-- No class interface is generated for the class.This is default setting when you do not specify anything.
AutoDispatch-- Interface that supports IDispatch is created for the class. However, no type information is produced.
AutoDual-- A dual interface is created for the class. Typeinfo is produced and made available in the type library.
In below source code we have used the third attribute.
Imports System.Runtime.InteropServices
<ClassInterfaceAttribute(ClassInterfaceType.AutoDual)> _
Public Class ClsCompliant
End Class
Other than class attributes defined up there are other attributes with which you can govern other part of assembly.Example “GuidAttribute” allows you to specify the GUID,”ComVisibleAttribute “ can be used to hide .NET types from COM etc .
What is .NET?
.NET - is the Microsoft Web services strategy to connect information, people, systems, and devices
through software. Integrated across the Microsoft platform, .NET technology provides the ability to quickly build, deploy, manage, and use connected, security-enhanced solutions with Web services. .NET-connected solutions enable businesses to integrate their systems more rapidly and in a more agile manner and help them realize the promise of information anytime, anywhere, on any device.
Which languages can be used to program in .NET?
Microsoft provides these languages for programming .NET - C#, VB.NET, JS.NET, C++.NET.
C++.NET may be used as a Managed Code Language by using managed extensions. This is done using an _gc postfix. A managed C++ class can inherit from VB.NET classes, C# classes, JS.NET classes. A managed class can inherit from only one class. .NET does'nt allow multiple inheritance in managed classes.
Any Language that has a compiler that can compile the language code to MSIL is a .NET compliant language.
Below is an alphabetical list of languages supported by .NET
APL - It is a language for describing procedures in the processing of information. It is a very powerful language. It may be used to describe mathematical procedures.
C++ - A widely known language. One of the oldest Object Oriented Languages, an advanced version of the C language. Microsoft has its own Visual C++ compiler that includes special tools and libraries for development on Windows platform. C++ is an object-oriented programming language
that is viewed by many as the best language for creating large-scale applications. C++ is a superset of the C language.
C# - Pronounced C Sharp. It is a complete Object-Oriented programming language from Microsoft built into the .NET Framework. First created in the late 1990’s was part of Microsoft’s whole .NET strategy.
COBOL - Expanded as Common Business Oriented Language. It is a widely used high level language for developing Business Applications.
Component Pascal - Its a Pascal derived programming language for development of programming components.
Eiffel - It is an Object-Oriented programming language which emphasizes the production of robust software.
Forth - It is both a programming language & a programming environment. It supports shell programming to a high level.
Fortran - Stands for Formula Translator. Its a high level programming language used for scientific computations. It supports plenty of compact notations.
Haskell - It is a standardized functional programming language with non-strict semantics, named after the logician Haskell Curry. It was created by a committee formed in the 1980s for the express purpose of defining such a language.The latest semi-official language standard is Haskell 98, intended to specify a minimal, portable version of the language for teaching and as a base for future extensions.
Java - It is an object-oriented programming language developed initially by James Gosling and colleagues at Sun Microsystems. The language, initially called Oak (named after the oak trees outside Gosling's office), was intended to replace C++, although the feature set better resembles that of Objective C. Java should not be confused with JavaScript, which shares only the name and a similar C-like syntax. Sun Microsystems currently maintains and updates Java regularly.
Microsoft JScript - A scripting language developed by Microsoft to enable Web page designers to design interactive sites. Although it shares many of the features and structures of the full Java language, it was developed independently. Jscript can interact with HTML source code, better enabling Web authors to spice up their sites with dynamic content.
Mercury - Mercury is a functional/logical programming language based on Prolog, but more useful for real-world programming.
Mondrian - It is a simple functional scripting language for Internet applications. It is a functional language specifically designed to inter-operate with other languages in an OO environment. Current versions of Mondrian run on .NET. Mondrian also supports ASP.NET, allowing you to embed functional language code in web pages along with C# code.
Oberon - It is a programming language very much like Modula-2 in syntax but with several interesting features. It's based on OOP concepts and also provides a Windows-based GUI.
Pascal - A high-level, highly structured, general-purpose programming language. Named after Blaise Pascal.
Perl - Stands for Practical Extraction and Report Language. It is a language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information.
Python - It is an interpreted, interactive, Object-Oriented programming language. Python combines remarkable power with very clear syntax. It has modules, classes, exceptions, very high level dynamic data types, and dynamic typing.
RPG - Stand for Report Program Generator. It is used for generation of reports from data files, including matching record and sub-total reports. RPG is one of the few languages created for punch card machines that is still in common use today.
Scheme - It is a statically scoped programming language. It was designed to have an exceptionally unambigous and simple semantics and few different ways to form expressions. A vast variety of programming paradigms, including imperative, functional, and message passing styles, find convenient expression in Scheme.
Smalltalk - It is a simple language that uses a simple sub set of human languages, nouns and verbs. Smalltalk was the first, and remains one of the few, pure object systems, which simply means that everything in a Smalltalk program is an object.
Standard ML - It is a safe, modular, strict, functional, polymorphic programming language with compile-time type checking and type inference, garbage collection, exception handling, immutable data types and updatable references, abstract data types, and parametric modules.
Microsoft Visual Basic - Most widely used language in the world today! Used for developing Windows based Applications, Windows Services, Remoting Applications, Web Services and Web Applications(using ASP.NET).
Which versions of .NET have been released so far?
The final version of the .NET 1.0 SDK & runtime were made publically available on 15 - Jan -2002. At the same time, the final version of Visual Studio.NET was made available to MSDN subscribers.
.NET 1.1 was released in April 2003, with bugs fixed. Visual Studio 2003 supports development of applications in version 1.1.
.NET 2.0 was launched in October 2005 for MSDN subscribers, and officially released in Nov 2005.
On - Jun - 2006, .NET 3.0 was launched. This version was earlier called WinFX. Visual Studio 2005 supports development of .NET 2.0 and .NET 3.0 applications. .NET 3 is comprised of the following:
Windows Communication Foundation
Windows Presentation Foundation
Windows Workflow Foundation
Windows Cardspace
Which tools can be used for .NET Development?
The .NET Framework SDK is free and includes command-line compilers for C++, C#, and VB.NET and various other utilities to aid development.
SharpDevelop is a free IDE for C# and VB.NET.
Microsoft Visual Studio Express editions are cut-down versions of Visual Studio, for hobbyist or novice developers and are available for FREE Download at Microsoft site. Note that .NET 2.0 Framework gets downloaded along with Visual Studio Express & All versions above Visual Studio Express. Download Visual Studio Express
There are different versions for C#, VB, web development etc. Microsoft Visual Studio Standard 2005 is around $300, or $200 for the upgrade.
Microsoft VIsual Studio Professional 2005 is around $800, or $550 for the upgrade. At the top end of the price range are the Microsoft Visual Studio Team Edition for Software Developers 2005 with MSDN Premium and Team Suite editions. Visual Web Developer Express is available as a free download.
Explain CLI, CIL, CTS, Metadata, CLS, IL and VES in .NET
CLI - Common Language Infrastructure. Microsoft has a piece of shared source, its the public implementation of ECMA Common Language Infrastructure. This shared code is code-named "Rotor". It has around 3 million lines of code. Those who are interested in development of a language that targets the .NET Framework, may extensively make use of this CLI. The following topics are covered in the Shared Source CLI :
* The CLI type system
* Component packing & assemblies
* Type Loading & JIT Compilation
* Managed code & Execution Engine (CLR)
* Description of Garbage Collection process & memory management
* The Platform Adaptation Layer (PAL): a portability layer for Win32®, Mac OS® X, and FreeBSD
Its been written by the Microsoft Team that has developed the .NET Framework.
Note: A compiled managed assembly is comprised of IL, Metadata and Manifest.
CIL Stands for Common Intermediate Language. Its actually a low level human readable language implementation of CLI. All .NET-aware languages compile the source ode to an intermediate language called Common Intermediate Language using the language specific compiler. It is also possible to build .NET assemblies directly using CIL using the ilasm.exe compiler. This compiler is shipped along with the .NET Framework 2.0 SDK. CIL is the only language that allows access to each aspect of the CTS. CIL is the definition of the fundamentals of the .NET framework.
CTS - stands for Common Type Specification. It is at the core of .NET Framework's cross-language integration, type safety, and high-performance code execution. It defines a common set of types that can be used with many different language syntaxes. Each language (C#, VB.NET, Managed C++, and so on) is free to define any syntax it wishes, but if that language is built on the CLR, it will use at least some of the types defined by the CTS.
Metadata - is code that describes the compiled IL. A .NET language compiler will generate the metadata and store this in the assembly containing the CIL. Metadata describes all class members and classes that are defined in the assembly, and the classes and class members that the current assembly will call from another assembly. The metadata for a method contains the complete description of the method, including the class (and the assembly that contains the class), the return type and all of the method parameters. When the CLR executes CIL it will check to make sure that the metadata of the called method is the same as the metadata that is stored in the calling method. This ensures that a method can only be called with exactly the correct number of parameters and exactly the correct parameter types.
CLS - Common Language Specification. A type that is CLS compliant, may be used across any .NET language. CLS is a set of language rules that defines language standards for a .NET language and types declared in it. While declaring a new type, if we make use of the [CLSCompliant] attribute, the type is forced to conform to the rules of CLS.
IL - Intermediate Language, is the compiled form of the .NET language source code. When .NET source code is compiled by the language specific compiler (say we compile C# code using csc.exe), it is compiled to a .NET binary, which is platform independent, and is called Intermediate Language code. The .NET binary also comprises of metadata.
Its important to note here that metadata describes the IL, whereas manifest describes the assembly.
VES - Virtual Execution System. The Virtual Execution System(VES) provides an environment for executing managed code. It provides direct support for a set of built-in data types, defines a hypothetical machine with an associated machine model and state, a set of control flow constructs, and an exception handling model. To a large extent, the purpose of the VES is to provide the support required to execute the Common Intermediate Language instruction set.
What is CLR in .NET?
Common Language Runtime - It is the implementation of CLI. The core runtime engine in the Microsoft .NET Framework for executing applications. The common language runtime supplies managed code with services such as cross-language integration, code access security, object lifetime management, resouce management, type safety, pre-emptive threading, metadata services (type reflection), and debugging and profiling support. The ASP.NET
Framework and Internet Explorer are examples of hosting CLR.
The CLR is a multi-language execution environment. There are currently over 15 compilers being built by Microsoft and other companies that produce code that will execute in the CLR.
The CLR is described as the "execution engine" of .NET. It's this CLR that manages the execution of programs. It provides the environment within which the programs run. The software version of .NET is actually the CLR version.
What is a Class Library in .NET?
Class library is the another major entity of the .NET Framework. This library gives the program access to runtime environment. The class library consists of lots of prewritten code that all the applications
created in .NET aware languages and Visual Studio .NET will use. The code for all the elements like forms, controls and the rest in VB .NET applications actually comes from the class library.
Explain Managed code, managed class and managed data in .NET
Managed Code - The .NET framework provides lots of core runtime services to the programs that run within it. For example - security & exception handling. Such a code has a minimum level of information. It has metadata associated with it. Such a code is called Managed Code. VB.NET, C#, JS.NET code is managed by default. In order to make C++ code managed, we make use of managed extensions, which is nothing but a postfix _gc after the class name.
Managed Data - Data that is allocated & freed by the .NET runtime's Garbage collector.
Managed Class - A class whose objects are managed by the CLR's garbage collector. In VC++.NET, classes are not managed. However, they can be managed using managed extensions. This is done using an _gc postfix. A managed C++ class can inherit from VB.NET classes, C# classes, JS.NET classes. A managed class can inherit from only one class. .NET doesn’t allow multiple inheritance in managed classes.
What is an assembly in .NET? What is ILDASM?
Assembly - An assembly may be an exe, a dll, an application having an entry point, or a library. It may consist of one or more files. It represents a group of resources, type definitions, and implementation of these types. They may contain references to other assemblies. These resources, types & references are compacted in a block of data called manifest. The manifest is a part of the assembly, which makes it self-describing. Assemblies also increase security of code in .NET. An assembly maybe shared(public) or private. The assembly, overall comprises of 3 entities: IL, Manifest, Metadata. Metadata describes IL, whereas Manifest describes the assembly.
An assembly may be created by building the class(the .vb or .cs file), thereby producing its DLL.
ILDASM - The contents of an assembly may be viewed using the ILDASM tool, that comes with the .NET SDK or the Visual Studio.NET. The ildasm.exe tool may also be used in the command line compiler.
What is Reflection in .NET?
Reflection - The process of getting the metadata from modules/assemblies. When .NET code is compiled, metadata about the types defined in the modules is produced. These modules are in turn packaged as assembled. The process of accessing this metadata in called Reflection.
The namespace System. Reflection contains classes that can be used for interrogating the types for a module/assembly. We use reflection for examining data type sizes for marshalling across process & machine boundaries.
Reflection is also used for:
1) To dynamically invoke methods (using System.Type.InvokeMember)
2) To dynamically create types at runtime (using System.Reflection.Emit.TypeBuilder).
What are the different types of assemblies in .NET?
An assembly may be Public or Private. A public assembly is also called a Shared Assembly.
Difference between Public and Private assembly
A Satellite Assembly - is an assembly that contains only resources, and no code. The resources are location specific. A satellite assembly is associated with a main assembly, the one that actually contains the code.
How to share an assembly? How to make it strongly named? What is SN.EXE?
In order to share an assembly with many applications, it must have a strong name.
In order to convert a private assembly to a shared assembly, i.e. to create a strongly named assembly, follow the steps below...
1) Create a strong key using the sn.exe tool. This is used to created a cryptographic key pair. The key pair that is generated by the Strong Name tool can be kept in a file or we can store it our your local machine's Crytographic Service Provider (CSP). For this, goto the .NET command interpreter, and type the following...
sn -k C:\samplekey.snk
This will create a strong key and save it to the location C:\samplekey.snk
2) If the key is stored in a file, just like we have done above, we use the attribute AssemblyKeyFileAttribute. This belongs to the namespace System.Reflection.AssemblyKeyFileAttribute.
If the key was in the CSP, we would make use of System.Reflection.AssemblyKeyNameAttribute.
Go to the assemblyinfo.vb file of your project. Open this file. Make the following changes in this file...
<assembly: assemblykeyfileattribute ("C:\samplekey.snk")>
We may write this in our code as well, like this...
Imports System.Reflection
<assembly: assemblykeyfileattribute("C:\samplekey.snk")>
Namespace Strong Name
Public class Sample
End Class
End Namespace
3) Build your project. Your assembly is now strongly named.
Installing the Shared assembly in GAC...
Go to .NET command interpreter, use the tool gacutil.exe
Type the following...
gaudily /i sampleclass.dll To uninstall it, use... actual /u sampleclass.dll Visual Studio.NET provides a GUI tool for viewing all shared assemblies in the GAC.
Folder of GAC: C:\Windows\Assembly OR C:\Winnt\Assembly
What is GAC?
A shared assembly has version constraints. It is stored in the Global Assembly Cache (GAC). GAC is a repository of shared assemblies maintained by the .NET runtime. The shared assemblies may be used by many applications. To make an assembly a shared assembly, it has to be strongly named.
To know more on how to share an assembly, create a strongly named assembly
GAC is located at C:\Windows\Assembly OR C:\Winnt\Assembly
Can garbage collector control the activities of a thread?
Garbage Collection - Garbage collection is a heap-management strategy where a run-time component takes responsibility for managing the lifetime of the memory used by objects. This concept is not new to .NET - Java and many other languages/runtimes have used garbage collection for some time. The garbage collector runs periodically. It runs through a list of objects that are currently being referenced by an application. All objects that it does not find during this search are ready to be destroyed (using the finalize method) and hence free the memory. However, the runtime gets notified of the object that has been destroyed, only in the next round of the garbage collector's periodic cycle.
In the class System.GC, there is a method called collect( ). This forces the garbage collector to collect all unreferenced objects immediately, thereby giving the developer some control over the garbage collector.
There is a concurrent setting that can be set through the applications's .config file. This specifies whether or not the garbage collector performs its activities on a specified thread or not.
We can view the performance monitor to view the activities of the garbage collector.
What is IDisposable interface in .NET?
IDisposable interface - We implement this interface to a class when we have to work with unmanaged types. For example, an IntPtr member representing an operating system's file handler is actually unmanaged, and in order to destroy it, we make use of the Dispose method by implementing the IDisposable interface. In this case, we override the Finalize method. Note that we make use of Dispose method on those objects which have an uncertain life period, and thus the garbage collector does not finalize them automatically.
We also make use of Dispose method while working with managed code, for example, an object os System.IO.FileStream may have an uncertain life, and in order to dispose it, we use the dispose method. Such types of objects are not accessed by the Garbage Collector's Finalize.
What is Serialization in .NET? Soap Formatter, Binary Formatter, XmlFormatter?
Serialization - The process of converting an object into a stream of bytes. This stream of bytes can be persisted. Federalization is an opposite process, which involves converting a stream of bytes into an object. Serialization is used usually during remoting (while transporting objects) and to persist file objects & database objects.
What is CAS in .NET?
Code Access Security (CAS) - CAS is the part of the .NET security model that determines whether or not code is allowed to run, and what resources it can use when it is running. For example, it is CAS that will prevent a .NET web applet from formatting your hard disk.
The CAS security policy revolves around two key concepts - code groups and permissions. Each .NET assembly is a member of a particular code group, and each code group is granted the permissions specified in a named permission set. For example, using the default security policy, a control downloaded from a web site belongs to the 'Zone - Internet' code group, which adheres to the permissions defined by the 'Internet' named permission set. (Naturally the 'Internet' named permission set represents a very restrictive range of permissions.) To view codegroups on our system, use the following command on .NET command interpretor... caspol -lg Note the hierarchy of code groups - the top of the hierarchy is the most general ('All code'), which is then sub-divided into several groups, each of which in turn can be sub-divided. Also note that (somewhat counter-intuitively) a sub-group can be associated with a more permissive permission set than its parent. If we want to trust a particular website giving it full rights to our system...Use caspol. For example, suppose we trust code from www.mydomain.com and we want it have full access to our system, but we want to keep the default restrictions for all other internet sites. To achieve this, we would add a new code group as a sub-group of the 'Zone - Internet' group,
like this: caspol -ag 1.3 -site www.mydomain.com FullTrust To change the permission, we use the -cg attribute.To turn off caspol, use caspol -s off
What is a class attribute in .NET?
Class Attributes - Is a kind of property attached with a class. It allows some data to be attached to a class or method. This data becomes part of the metadata for the class, and (like other class metadata) can be accessed via reflection. An example of a metadata attribute is [serializable], which can be attached to a class and means that instances of the class can be serialized.
What is a thread? How to use and create a thread in .NET?
Threads - When we want to run one or more instances of a method, we make use of threading. Suppose we have a method like this...
Private Sub OnGoingProcess()
Dim i As Integer = 1
Do While True
ListBox1.Items.Add("Repeatitions: " + i)
i += 1
Loop
End Sub
Dim t As Thread
t = New Thread(AddressOf Me.OnGoingProcess)
t.Start()
The AddressOf operator creates a delegate object to the BackgroundProcess method. A delegate within VB.NET is a type-safe, object-oriented function pointer. After the thread has been instantiated, you begin the execution of the code by calling the Start() method of the thread. After the thread is started, you have some control over the state of it by using methods of the Thread object. You can pause a thread's execution by calling the Thread.Sleep method. This method takes an integer value that determines how long the thread should sleep. If you wanted to slow down the addition of items to the listbox in the example above, place a call to the sleep method in this code:
Private Sub OnGoingProcess()
Dim i As Integer = 1
Do While True
ListBox1.Items.Add("Repeatitions: " + i)
i += 1
Thread.CurrentThread.Sleep(2000)
Loop
End Sub
You can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep (System.Threading.Timeout.Infinite). To interrupt this sleep you can call the Thread.Interrupt method. Similar to Sleep and Interrupt are Suspend and Resume. 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. Lastly, Thread.Abort stops a thread from executing. In our simple example, we would want to add another button on the form that allows us to stop the process. To do this all we would have to do is call the Thread.Abort method as follows:
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
t.Abort()
End Sub
This is where the power of multithreading can be seen. The UI seems responsive to the user because it is running in one thread and the background process is running in another thread. The cancel button immediately responds to the user's click event and processing stops. The next example shows a rather simple situation. Multithreading has many complications that we have to work out when we program. One issue that we will run into is passing data to and from the procedure passed to the constructor of the Thread class. That is to say, the procedure we want to kick off on another thread cannot be passed any parameters and we cannot return data from that procedure. This is because the procedure wepass to the thread constructor cannot have any parameters or return value. To get around this, wrap our procedure in a class where the parameters to the method are written as fields of the class. A simple example of this would be if we had a procedure that calculated the square of a number:
Function Square(ByVal Value As Double) As Double Return Value * Value End Function To make this procedure available to be used in a new thread we would wrap it in a class: Public Class SquareClass Public Value As Double Public Square As Double Public Sub CalcSquare() Square = Value * Value End Sub End Class
Use this code to start the CalcSquare procedure on a new thread. following code:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim oSquare As New SquareClass()
t = New Thread(AddressOf oSquare.CalcSquare)
oSquare.Value = 30
t.Start()
End Sub
Notice that after the thread is started, we do not inspect the square value of the class, because it is not guaranteed to have executed once you call the start method of the thread. There are a few ways to retrieve values back from another thread. The easiest way is to raise an event when the thread is complete. We will examine another method in the next section on thread synchronization
. The following code adds the event declarations to the SquareClass.
Public Class SquareClass
Public Value As Double
Public Square As Double
Public Event ThreadComplete(ByVal Square As Double)
Public Sub CalcSquare()
Square = Value * Value
RaiseEvent ThreadComplete(Square)
End Sub
End Class
Catching the events in the calling code has not changed much from VB6, you still declare the variables WithEvents and handle the event in a procedure. The part that has changed is that you declare that a procedure handles the event using the Handles keyword and not through the naming convention of Object_Event as in VB6. Dim WithEvents oSquare As SquareClass
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
oSquare = New SquareClass()
t = New Thread(AddressOf oSquare.CalcSquare)
oSquare.Value = 30
t.Start()
End Sub
Sub SquareEventHandler(ByVal Square As Double) _
Handles oSquare.ThreadComplete
MsgBox("The square is " & Square)
End Sub
The one thing to note with this method is that the procedure handling the event, in this case SquareEventHandler, will run within the thread that raised the event. It does not run within the thread from which the form is executing.
What is globalization in .NET?
Globalization refers to the process with which an application or software is designed and developed so as to make it run across all platforms and all sites with minimum or no modification to the software application. The software is very easy to customize so as to suit to the location-specific conditions and it is also capable of providing information based on the varied inputs and the location-specific operating system.
There are two processes in Globalization and they are customisation or localisation of the application and internationalizing the application codes so as to meet the standards of the local culture and other related matters.
What are Generations in Garbage Collector?
Generations in the Garbage Collector is a way of enhancing the garbage collection performance. In .NET, all resources are allocated space (memory) from the heap. Objects are automatically freed from the managed heap when they are no longer required by the application.
When objects are no longer being used by the application, the .NET runtime's garbage collector performs the task of collection, to determine the status of the objects. Necessary operations are performed to relieve the memory, in case the object is no longer in use. This is identified by the GC by examining the metadata of the object. For this, the GC has to know the location of the roots that represent the object. Roots are actually the location of the object on the managed heap. There are two types of memory references, strong & weak. When a root references an object, it is said to be a strong reference as the object is being pointed to by application code. The other type of object, that is not being referenced by the application code is called the weak reference, and this may be collected by the GC. However, this may still be accessed by the application code if required. But for the application to access the weakly referenced object, this has to be converted to a strong reference (and note that this has to be done before the GC collects the weakly referenced object).
So what are Generations in the GC? Its a feature of the GC that enhances its performance. There are 3 Generations...0,1,2.
Generation 0 - When an object is initialized, its in generation 0. These are new objects that have never been played around with by the GC. As and when more objects get created, the process of Garbage Collection is invoked by the CLR.
Generation 1 - The objects that survive the garbage collection process are considered to be in generation 1. These are the old objects.
Generation 2 - As more new objects get created and added to the memory, the new objects are added to generation 0, the generation 1 old objects become older, and so are considered to be in generation 2. Generation 2 is the highest level generation in the garbage collection process. Any further garbage collection process occuring causes the level 1 objects promoted to level 2, and the level 2 objects stay in level 2 itself, as this generation level is the highest level.
So what is the importance & use of the generations process? Its actually the priority the GC gives to objects while freeing objects from the heap. During every GC cycle, the objects in the Generation 0 are scanned first -> Followed by Generation 1 and then 2. This is because the generation 0 objects are usually short term objects, that need to be freed. The newer an object, the shorter its life is. The older an object, longer its life is.
This process also helps in categorizing the memory heap as to where the de-allocation needs to be done first and where next.
How to call COM components from .NET? What is interoperability?
COM components & .NET components have a different internal architecture. For both of them to communicate with each other, inter-operation feature is required, this feature is called interoperability. Enterprises that have written their business solutions using the old native COM technology need a way for re-using these components in the new .NET environment.
.NET components communicate with COM using RCW (Runtime Callable Wrapper (RCW).
To use a COM component,
* Right click the Project & click on Add References.
* Select the COM tab
* Select the COM component
Another way of using a COM component is using the tblimp.exe tool (Type Library Import).
Using the COM component directly in the code may be achieved by using System.Runtime.InteropServices namespace. This contains class TypeLib Converter which provides methods to convert COM classes and interface in to assembly metadata. If the COM component does not have a Type Library, then custome wrappers need to be created. Once the COM wrapper is created, it has to be registered in the registry.
How to call .NET component from COM?
In case a .NET component needs to be used in COM, we make use of COM Callable Wrapper (CCW). Following are the different approaches to implement it:
* Explicitly declare interfaces.
* The second way to create CCW using InteropServices attributes.Here interfaces are created automatically.
Following are different type of class attributes :
None-- No class interface is generated for the class.This is default setting when you do not specify anything.
AutoDispatch-- Interface that supports IDispatch is created for the class. However, no type information is produced.
AutoDual-- A dual interface is created for the class. Typeinfo is produced and made available in the type library.
In below source code we have used the third attribute.
Imports System.Runtime.InteropServices
<ClassInterfaceAttribute(ClassInterfaceType.AutoDual)> _
Public Class ClsCompliant
End Class
Other than class attributes defined up there are other attributes with which you can govern other part of assembly.Example “GuidAttribute” allows you to specify the GUID,”ComVisibleAttribute “ can be used to hide .NET types from COM etc .
0 comments:
Speak up your mind
Tell us what you're thinking... !