Tuesday, July 22, 2014

DotNet Framework Tutorial

DotNet Framework Tutorial | .NET Framework Concepts | .NET Framework Fundamentals
.NET is a platform for developing applications for the internet and standalone applications.

.NET Framework:

The .NET Framework is a software framework developed by Microsoft that runs primarily on Microsoft Windows. It simplifies the application development in the highly distributed environment of the internet.

The .NET Framework is made up of two major components: The common language runtime (CLR) and the framework class library (FCL). The CLR is the foundation of the .NET Framework and provides various services that applications can use. The FCL is a collection of over 7000+ types that cater to all the services, and data structures that applications will ever need.
The following diagram shows the .NET Framework, its hierarchy and the associated toolset.
At the base of the diagram, you see the operating system which can be any platform (windows XP/7/8/etc..). The Common Language Runtime (CLR) abstracts the underlying operating system from you code. It means that your code has to run using the services provided by the CLR and we get a new name called managed code. The CLR provides its services to applications by providing a standard set of library classes that abstract all the tasks that you will ever need. These classes are called as the Base Class Libraries. On top of this, other development platforms and applications are built (like ASP.NET, ADO.NET and so on). Language compilers that need to generate code for the CLR must adhere to a common set of specifications as laid down by the Common Language Specification (CLS). Above this, you have all the popular .NET languages.
Visual Studio .NET, then is the "glue" that helps your generate .NET applications and provides an IDE that is excellent for collaborative development.

Common Language Runtime:
The CLR is the platform on which applications are hosted and executed. The CLR also provides a set of services that applications can use to access various resources (like arrays, collections, operating system folders etc). Since this runtime "manages" the execution of your code, code that works on the CLR is called as managed code. Any other code, not managed by CLR, is called unmanaged code.
Compilers and tools expose the CLR's functionality and enable you to write code that benefits from this managed execution environment. To enable the runtime to provide services to managed code, language compilers must also emit metadata that describes the types that we develop in .NET. This metadata is stored along with the type file and makes it "self-describing". Using this information, the runtime automatically handles object layout and manages references to objects, releasing them when they are no longer being used.
When compilers emit code to run on the CLR, they do not emit machine language code. Rather, an intermediate language code is used called Microsoft Intermediate Language (MSIL). MSIL is like an object-oriented version of assembly language output by .NET compilers—C#, VB .NET, etc. and is platform independent. It has a rich set of instructions that enable efficient representation of the code. When a code starts to execute, a process knowing as Just in Time Compilation (JIT) converts the MSIL code into the native processor instructions of the platform, which is then executed. This is shown in the following diagram:



The Code Execution Process involves the following two stages:
  1. Compiler time process.
  2. Runtime process.
1. Compiler time process
 
The .Net framework has one or more language compliers, such as Visual Basic, C#, Visual C++,  JScript, or one of many third-party compilers such as an Eiffel, Perl, or COBOL compiler.
    Any one of the compilers translate your source code into Microsoft Intermediate Language (MSIL) code.
    For example, if you are using the C# programming language to develop an application, when you compile the application, the C# language compiler will convert your source code into Microsoft Intermediate Language (MSIL) code.
    In short, VB.NET, C# and other language compilers generate MSIL code. (In other words, compiling translates your source code into MSIL and generates the required metadata.)
    Currently "Microsoft Intermediate Language" (MSIL) code is also known as "Intermediate Language" (IL) Code or "Common Intermediate Language" (CIL) Code.

    SOURCE CODE -----.NET COMLIPER------> BYTE CODE (MSIL + META DATA)

2. Runtime process.

    The Common Language Runtime (CLR) includes a JIT compiler for converting MSIL to native code.
    The JIT Compiler in CLR converts the MSIL code into native machine code that is then executed by the OS.
    During the runtime of a program the "Just in Time" (JIT) compiler of the Common Language Runtime (CLR) uses the Metadata and converts Microsoft Intermediate Language (MSIL) into native code.

    BYTE CODE (MSIL + META DATA) ----- Just-In-Time (JIT) compiler------> NATIVE CODE

Notes:

When you compile a C# application or any application written in a CLS-compliant language, the application is compiled into MSIL. This MSIL is then further compiled into native CPU instructions when the application is executed for the first time by the CLR. (Actually, only the called functions are compiled the first time they are invoked.)

    You write source code in C#.
    You then compile it using the C# compiler (csc.exe) into an EXE.
    The C# compiler outputs the MSIL code and a manifest into a read-only part of the EXE that has a standard PE (Win32-portable executable) header.
    When the compiler creates the output, it also imports a function named "_CorExeMain" from the .NET runtime.
    When the application is executed, the operating system loads the PE, as well as any dependent dynamic-link libraries (DLLs), such as the one that exports the "_CorExeMain" function (mscoree.dll), just as it does with any valid PE.
    However, since the operating system obviously can't execute the MSIL code, the entry point is just a small stub that jumps to the "_CorExeMain" function in mscoree.dll.
    The "_CorExeMain" function starts the execution of the MSIL code that was placed in the PE.
    Since MSIL code cannot be executed directly, because it's not in a machine-executable format, the CLR compiles the MSIL using the Just-In-Time (JIT) compiler (or JITter) into native CPU instructions as it processes the MSIL. The JIT compiling occurs only as methods in the program are called. The compiled executable code is cached on the machine and is recompiled only if there's some change to the source code.
 

Conclusion:
·         CLR is a runtime environment provided by .NET framework. 
·         Developers write code in either C# or VB.NET.
·         .NET compilers convert this high level code into Microsoft Intermediate Language (MSIL). At runtime JIT compiler converts the MSIL into native code specific to the OS. CLR runs MSIL. 
·         CLR provides memory management, exceptional handling, security etc to the .NET code. 


Base Class Library
 
The .NET framework provides a set of base class libraries which provide functions and features which can be used with any programming language which implements .NET, such as Visual Basic, C#, Visual C++, etc. 
The base class library contains standard programming features such as Collections, XML, DataType definitions, IO ( for reading and writing to files), Reflection and Globalization to name a few. All of which are contained in the System namespace. As well, it contains some non-standard features such as LINQ, ADO.NET (for database interactions), drawing capabilities, forms and web support.
 
The below table provides a list each class of the base class library and a brief description of what they provide.



Base Class Library Namespace
Brief Description
System
Contains the fundamentals for programming such as the data types, console, match and arrays, etc.
System.CodeDom
Supports the creation of code at runtime and the ability to run it.
System.Collections
Contains Lists, stacks, hashtables and dictionaries
System.ComponentModel
Provides licensing, controls and type conversion capabilities
System.Configuration
Used for reading and writing program configuration data
System.Data
Is the namespace for ADO.NET
System.Deployment
Upgrading capabilities via ClickOnce
System.Diagnostics
Provides tracing, logging, performance counters, etc. functionality
System.DirectoryServices
Is the namespace used to access the Active Directory
System.Drawing
Contains the GDI+ functionality for graphics support
System.EnterpriseServices
Used when working with COM+ from .NET
System.Globalization
Supports the localization of custom programs
System.IO
Provides connection to file system and the reading and writing to data streams such as files
System.Linq
Interface to LINQ providers and the execution of LINQ queries
System.Linq.Expressions
Namespace which contains delegates and lambda expressions
System.Management
Provides access to system information such as CPU utilization, storage space, etc.
System.Media
Contains methods to play sounds
System.Messaging
Used when message queues are required within an application, superseded by WCF
System.Net
Provides access to network protocols such as SSL, HTTP, SMTP and FTP
System.Reflection
Ability to read, create and invoke class information.
System.Resources
Used when localizing a program in relation to language support on web or form controls
System.Runtime
Contains functionality which allows the management of runtime behavior.
System.Security
Provides hashing and the ability to create custom security systems using policies and permissions.
System.ServiceProcess
Used when a windows service is required
System.Text
Provides the StringBuilder class, plus regular expression capabilities
System.Threading
Contains methods to manage the creation, synchronization and pooling of program threads
System.Timers
Provides the ability to raise events or take an action within a given timer period.
System.Transactions
Contains methods for the management of transactions
System.Web
Namespace for ASP.NET capabilities such as Web Services and browser communication.
System.Windows.Forms
Namespace containing the interface into the Windows API for the creation of Windows Forms programs.
System.Xml
Provides the methods for reading, writing, searching and changing XML documents and entities.


ADO.NET and XML
ADO.NET is also a part of .NET framework.
ADO.NET can be used as a powerful XML middleware.
A main feature of ADO.NET is its disconnected recordsets, known as DataSet.
Native support for XML is another principal feature for ADO.NET. In native mode, record tables are stored as XML documents where schema and data are treated as distinct and replaceable elements.

Common Language Specification
Common Language specification (CLS) is a set of rules that are to be followed by a language in order to be .NET compliant. This facilitates cross-language integration. Programs written in one .NET language can interoperate with programs written in another .NET language.
For example a VB.NET class can inherit from a C#.NET class and override its virtual methods.
Common Type System (CTS) describes how types are declared, used and managed. CTS facilitate cross-language integration, type safety, and high performance code execution.

The CTS also defines the rules that ensure that the data types of objects written in various languages are able to interact with each other.



Practice Tests:
 
1.The CLR provides the ______________ that defines the data types that are used by all .NET languages.
  a). MSIL         b). CTS
  c). BCL           d). DL

2. The _____________ manages the execution of .NET programs by co-ordinating essential functions such as memory management, code execution, security  and other services.
  a). CTS           b). CLR
  c). BCL           d). IL

3. Find any errors in the following Bank Account Constructor:
   Public int Bank Account()
            {
              balance =  0;
            }
   a) Name                   b) No Parameters
   c) Return Type         d) No Error

4. Class String Builder exists in which of the following namespaces:
   a) System.Strings                 b) System.Text
   c) System.Chars                   d) System

5. Which of the following statements are true?
   a) You need to explicitly open connection to execute DataReader
   b) You need to explicitly open connection to fill DataSet
   a) 1               b) 2
   c) 1 and 2     c) None

6. Which of these string definitions will prevent escaping on backslashes in c#?
   a) string s = #"Test string";
   b) string s = ."Test string";
   C) string s = @"Test string";
   d) string s = &"Test string";

7. To get metadata information about types you can use mechanism called _____
  a) Serialization          b) Reflection
  c) Compilation          d) None

8. .NET code compiled into _______
  a) Byte Code             b) MSIL
  c) C#                          d) None

9. .NET DLLS are compiled into ______
  a) Machine language (Native Language) b) EXE
  c) MSIL                                              d) None

10. The unique feature of .NET is the ________ support that it provides
  a) Multi-language

11. A program in .NET is first compiled by the language specific compiler into ______
  a) Common Language          b) Runtim Language
  c) Intermediate Language d) Visual Basic

12. Which of the following statements are NOT true about the role of the CLR?
  a) Manages Memory
  b) Compiles program into machine code
  c) Compiles program to a .exe file
  d) Compile once and run on any CPU & OS that supports the runtime.

13. Which of the following statements are TRUE about the .NET CLR?
    1. It provides a language-neutral development & execution environment.
    2. It ensures that an application would not be able to access memory that it is not authorized to access.
    3. It provides services to run "managed" applications.
    4. The resources are garbage collected.
    5. It provides services to run "unmanaged" applications.
    a).    Only 1 and 2
    b).    Only 1, 2 and 4
     c).   1, 2, 3, 4
     d).   Only 4 and 5
    e).    Only 3 and 4

14. Which of the following are valid .NET CLR JIT performance counters?
    1. Total memory used for JIT compilation
    2. Average memory used for JIT compilation
    3. Number of methods that failed to compile with the standard JIT
    4. Percentage of processor time spent performing JIT compilation
    5. Percentage of memory currently dedicated for JIT compilation
    A.     1, 5                  B.         3, 4
    C.     1, 2                  D.         4, 5

15.  Which of the following statements is correct about Managed Code?
A.         Managed code is the code that is compiled by the JIT compilers.
B.         Managed code is the code where resources are Garbage Collected.
C.         Managed code is the code that runs on top of Windows.
D.         Managed code is the code that is written to target the services of the CLR.
E.         Managed code is the code that can run on top of Linux.

16. Which of the following utilities can be used to compile managed assemblies into processor-specific native code?
A.         gacutil               B.         ngen
C.         sn                     D.         dumpbin          E.         ildasm


Q:
What is the difference between nGen and JIT compiler?

A:  Basically NGen allows you pre-JIT and cache the assembly on the local machine. This allows for a faster startup and sometimes execution.

(The Native Image Generator (Ngen.exe) is a tool that improves the performance of managed applications. Ngen.exe creates native images, which are files containing compiled processor-specific machine code, and installs them into the native image cache on the local computer. The runtime can use native images from the cache instead of using the just-in-time (JIT) compiler to compile the original assembly.)

17.  Which of the following are NOT true about .NET Framework?
1.      It provides a consistent object-oriented programming environment whether object code is stored and executed locally, executed locally but Internet-distributed, or executed remotely.
2.      It provides a code-execution environment that minimizes software deployment and versioning conflicts.
3.      It provides a code-execution environment that promotes safe execution of code, including code created by an unknown or semi-trusted third party.
4.      It provides different programming models for Windows-based applications and Web-based applications.
5.      It provides an event driven programming model for building Windows Device Drivers.
A.     1, 2
B.     2, 4
C.     4, 5
D.     1, 2, 4

18. Which of the following components of the .NET framework provide an extensible set of classes that can be used by any .NET compliant programming language?
A.         .NET class libraries
B.         Common Language Runtime
C.         Common Language Infrastructure
D.         Component Object Model
E.         Common Type System

19.  Which of the following jobs are NOT performed by Garbage Collector?
1.        Freeing memory on the stack.
2.         Avoiding memory leaks.
3.          Freeing memory occupied by unreferenced objects.
4.          Closing unclosed database collections.
5.          Closing unclosed files.
A.   1, 2, 3              B. 3, 5
C.   1, 4, 5              D. 3, 4

20. Which of the following .NET components can be used to remove unused references from the managed heap?
     A.    Common Language Infrastructure
    B.     CLR
    C.     Garbage Collector
    D.     Class Loader
    E.     CTS

21. Which of the following statements correctly define .NET Framework?
     A.    It is an environment for developing, building, deploying and executing Desktop Applications, Web Applications and Web Services.
     B.    It is an environment for developing, building, deploying and executing only Web Applications.
     C.    It is an environment for developing, building, deploying and executing Distributed Applications.
     D.    It is an environment for developing, building, deploying and executing Web Services.
     E.    It is an environment for development and execution of Windows applications.

22. Which of the following constitutes the .NET Framework?

  • 1.      ASP.NET Applications
  • 2.      CLR
  • 3.      Framework Class Library
  • 4.      WinForm Applications
  • 5.      Windows Services

A.   1, 2                  B.  2, 3
C.   3, 4                  D.  2, 5










3 comments:

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    c-net training in electronic city

    ReplyDelete
  2. Thanks a lot for sharing such a good source with all, i appreciate your efforts taken for the same. I found this worth sharing and must share this with all.




    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery





    ReplyDelete