Top Banner
Bill Campbell, UMB Bill Campbell, UMB Microsoft's .NET Microsoft's .NET C# and C# and The Common Language The Common Language Runtime Runtime
22

Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Jan 20, 2016

Download

Documents

Erik Allison
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: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Microsoft's .NETMicrosoft's .NET

C# and C# and

The Common Language The Common Language Runtime Runtime

Page 2: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

CorrespondenceCorrespondence

Java (J2EE)Java (J2EE) .Net.Net

Family of StandardsFamily of Standards Family of Products Family of Products

Cross platformCross platform WindowsWindows

JavaJavaC# (VB, C++, Jscript)C# (VB, C++, Jscript)

& others& others

JSPJSP ASP+ASP+

Page 3: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Correspondence (cont)Correspondence (cont)

Java (J2EE)Java (J2EE) .Net.Net

JDBCJDBC .ADO.ADO

EJBEJB DCOM+DCOM+

IIOP, SOAP (xml)IIOP, SOAP (xml) Remoting, SOAPRemoting, SOAP

Several VendorsSeveral Vendors One vendorOne vendor

Page 4: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

C# a language for programmersC# a language for programmers

From C and C++ From C and C++ Managed Data (garbage collection)Managed Data (garbage collection) Larger than Java Larger than Java

• (80 vs 50 keywords)(80 vs 50 keywords)• Preprocessing directives, #ifdef etc.Preprocessing directives, #ifdef etc.• Operator overloadingOperator overloading• Syntactic sugar (convenience)Syntactic sugar (convenience)

Page 5: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Everybody's an ObjectEverybody's an Object

Type

Value Type Ref Type

Scalar Enumeration Class Interface

Structure

(stack allocated) (heap allocated)

Delegate

Page 6: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Choice in ScalarsChoice in Scalars

sbytesbyte floatfloat charcharbytebyte doubledouble boolboolshortshort decimaldecimalushortushortintintuintuintlonglongulongulong

Page 7: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Scalars are structsScalars are structs

int is really syntax for Int32int is really syntax for Int32

struct Int32struct Int32{...{...}}

so, e.g. 56.ToString()so, e.g. 56.ToString()

Page 8: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

keywordskeywordsabstract as base bool breakabstract as base bool breakbyte byte case case catch char catch char checked checked class const continue decimal defaultclass const continue decimal defaultdelegate do double else enumdelegate do double else enumevent explicit extern false finalevent explicit extern false finalfixed float for foreach goto fixed float for foreach goto if implicit in int interfaceif implicit in int interfaceinternal is lock long namespace internal is lock long namespace new null object operator out new null object operator out override params private protected public override params private protected public readonly ref return sbyte sealed readonly ref return sbyte sealed short sizeof stackalloc static string short sizeof stackalloc static string struct switch this throw true struct switch this throw true try typeof uint ulong unchecked try typeof uint ulong unchecked void volatile whilevoid volatile while

Page 9: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

foreachforeach

foreach (int elem in intArray) foreach (int elem in intArray) {{

……}}

foreach (BankAccount acct in accounts) foreach (BankAccount acct in accounts) {{

……

}}

Page 10: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Properties mimic field syntaxProperties mimic field syntax

Temperature t = New Temperature(...);Temperature t = New Temperature(...);

......

double c = t.DegreesCelsius;double c = t.DegreesCelsius;

......

t.DegreesCelsius = 0.0;t.DegreesCelsius = 0.0;

double f = t.DegreesFarenheit; // 32.0double f = t.DegreesFarenheit; // 32.0

Page 11: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Behave like getters and settersBehave like getters and setters

public double DegreesCelsiuspublic double DegreesCelsius

{{

getget

{{

return degreesCelsius;return degreesCelsius;

}}

setset

{{

degreesCelsius = value;degreesCelsius = value;

}}

}}

Page 12: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

DegreesFahrenheit PropertyDegreesFahrenheit Property

public double DegreesFahrenheitpublic double DegreesFahrenheit{{

getget{ {

return 9.0/5.0*DegreesCelsius + 32.0;return 9.0/5.0*DegreesCelsius + 32.0;}}setset{{

DegreesCelsius = 5.0/9.0*(value-32.0);DegreesCelsius = 5.0/9.0*(value-32.0);}}

}}

Page 13: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

IndexersIndexers

public class BitSetpublic class BitSet{{ ulong word;ulong word; public bool this[int position]public bool this[int position] {{

getget{ { if (position < 0 || position > 63)if (position < 0 || position > 63)

throw new IndexOutOfRangeException();throw new IndexOutOfRangeException(); elseelse

return word & (1 << position) != 0;return word & (1 << position) != 0;}}......

}}

Page 14: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Properties and Indexers Properties and Indexers Ubiquitous Ubiquitous

button.Sizebutton.Size

"cat".length => 3"cat".length => 3"cat"[1] => 'a'"cat"[1] => 'a'

hashtable["key"] = "value";hashtable["key"] = "value";......hashtable["key"] => "value"hashtable["key"] => "value"

Page 15: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Boxing and Unboxing Boxing and Unboxing

Like Java, Collections of (Reference) ObjectsLike Java, Collections of (Reference) Objects

hashtable["one"] = (object) 1; // 1 boxedhashtable["one"] = (object) 1; // 1 boxed

int one = (int) hashtable["one"]; // unboxedint one = (int) hashtable["one"]; // unboxed

Page 16: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Delegates and Events Delegates and Events

Type safe callbacksType safe callbacks

Button button = new Button("Press Me");Button button = new Button("Press Me");button.Click += new button.Click += new

System.EventHandler(button_Click);System.EventHandler(button_Click);......private void button_Click(object sender, private void button_Click(object sender,

System.EventArgs e)System.EventArgs e){{ <do whatever you want for a pressed button><do whatever you want for a pressed button>}}

Page 17: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Attributes Attributes For marking up your programFor marking up your program

[Serializable][Serializable]class BankAccountclass BankAccount{ ...{ ...}}

[Flags, Serializable][Flags, Serializable] You can define your ownYou can define your ownpublic enum FileAttributespublic enum FileAttributes{{ They can have argumentsThey can have arguments ReadOnly = 0x0001,ReadOnly = 0x0001, ...... Your program can actYour program can act Encrypted = 0x4000Encrypted = 0x4000 upon them at run time upon them at run time}}

Page 18: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Common Language Runtime (CLR)Common Language Runtime (CLR)

Visual BasicVisual Basic C++C++ JScriptJScript CLR (JIT) CLR (JIT) native native

codecode C#C# (others) (others) CLR CLR a modela model

Page 19: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

Common Type SystemCommon Type System

Your language can talk to all the Your language can talk to all the others so long as it supports:others so long as it supports:

boolbool short short ulongulong

charchar intint

floatfloat longlong

doubledouble bytebyte

Page 20: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

CLR EntitiesCLR Entities

Assemblies are logical collections of Assemblies are logical collections of program parts (classes, interfaces, etc.)program parts (classes, interfaces, etc.)• Contain ALL the metadata required for an Contain ALL the metadata required for an

application (so it doesn't have to go in application (so it doesn't have to go in registries)registries)

• One assembly can be physically captured by One assembly can be physically captured by several (.exe or .dll) files several (.exe or .dll) files

• Support versioningSupport versioning• Can operate under one of several publishing Can operate under one of several publishing

policies (eg bug fixes vs. new versions)policies (eg bug fixes vs. new versions) Namespaces are purely logicalNamespaces are purely logical

Page 21: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

CLR ImplementationCLR Implementation

CLR has a real assembly languageCLR has a real assembly language

assembly P -ildasmassembly P -ildasm -ilasm -ilasm P P

CLR is a beautiful target for compilersCLR is a beautiful target for compilers

Implemented using a "just in time" Implemented using a "just in time" compilercompiler

Generational garbage collectorGenerational garbage collector

Page 22: Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.

Bill Campbell, UMBBill Campbell, UMB

How to learn about this stuffHow to learn about this stuff

InternetInternet Good books (Microsoft Press)Good books (Microsoft Press) Download the MS Framework SDKDownload the MS Framework SDK Borrow Visual Studio .net under the Borrow Visual Studio .net under the

msdnaa program here at umb (You must msdnaa program here at umb (You must be a student and adhere to the licensing be a student and adhere to the licensing agreement.)agreement.)

Take one of my summer courses: CS650 or Take one of my summer courses: CS650 or CS697a (both limited in size)CS697a (both limited in size)