220 likes | 444 Views
Microsoft's .NET. C# and The Common Language Runtime . Correspondence. Correspondence (cont). C# a language for programmers. From C and C++ Managed Data (garbage collection) Larger than Java (80 vs 50 keywords) Preprocessing directives, #ifdef etc. Operator overloading
E N D
Microsoft's .NET C# and The Common Language Runtime Bill Campbell, UMB
Correspondence Bill Campbell, UMB
Correspondence (cont) Bill Campbell, UMB
C# a language for programmers • From C and C++ • Managed Data (garbage collection) • Larger than Java • (80 vs 50 keywords) • Preprocessing directives, #ifdef etc. • Operator overloading • Syntactic sugar (convenience) Bill Campbell, UMB
Type Value Type Ref Type Scalar Enumeration Class Interface Structure Delegate Everybody's an Object (stack allocated) (heap allocated) Bill Campbell, UMB
Choice in Scalars sbyte float char byte double bool short decimal ushort int uint long ulong Bill Campbell, UMB
Scalars are structs int is really syntax for Int32 struct Int32 {... } so, e.g. 56.ToString() Bill Campbell, UMB
keywords abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false final fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked void volatile while Bill Campbell, UMB
foreach foreach (int elem in intArray) { … } foreach (BankAccount acct in accounts) { … } Bill Campbell, UMB
Properties mimic field syntax Temperature t = New Temperature(...); ... double c = t.DegreesCelsius; ... t.DegreesCelsius = 0.0; double f = t.DegreesFarenheit; // 32.0 Bill Campbell, UMB
Behave like getters and setters public double DegreesCelsius { get { return degreesCelsius; } set { degreesCelsius = value; } } Bill Campbell, UMB
DegreesFahrenheit Property public double DegreesFahrenheit { get { return 9.0/5.0*DegreesCelsius + 32.0; } set { DegreesCelsius = 5.0/9.0*(value-32.0); } } Bill Campbell, UMB
Indexers public class BitSet { ulong word; public bool this[int position] { get { if (position < 0 || position > 63) throw new IndexOutOfRangeException(); else return word & (1 << position) != 0; } ... } Bill Campbell, UMB
Properties and Indexers Ubiquitous button.Size "cat".length => 3 "cat"[1] => 'a' hashtable["key"] = "value"; ... hashtable["key"] => "value" Bill Campbell, UMB
Boxing and Unboxing Like Java, Collections of (Reference) Objects hashtable["one"] = (object) 1; // 1 boxed int one = (int) hashtable["one"]; // unboxed Bill Campbell, UMB
Delegates and Events Type safe callbacks Button button = new Button("Press Me"); button.Click += new System.EventHandler(button_Click); ... private void button_Click(object sender, System.EventArgs e) { <do whatever you want for a pressed button> } Bill Campbell, UMB
Attributes For marking up your program [Serializable] class BankAccount { ... } [Flags, Serializable] You can define your own public enum FileAttributes { They can have arguments ReadOnly = 0x0001, ... Your program can act Encrypted = 0x4000 upon them at run time } Bill Campbell, UMB
Common Language Runtime (CLR) • Visual Basic • C++ • JScript CLR (JIT) native code • C# • (others) CLR a model Bill Campbell, UMB
Common Type System Your language can talk to all the others so long as it supports: bool short ulong char int float long double byte Bill Campbell, UMB
CLR Entities • Assemblies are logical collections of program parts (classes, interfaces, etc.) • Contain ALL the metadata required for an application (so it doesn't have to go in registries) • One assembly can be physically captured by several (.exe or .dll) files • Support versioning • Can operate under one of several publishing policies (eg bug fixes vs. new versions) • Namespaces are purely logical Bill Campbell, UMB
CLR Implementation • CLR has a real assembly language assembly P -ildasm -ilasm P • CLR is a beautiful target for compilers • Implemented using a "just in time" compiler • Generational garbage collector Bill Campbell, UMB
How to learn about this stuff • Internet • Good books (Microsoft Press) • Download the MS Framework SDK • Borrow Visual Studio .net under the msdnaa program here at umb (You must be a student and adhere to the licensing agreement.) • Take one of my summer courses: CS650 or CS697a (both limited in size) Bill Campbell, UMB