The 100% Inspiration Tour
Exploring the .NET Framework with C# David Chalmers Academic Team, Microsoft UK davidcha@microsoft.com http://blogs.gotdotnet.com/davidcha
Topics Exploring the .NET Framework SDK What actually is .NET? Understanding key Framework concepts Delving into the Common Language Runtime Introducing the C# programming language
What is .NET? Software for connecting people, information, systems and devices? Runtime for executing code Tools to help develop applications Server products to manage applications Value-added services Managed environment for developing and executing components
.NET Framework SDK A platform for building Standards based XML Web services Windows Applications Web Applications Mobile Device Applications Standards based Enhances developer productivity Simplifies application development Makes delivery of complex applications possible
Creating Our First .NET Application
A Modest Application! Module Module1 Sub Main() 10: Console.WriteLine(“I rock”) 20: GoTo 10 End Sub End Module
Introducing C# Less typing! Object Orientation Better control structures Combines best features of C/C++ Java Visual Basic
Languages with {}; using System; public class HelloWorld { static void Main(string[] args) Console.WriteLine(s); }
A Improved ‘Hello, World’ using System; public class HelloWorld { static void Main(string[] args) string[] strArray = {“I rock”, “You rock”, “We all rock”}; foreach(string s in strArray) Console.WriteLine(s); }
C# Program Structure using System; namespace System.Collections { public class Stack Entry top; public void Push(object data) { top = new Entry(top, data); } public object Pop() { if (top == null) throw new InvalidOperationException(); object result = top.data; top = top.next; return result;
Properties Properties are “smart fields” Natural syntax, accessors, inlining public class Button: Control { private string caption; public string Caption { get { return caption; } set { caption = value; Repaint(); Button b = new Button(); b.Caption = "OK"; String s = b.Caption;
Attributes public class OrderProcessor { [WebMethod] public void SubmitOrder(PurchaseOrder order) {...} } [XmlRoot("Order", Namespace="urn:acme.b2b-schema.v1")] public class PurchaseOrder [XmlElement("shipTo")] public Address ShipTo; [XmlElement("billTo")] public Address BillTo; [XmlElement("comment")] public string Comment; [XmlElement("items")] public Item[] Items; [XmlAttribute("date")] public DateTime OrderDate; public class Address {...} public class Item {...}
Indexers Indexers are “smart arrays” Can be overloaded public class ListBox: Control { private string[] items; public string this[int index] { get { return items[index]; } set { items[index] = value; Repaint(); ListBox listBox = new ListBox(); listBox[0] = "hello"; Console.WriteLine(listBox[0]);
foreach Statement Iteration of arrays Iteration of user-defined collections public static void Main(string[] args) { foreach (string s in args) Console.WriteLine(s); } foreach (Customer c in customers.OrderBy("name")) { if (c.Orders.Count != 0) { ... }
C# – The Big Ideas Everything is an Object Traditional views C++, Java: Primitive types are ‘magic’ and do not interoperate with objects Smalltalk, Lisp: Primitive types are objects, but at great performance cost C# unifies with no performance cost Deep simplicity throughout system Improved extensibility and reusability New primitive types: Decimal, SQL… Collections, etc., work for all types
C# – The Big Ideas Building Robust Code Garbage collection No memory leaks and stray pointers Exceptions Error handling is not an afterthought Type-safety No uninitialised variables, unsafe casts Versioning Pervasive versioning considerations in all aspects of language design
Advanced Coding Capabilities class FileStream: Stream { int handle; public unsafe int Read(byte[] buffer, int index, int count) { int n = 0; fixed (byte* p = buffer) { ReadFile(handle, p + index, count, &n, null); } return n; [dllimport("kernel32", SetLastError=true)] static extern unsafe bool ReadFile(int hFile, void* lpBuffer, int nBytesToRead, int* nBytesRead, Overlapped* lpOverlapped);
Objects and Types Need a way of grouping code with the data it operates on Classes Primitive types Operations on those types Ensuring interoperability Common Type System The Common Language Specification (CLS)
Libraries and the CLS Classes for objects that are generally useful or support specific application types are built into libraries Most of the base-class libraries are CLS compliant Can be called by any component you create in a CLS-compliant programming language
Don’t Start from Scratch! Lots of good content out there to learn from Quick Start Tutorials samples.gotdotnet.com/quickstart ASP.NET Starter Kits www.asp.net Sample Applications IBuySpy Pet Store Many more …
Delving into Source Code
What’s Under the Hood? Assemblies Execution Managed Execution IL Metadata Execution Class Loader Security Verification Managed Execution Just In Time Compilation Garbage Collection
Common Language Specification Common Language Runtime The .NET Framework VB C++ C# JScript … Visual Studio.NET Common Language Specification ASP.NET Windows Forms Data and XML Base Class Library Web Matrix Common Language Runtime Windows COM+ Services
Common Language Runtime Base Class Library Support Thread Support COM Marshaler Type Checker Exception Manager Security Engine Debug Engine IL to Native Compilers Code Manager Garbage Collector Class Loader
Compilation and Execution Model Source Code Language Compiler Assembly Code (IL) Metadata Execution JIT Compiler Native Code At installation or the first time each method is called
Summary Flexible platform for sharing code and data across languages Processes Machines Sites or domains Execution Environment Powerful language support Great tools and utilities for developers
Further Resources .NET Framework SDK Further Reading msdn.microsoft.com/netframework msdn.microsoft.com/vstudio Further Reading Applied .NET Framework Programming. Richter, J. (2002); Microsoft Press www.microsoft.com/mspress/books/5353.asp Inspiration Tour Site www.microsoft.com/uk/inspiration
© 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.