Professional Documents
Culture Documents
NET Concepts
Chapter - 1
Objectives
Understanding the previous state of
affairs The .NET Solution Building blocks of .NET Platform
CLR, CTS, and CLS
As a COM Programmer
Use of Java front-to-back during development cycle No language freedom! Pure Java is not suitable for graphic intensive problems (E.g. 3D game) No cross-language integration
Complex creation of COM types Active Template Library (ATL) Forced to contend with brittle/fragile registration entries Deployment issues
6
.NET Solution
Full interoperability with existing Win32 Code Complete and total language integration
.NET Framework
VB C++ C# JScript J# Common Language Specification Visual Studio.NET ASP.NET Web Forms Web Services Windows Forms
CLR
CLR sits on top of OS (same as JVM of Java) CLR loads modules containing executables and executes them Code may be managed or unmanaged Managed code consists of instructions in pseudo random code called CIL (Common Intermediate Language). CIL instructions are JIT compiled into native machine code at runtime JIT compiled methods reside in cache until the applications life time Advantages of managed code: type safety, memory management, and code verification security CLR can translate code from C#, J#, C, C++, VB, and Jscript into CIL. CLR doesnt launch a new process for every application. It launches one process and hosts individual applications in application domains
10
XML/SOAP
C#
Almost same as Java No pointers required Automatic memory management (No delete) Enumeration, class, structure, etc. Operator overloading allowed Interface-based programming techniques Assign characteristics to types (same as COM IDL) C# can produce code that can run only on .NET environment (unlike COM server or Win32 API)
12
Understanding Assemblies
Windows applications have dependencies
on one or more DLLs These DLLs may contain COM classes registered in System registry When these components are updated, applications may break 'DLL hell' Solution: .NET Assemblies C# .NET compiler doesn't generate machine code. It is compiled into "assembly"
13
Understanding Assemblies
An assembly is a logical collection of one or
more EXE or DLL files containing an application code & resources .NET binaries not described using com libraries & not registered into system registry .NET binaries contain platform agnostic intermediate language (IL) & type metada
14
Assembly
C# source code
IL Assembly
Intermediate Language (IL/CIL): Same as first pass of compiler. It can't be executed (it is not in binary format) Metadata Describes the assembly contents [characteristics] No need for component registry Each assembly includes information about references to other assemblies E.g. If you have a class called Car in a given assembly, the type metadata describes Car's base class, which interfaces are implemented by Car, description of members of Car. It is always present & is automatically generated by a given .NET aware compiler
15
Assembly
When CLR loads your application, it examines your
program's metadata to know which external assemblies are required for execution Private assemblies Used by single application Is not shared Most preferred method Shared assemblies Intended for multiple applications Manifest Assemblies themselves are described using metadata manifest The metadata of assemblies: version, list of externally defined assemblies, etc.
16
18
Example of CIL
CIL sits above a specific compiler (C#, J#, etc.) The associated compiler emits CIL instructions } using System; namespace Calculator { public class Calc { public int Add(int x, int y) { return x + y; } }
19
21
22
CIL to Execution
Desktop JIT Server
CIL
Pocket PC
into corresponding machine code and cache it. This is useful for not recompiling, if the same method is called again. Eg: Print()
23
25
assemblies required by the current assembly to function correctly, the assembly version, copyright info. Etc Eg: Csharpcalculator. Exe manifest
26
Manifest
External Assembly
// .z\V.4..
} .module ConsoleApplication1.exe // MVID: {51BE4F31-CBD0-4AE6-BC9D-F9A4976795FD} .imagebase 0x00400000 .subsystem 0x00000003 .file alignment 4096 .corflags 0x00000001 // Image base: 0x070b0000
27
properties, events, etc. Support for abstract members that define a polymorphic interface for derived classes Multiple inheritance is not allowed
29
30
Same as C/C++ Derived from a common base class System.ValueType Structures are suited for modeling geometric & mathematical data. C# use struct keyword struct Point {// structures can contain fields public int xpos, ypos; // structures can contain parameterized constructors public point (int x, int y) { xpos = x; ypos = y;} // structure may define methods public void Display() { console. WriteLine(({0},{1}, xpos, ypos); } }
C# Type
Byte sbyte short int long ulong float double object string bool
36
(CLS)
and complete set of features a given .NET aware compiler must support C# uses + for concatenation whereas VB .NET uses & C# allows operator overloading but VB .NET does not! The void functions may differ in syntax:
' VB .NET // C#
(CLS)
that can be hosted by CLR The rules serve as a guide to thirdparty compiler designers & library builders CLS is the subset of CLT
38
CLS Compliance
C# Type
byte sbyte short int long ulong float double object string char bool
CLS Compliance
Yes No Yes Yes Yes No Yes Yes Yes Yes Yes Yes
39
Example
public class Calc { // CLS compliant public int Add(int x, int y) { return x + y; }
40
CLR
.NET Namespace
MFC, Java, VB 6.0 have predefined set of
classes; C# doesn't
libraries, .NET platform use namespace concept eg. System.IO makes use of the same namespaces and same types as C#
Example in C#
System Namespace
using System; public Class MyApp { public static void Main() Console class in System Namespace { Console.WriteLine("Hello World"); } }
45
Example in VB .NET
Imports System Public Module MyApp Sub Main() Console.WriteLine("Hello World") End Sub End Module
46
file IO, buffering, etc. GDI+ primitives, bitmaps, fonts, icons, etc.
System.Threading
Threads
48
50
// Not listing System.Drawing namespace using System; Class Myapp() { pubplic void Display logo() { // use fully qualified name Syatem.Drawing.Bitmap Companylogo = new Syatem.Drawing.bitmap(20.20); .. } } using helps to create short hand notation for specifying a types fully qualified name
51
notation for specifying a types fully qualified name Referencing external assemblies
Majority of the .NET framework assemblies are located under specific directory called Global Assembly Cache (GAL) On windows machine, this can be located under %windir%\Assembly
52
Using ildasm.exe
Intermediate Language Disassembler
utility It allows to load up any .NET assembly & investigate its contents, including associated manifest, CIL & type metadata ildasm.exe installed under c:\program files\ microsoft visual studio8\ SDK\v2.0\bin
53
Using ildasm.exe
Viewing CIL code double click the
main() method of perticular application class Viewing type metadata press ctrl+M to view currently loaded assembly Viewing assembly metadata double click on MANIFEST icon
54
Questions
What is .net Framework? With a neat diagram
explain the building blocks of .net platform? Explain with neat diagram the workflow that takes place b/w your source code, .net execution engine? What are the key features of c#? Write short notes on CIL & Assembly manifest What is CLR? What are the features of CLR What is CTS? List out some Intrinsic CTS datatypes? Explain the functions of .NET runtime (CLR) Explain about .net namespaces?
56
Questions
Write short notes on: .net binaries & .net
type metadata Explain briefly about ILDASM? Briefly discuss the state of affairs that eventually led to the .NET platform. What is the .NET solution and what C# brings to the table. Write the difference b/w single-file & multifile assemblies. What are namespaces? List and explain the purpose of at least five namespaces.
57
End of Chapter 1
58