Framework Questions
Framework Questions
Framework Questions
1) 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. Reference
Microsoft provides these languages for programming .NET - C#, VB.NET, JS.NET, C+
+.NET.
Any Language that has a compiler that can compile the language code to MSIL is a .NET
compliant language.
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:
The next version of Visual Studio, code named Orcas, Beta 1 has been released as a Beta
Version. The .NET Framework 3.5 Beta is also available as a download.
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.
The next version of Visual Studio, code named Orcas, Beta 1 has been released as a Beta
Version and is available for download. Download Orcas
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 interesed 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 :
Its been written by the Microsoft Team that has developed the .NET Framework.
CIL Stands for Common Intermediate Language. Its actually a low level human readable
language implementation of CLI. All .NET-aware languages compile the source oode to
an intermediate language called Common Intermediate Language using the language
specific compiler. It is also possible to build .NET assemblies direclty 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.
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 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.
When the .NET program is compiled, the output of the compiler is not an executable file
but a file that contains a special type of code called the Microsoft Intermediate
Language (MSIL, now called CIL, Common Intermediate Language). This MSIL
defines a set of portable instructions that are independent of any specific CPU. It's the job
of the CLR to translate this Intermediate code into a executable code when the program is
executed making the program to run in any environment for which the CLR is
implemented. And that's how the .NET Framework achieves Portability. This MSIL is
turned into executable code using a JIT (Just In Time) complier. The process goes like
this, when .NET programs are executed, the CLR activates the JIT complier. The JIT
complier converts MSIL into native code on a demand basis as each part of the program
is needed. Thus the program executes as a native code even though it is compiled into
MSIL making the program to run as fast as it would if it is compiled to native code but
achieves the portability benefits of MSIL.
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 collecter.
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
extentions. 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.
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.
Code in class libraries may be shared & reused. One of the core . NET libraries is
mscorlib.dll. .NET language compilers reference this library automatically as it contains
core types. A class library, contains types, that may be used by external applications. A
class library may be a DLL or an EXE. Note that the .NET class libraries, even though
have a same extension as the old COM Win32 binaries, yet they are very different
internally.
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.
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 assemblied. 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.
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.
12) 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.
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.
Go to the assemblyinfo.vb file of your project. Open this file. Make the following
changes in this file...
<assembly: assemblykeyfileattribute("C:\samplekey.snk")>
Imports System.Reflection
<assembly: assemblykeyfileattribute("C:\samplekey.snk")>
Namespace StrongName
Public class Sample
End Class
End Namespace
3) Build your project. Your assembly is now strongly named.
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 Click
Here
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 gcConcurrent 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.
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 Finalizer.
Serialization - The process of converting an object into a stream of bytes. This stream of
bytes can be persisted. Deserialization 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 objecst & database objects.
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
18) 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:
Use this code to start the CalcSquare procedure on a new thread. following code:
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
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.
[Serializable]
Public Class SampleClass
( ... )
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.
In internationalization process the application code base is same and the efforts will be on
jobs such as translating, storing, retrieving and to make the application user friendly for
the selected locale. In any given place the culture and the language will always be
different and besides this you should also take into account the other factors such as time
zone, normal date pattern usage, cultural and language environments, currencies,
telephone numbers, and so many other factors that are specific to the locale. In
globalization the process of internationalization enables us to remove from the code base
and the presentation layer all the contents and make you to use only a single presentation
layer and single code base with a common contents that can suit any culture. The
internationalization process will aid us to keep all the contents in a common place with an
idea of making it easily accessible by the programme codes and the results can easily be
populated all over presentation layer and the application with ease and efficiently.
Moreover, the internationalization process also enables you to store the contents and all
the collected inputs from the user in a user friendly format and in a highly secured
manner without compromising any standards pertaining to the local culture. The
internationalization process is one step before any attempt for localising the application to
suit to the local needs.
With the help of the localization process of globalization, you can make your application
adaptable to the various location specific conditions and it will be easy for you to
translate and re-format our application to suit to our new location and that too without
changing any of the codes. Further, we can make use of the process for rectifying any of
the reported bugs and for fine tuning the application for running efficiently. The
globalization process also makes use of the locally prevailing information on culture
where the software or the application is to be installed and maintained. The locational
details and the language used in that particular area constitute to culture information and
for working with any culture based information the namespace concept is utilised and the
SystemGlobalization, SystemResources and SystemThreading are the available
namespaces in .NET Framework.
The SystemResources namespace constitutes interfaces and classes that are very helpful
for developers and maintenance experts in creating, storing, retrieving, and managing
various resources used in the application that are culture and location-specific.
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.
.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.
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.
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