0% found this document useful (0 votes)
22 views39 pages

1 (1) .Philosophy of

The document provides an overview of C# programming and .NET concepts, detailing the role of .NET assemblies, the Common Intermediate Language (CIL), and Just-In-Time (JIT) compilation. It outlines the learning objectives for students, including the use of Visual Studio .NET IDE and the operation of the Command Line Compiler (CSC.EXE). Additionally, it explains the building blocks of the .NET framework, including CLR, CTS, and CLS, along with the benefits of .NET over previous programming paradigms.

Uploaded by

lalita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views39 pages

1 (1) .Philosophy of

The document provides an overview of C# programming and .NET concepts, detailing the role of .NET assemblies, the Common Intermediate Language (CIL), and Just-In-Time (JIT) compilation. It outlines the learning objectives for students, including the use of Visual Studio .NET IDE and the operation of the Command Line Compiler (CSC.EXE). Additionally, it explains the building blocks of the .NET framework, including CLR, CTS, and CLS, along with the benefits of .NET over previous programming paradigms.

Uploaded by

lalita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

C# Programming and

.NET Concepts
Books for Reference
C# and the .NET Platform (2 Ed)
By Andrew Troelsen
Dreamtech Publications
Microsoft Visual C# .NET
By Mickey Williams
Microsoft Press

2
Chapter - 1

The Philosophy of .NET

3
Learning Objectives

At the end of this chapter student should be able to:


 Describe the role of the .NET assembly and the various development
utilities that ship with the .NET SDK. (L2)
 Differentiate the .NET related atoms such as assemblies, the Common
Intermediate Language (CIL) and Just in Time (JIT) compilation.(L3)
 Illustrate the relationship between various aspects of the .NET
framework such as the Common Language Runtime (CLR), Common
Type System (CTS) and the Common Language Specification (CLS).(L2)
 Demonstrate how C# source code files are compiled and debugged
using various approaches. (L3)
 Operate Command Line Compiler (CSC.EXE) to compile, debug, the C#
source code and to examine the Command Line Flags. (L3)
 Use Visual Studio .NET IDE for developing C# applications. (L3)

4
Lesson Schedule

Class No. Portion covered per hour


 The Building Block of the .NET Platform (CLR, CTS and
CLS), .NET Base Class Libraries, .NET Binaries (aka
Assemblies).
 The Role of the Common Intermediate Language, .NET Type
Metadata, Assembly Manifest, Compiling CIL to Platform –
Specific Instructions.
 Understanding the Common Type System, Intrinsic CTS Data
Types, Understanding the Common Languages Specification.
 Understanding the Common Language Runtime A tour of
the .NET Namespaces, Increasing Your Namespace
Nomenclature, Deploying the .NET Runtime.
 Building C # Application using csc.exe and Visual Studio .NET
IDE

5
Understanding the previous state
of affairs
 As a C/Win32 API Programmer
 It is complex
 C is a short/abrupt language
 Manual memory management, ugly pointer arithmetic,
ugly syntactic constructs
 Not a OO language
 As a C++/MFC Programmer
 Root is C
 C++ with MFC is still complex and error-prone
 As a VB 6 Programmer
 Not a complete OOP (“Object-aware”) – Why?
 Doesn’t support inheritance
 No multithreading
 No parameterized Classes
 Low-level API calls are complex

6
Previous state of affairs…
As a Java/J2EE 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
As a COM Programmer
Complex creation of COM types
Active Template Library (ATL)
Forced to contend with brittle/fragile registration
entries
Deployment issues

7
.NET Solution
 Full interoperability with existing Win32 Code
 Existing COM binaries can interoperate with .NET binaries
 Complete and total language integration
 Supports cross-language inheritance, exception handling, and
debugging
 Common runtime engine shared by all .NET aware
languages
 A base class library
 Good object model used by all .NET aware languages
 No more COM plumbing!
 No IClassFactory, IUnKnown, IDispatch, etc.
 Truly simplified deployment model
 No need to register a binary unit into the system registry
 Allows multiple versions of same *.dll

8
.NET Framework
VB C++ C# JScript J#

Common Language Specification

Visual Studio.NET
ASP.NET Windows
Web Forms Web Services Forms

ADO.NET and XML

Base Class Library

Common Language Runtime

Operating System

9
Building Blocks of .NET
CLR (Common Language Runtime)
To locate, load, and manage .NET types
Automatic memory management, language
integration, and type safety
CTS (Common Type System)
Describes all possible data types and programming
constructs supported by the runtime
CLS (Common Language Specification)
A set of rules that defines a subset of types and
specifications

10
CLR (Common Language Runtime)
 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 application’s
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 doesn’t launch a new process for every application. It
launches one process and hosts individual applications in
application domains

11
Base Class Libraries
 Encapsulates various primitives like: threads, file
IO, graphical rendering, and other interaction with
HW devices
 It also provides: database manipulation, XML
integration, Web-enabled front-end.

Data Access GUI Base Class Libraries

Threading File IO XML/SOAP

Common Language Runtime

CTS CLS
12
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)
13
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"

14
Assembly
Metadata

IL
C# source code + C# .NET Compiler =
Assembly

15
Assembly
Metadata
C# source code + C# .NET Compiler = IL
Assembly

Metadata
Perl source code + Perl .NET Compiler =
IL
Assembly

= Metadata
COBOLsource code COBOL.NET Compiler
+
IL
Assembly

16
 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
 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.

17
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
 Global Assembly Cache
 Manifest
 The metadata of assemblies: version, list of externally
defined assemblies, etc.

18
Single-File and Multifile
Assemblies
 Single file assemblies
 single binary file
 CIL, metadata and associated manifest

 Multifile assemblies-
 numerous . NET binaries
 Primary module must contain assembly manifest
 Other related modules contain module level manifest, CIL
and type metadata
Assembly is a logical grouping of one or more related
modules that are intended to be initially deployed
and versioned as single unit
Example of CIL
 CIL sits above a specific compiler (C#, public class Calc
J#, etc.) {
 The associated compiler emits CIL public int Add(int x, int y)
instructions { return x + y; }
}
using System; }
namespace Calculator
{

public class CalcApp  All .NET aware languages


{ emit same CIL instructions

public static void Main(string[]


args)
{
Calc c = new Calc();
int ans = c.Add(10, 84);
Console.WriteLine(ans);
Console.ReadLine();
}
20 }
Program in VB.NET
Imports System
Namespace ConsoleApplication7
Module CalcApp

Sub Main()
Dim ans As Integer
Dim c As New Calc
ans = c.Add(10, 84)
Console.WriteLine("10+84 is{0}.", ans)
Console.ReadLine()
End Sub

End Module
Class Calc
Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
CIL of Add() Method
.method public hidebysig instance int32 Add(int32 x,
int32 y) cil managed
{
// Code size 8 (0x8)
.maxstack 2
.locals init ([0] int32 CS$00000003$00000000)
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: add
IL_0003: stloc.0
IL_0004: br.s IL_0006
IL_0006: ldloc.0
IL_0007: ret
} // end of method Calc::Add

22
CIL Of VB Code Main method
IL_000b: callvirt instance int32
.method public static void Main() cil managed
ConsoleApplication7.Calc::Add(int32,
{ int32)
IL_0010: stloc.0
.entrypoint IL_0011: ldstr "10+84 is{0}."
.custom instance void IL_0016: ldloc.0
[mscorlib]System.STAThreadAttribute::.ctor() = ( 01 IL_0017: box [mscorlib]System.Int32
00 00 00 ) IL_001c: call void
[mscorlib]System.Console::WriteLine(string,
// Code size 40 (0x28) object)
.maxstack 3 IL_0021: call string
[mscorlib]System.Console::ReadLine()
.locals init (int32 V_0, IL_0026: pop
class ConsoleApplication7.Calc V_1) IL_0027: ret
} // end of method CalcApp::Main
IL_0000: newobj instance void
ConsoleApplication7.Calc::.ctor()
IL_0005: stloc.1
IL_0006: ldloc.1
IL_0007: ldc.i4.s 10
IL_0009: ldc.i4.s 84

23
Manifest External
Assembly
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 1:0:5000:0
}
.assembly ConsoleApplication1
{

.hash algorithm 0x00008004


.ver 1:0:2058:39833
}
.module ConsoleApplication1.exe
// MVID: {51BE4F31-CBD0-4AE6-BC9D-F9A4976795FD}
.imagebase 0x00400000
.subsystem 0x00000003
.file alignment 4096
.corflags 0x00000001
// Image base: 0x070b0000

24
CIL to Execution
Desktop

CIL JIT Server

Pocket PC

Jitter compiles CIL instructions on the fly


into corresponding machine code and
cache it. This is useful for not recompiling,
if the same method is called again

25
Common Type System (CTS)
CTS is a formal specification that describes
how a given type must be defined for CLR
CTS Class Type
CTS Structure Type
CTS Interface Type
CTS Enumeration type
CTS Delegate type

26
CTS Class Type
Same as C++ class
Can contain members: methods,
properties, events, etc.
Support for abstract members that define a
polymorphic interface for derived classes
Multiple inheritance is not allowed

27
CTS Class Characteristics
"sealed"? – sealed classes can't function
as base classes
Implement any interfaces? – An
interface is a collection of abstract
members
Abstract or Concrete? – Abstract classes
(to define common behaviors for derived)
can't be created directly but concrete
classes can.
Visibility? – visibility attribute to know
whether external assemblies can use it.

28
CTS Structure types
Same as C/C++
Derived from a common base class
System.ValueType
CTS Interface Type
Same as pure abstract class of C++
A description of work that a derived class can
perform
Similar to a class, but can never be instantiated
CTS Enumeration type
To group name/value pairs under a specific name
Default Storage: System.Int32 (could be changed)
CTS Delegate type
Same as C's function pointer (System.MulticastDelegate)
Useful for event handling (ASP .NET)

29
Intrinsic CTS Data Types
.NET Base Type C# Type
System.Byte Byte
System.SByte sbyte
System.Int16 short
System.Int32 int
System.Int64 long
System.UInt64 ulong
System.Single float
System.Double double
System.Object object
System.String string
System.Boolean bool
30
Common Language Specification
(CLS)
Set of guidelines that describe the minimal
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#
Public Sub Foo() public void Foo()
'……. { ……. }
End Sub
31
CLS Compliance
C# Type CLS Compliance
byte Yes
sbyte No
short Yes
int Yes
long Yes
ulong No
float Yes
double Yes
object Yes
string Yes
char Yes
bool Yes 32
Example
public class Calc
{
// CLS compliant
public int Add(int x, int y)
{ return x + y; }

// Not CLS compliant


public ulong Add(ulong x, ulong y)
{ return x + y; }
}
 Once a method is CLS compliant, then all
the .NET aware languages can interact with that
implementation

33
CLR .NET .NET Compiler
Source
Code
DLL or EXE
(CIL)

mscoree.dll

Class Loader

Base Class
Libraries Jitter
(mscorlib.dll)

Platform
Specific code

mscoree.dll
Execute
MicroSoft Common
Object Runtime Execution Engine .NET Execution Engine
34
.NET Namespace
MFC, Java, VB 6.0 have predefined set of
classes; C# doesn't
C# uses namespace concept

Any language targeting the .NET runtime


makes use of the same namespaces and
same types as C#
System is the root namespace

35
Example in C# System Namespace

using System;
public Class MyApp
{
public static void Main()
{ Console class in
Console.WriteLine("Hello World");
System Namespace

}
}

36
Example in VB .NET
Imports System
Public Module MyApp
Sub Main()
Console.WriteLine("Hello World")
End Sub
End Module

37
Example in Managed C++
#using <mscorlib.dll>
using namespace System;
void Main()
{
Console::WriteLine("Hello World");
}

38
Sample .NET namespaces
System primitive types, garbage
collection, etc
System.Collections Container objects: ArrayList,
Queue, etc.
System.Data For Database manipulations
System.Data.Common ADO .NET
System.Data.OleDb
System.Data.SqlClient
System.IO file IO, buffering, etc.
System.Drawing GDI+ primitives, bitmaps, fonts,
System.Drawing.2D icons, etc.

System.Threading Threads

39

You might also like