Top Banner
Chris Lomont, May 2008 www.lomont.org
55

Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Mar 10, 2018

Download

Documents

buidieu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Chris

Lomont, May 2008www.lomont.org

Page 2: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Purpose: •

Show how C# compares to C/C++.

Demonstrate through code examples.

Promote the general welfare.

Answer questions.

2

Page 3: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Example simple program

3

Page 4: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

How .NET languages interoperate with the system

4

Page 5: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

CLI - Common Language Infrastructure•

CTS –

Common Type System•

Metadata•

CLS -

Common Language Specification•

VES –

Virtual Execution System combines pieces at runtime using metadata

CLR - Common Language Runtime•

the virtual machine

CIL - Common Intermediate Language

JIT – Just-In-Time compile

.NET – the set of language neutral runtime libraries available.

5

Page 6: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

NET languages compile to CIL, which is like assembly code.

The result compiled to bytecode.

CIL is CPU and platform independent.

CIL function classes:•

Load and store•

Arithmetic•

Type conversion•

Object creation and manipulation•

Stack management•

Branching•

Function call and return•

Exception handling•

Concurrency

6

Page 7: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Common Language Runtime (CLR)•

Memory Management

Thread Management•

Security

Exception Handling•

Garbage Collection

Implementations•

Microsoft Windows

Rotor (shared source) by Microsoft•

Mono on Linux

7

Page 8: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Allows cross language inheritance•

Your Python classes can be extended in C++

Visual Basic containers used in C#•

Etc.

Benefits of Intermediate Language (IL)•

Compilers can target processor at deploy time

Metadata allows simpler gluing and versioningAssembly •

1 or more

exe’s

and

dll’s, versioned and tied together

.NET Library knowledge transfers across languages

8

Page 9: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

C#C++/CLIF#J#IronPythonIronRubyVisual BasicA# (Ada)Chrome (Object Pascal)IronLispL# (Lisp)NetCOBOL

Smalltalk#Active OberonAPLNextCommon Larceny (Scheme)Delphi.NETForth.NETDotLispEiffelEnvisionModula-2/CLRHaskell.NETIronScheme

9

Page 10: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

The .NET compiler takes your C# source code and compiles it to CIL that the CLR JIT compiler converts into native code for an operating system.

10

Page 11: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

An overview of the C# language

11

Page 12: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Simple, modern, general-purpose, object-oriented.Strong type checking, array bounds checking, catching uninitialized variables, automatic garbage collection. Useful for distributed environments.Similar to C/C++ for programmer buy-in.Internationalization.Range from embedded to large systems.

12

Page 13: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Very much like C/C++/Java•

Functions, { }

int, long, double, float

for, while, switch, case, break

class,

struct, public, protected, private, namespace

13

Page 14: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

type Notesbool true of falsebyte 8 bits, 0-255sbyte -128 to 127char 16 bit Unicodedecimal 128 bits, high precision, ±1.0

×

10−28

to ±7.9

×

1028

double 64 bits, ±5.0

×

10−324

to ±1.7

×

10308

float 32 bits, ±1.5

×

10−45

to ±3.4

×

1038

int 32 bits, -2,147,483,648 to 2,147,483,647uint 32 bits, 0 to 4,294,967,295long 64 bits, –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

ulong 64 bits, 0 to 18,446,744,073,709,551,615object Base of unified type systemstring Unicode string built in as base type.

14

Page 15: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Very C++ likeSupports multidimensional, jaggedBounds checkedSimple initializationSimple looping with foreachMany Array functions across all arrays:•

Length

Copy•

Sort

BinarySearch•

Find

FindAll

15

Page 16: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Unicode Internationalization supportExcellent formatting (C/C++ printf and cout suck)ImmutableStringBuilderFunctions include:

Length•

Array of chars•

Compare•

Concatenation•

Contains•

Insert•

Pad•

Replace•

Substring•

Trim

16

Page 17: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

17

Page 18: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

switch on stringConditionals require bool, no “if (int…)Unified Type System (all objects derive from object, even int, short, etc.)No shadowingNo typedef

18

Page 19: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Syntax very C/C++ like•

class, public, private, protected, static, virtual•

Operator overloading•

No multiple inheritancestatic constructor initializes a class typeConstructor chainingvirtual and override and differencesealed classesCan seal virtual methodsAbstract classesInterfaces

Multiple interfaces allowednew on function allows shadowingabstract versus interface

19

Page 20: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

out•

Will return a value

ref•

Can change a value

params•

Variable number of parameters

20

Page 21: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

get and setNamed property values on construction.No ‘->’, use’.’Visibility of get/set make read only properties.Automatic properties

21

Page 22: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

ZLIB

22

Page 23: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Replaces C++ multiple-inheritance•

Similar to an abstract base class.

Defines a contract•

class must implement a set of members

Examples•

ICloneable

-

implements Clone, object copier

IComparable

generic comparison•

IDisposable

release allocated resources, using

IFormattable

implements

ToString()•

IEnumerable

allows

foreach

looping

23

Page 24: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

A simple interface

24

Page 25: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

as and is walk object hierarchies

25

Page 26: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Performance excellentMuch quicker to develop codeCan control GC•

Finalize –

deterministically release resources

IDisposable

class implements DisposeAdvantages•

Much simpler to write code

May be faster by caching and grouping

deallocationsDisadvantages•

May be slower, but you can always manually do GC

Can be overriden to operate with pointers and “unsafe” code.

26

Page 27: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Like C++, but adds finally and usingtry/catch/finallyusingException provides:•

Can nest exceptions -

InnerException

Help link•

Message

Source•

StackTrace

delivers stack trace

TargetSite

get thrower

27

Page 28: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Throw an exception

28

Page 29: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Bad •

leaks on exceptions

Better •

try/finally

Best •

using

29

Page 30: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

#if / #else / #elif / #endif#define SYMBOL / #undef

DEBUG standard debug symbolNo C++ style macros

#warning / #error#line#region / #endregion•

Used for code folding

#pragma•

warning

checksum

30

Page 31: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

No time to cover all these:•

Interesting keywords:

internal, sealed, checked, readonly•

Boxing and

unboxing

static

constructors•

Generators through the yield

keyword (2.0+)

Anonymous delegates implement closure•

Anonymous methods

Anonymous types•

Extension methods –

can add functionality to built in types.

31

Page 32: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

“lock” keyword •

makes easy mutual exclusive sections

Producer / consumer queue

32

Page 33: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

delegate•

Typesafe

function pointer

Allows “closure”

binding of values to variables•

Used to implement callbacks and event listeners.

event•

Objects can subscribe to events

Allows listening to other objects and other threads

Fairly complicated to explore here

33

Page 34: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Lambda •

From formal language theory, the Lambda Calculus

Requires C# 3.0•

Allows inline anonymous functions

Replaces the functor concept from C++ with a much nicer, simpler syntax.First class items - can be passed as function arguments, for example.

34

Page 35: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

use ‘=>’ for a transform

35

Page 36: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Generics •

Instantiated at runtime instead of at compile time

Reflection allows discovery at runtime, unlike C++

36

Page 37: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

XML basedNDOC to HTML helpSandCastle to HTMLExamples with Intellisense popupsTagsAdds to Intellisense immediatelyTODO – more ☺

37

Page 38: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Think of attributes as object “metadata”Can make custom ones for your own uses[Serializable] •

Makes a class

serializable automagically

[Flags]•

Makes an

enum

work like flags (can combine)

[Obsolete][CLSCompliant]

38

Page 39: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Allows runtime inspection of classes and assemblies

Full type reflection and discovery

Used throughout the tools, IDE, and verification code

39

Page 40: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

LINQ – Language Integrated Query

Allows easy query of •

Databases

Containers•

Local and remote objects and

datastores

40

Page 41: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

More typesafe than C++.Buffer overflows caught.Provable pieces of code.Checked math•

checked

keyword catches over/underflow

Casting to smaller types requires deliberate cast.

41

Page 42: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Compilation model •

No headers

XML comments integrate well with IDE•

Intellisense

much better and less error prone

Many other IDE nicetiesJust In Time (JIT)•

Charles Bloom calls JIT “Just Too Late”

Performance •

Much, much faster than C++ compilation times

Seems to background compile all the time•

Need more data

42

Page 43: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

NGEN.exe will convert to exe for you.ILDISASM example, C# code to 32 and 64 bit

43

Page 44: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Pre-invented wheels

44

Page 45: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Knowledge useable across all languagesLarge class libraryEvolves much faster than C++•

Could be bad, but so far seems good.

Hierarchical, much easier to use than Win32 API.

45

Page 46: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Regular expressionsRandom numbersFile

Isolated storageCryptographyNetworking

Low level and high level Serial portsRich Imaging support

2D and 3DFontsTextSystem support

Processor infoRich system counters through WMIExtensive threading supportDiagnostics.Stopwatch

Date, Time, TimeSpanManaged DirectX (add-on)Internet protocols

HTML and web requests•

Mail•

FTP•

MoreDebug support

Debug.Assert•

TraceListenersEasy things to do:

Split path into file and directory•

Walk directories•

Get file sizes•

Find drives, identify types•

Find # processors•

Time execution of code

46

Page 47: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

47

Type FunctionArray static sized arrayList dynamic sized arrayBitArray Array of

boolHashtable Standard hash table (key,value)Queue StandardSortedList Sorted upon insertsDictionary List of <key, value> pairsHashSet Set of valuesLinkedList Linked listSortedDictionary List of <key,value>, sorted by key

Page 48: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

IFormattable interface•

Objects format themselves (with extensions)

{index[,alignment][:formatString]}

Slightly easier than stream overloading in C++

48

Page 49: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

System.Net and System.Net.SocketsWeb protocols easy to use•

Http, Mail, Ftp, cookies, DNS, Credentials, IP Address, TCP/IP, Sockets

49

Page 50: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Excellent GUI Designer

50

Page 51: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Windows Presentation Foundation•

New method of user interfaces

Separates code from presentationXAML defines the UI, independent of the applicationVector based instead of pixel based.

51

Page 52: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Web AppsSilverlight,WCFXNAASP.NETCan share code and knowledge between windows and web apps

52

Page 53: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Enhancing productivity

53

Page 54: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Code refactoring toolsClass DiagramCode SnippetsCode View WindowExcellent GUI DesignerMuch more.Nice tools added to C# before C++ by Microsoft…

54

Page 55: Chris Lomont, May 2008 for C++ programmers.pdfCommon Type System • Metadata • CLS - Common Language Specification • VES – Virtual Execution System xcombines pieces at runtime

Until next time

55