Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C # – Part 2 Stephen Turner Software Design Engineer Microsoft UK.

Similar presentations


Presentation on theme: "Introduction to C # – Part 2 Stephen Turner Software Design Engineer Microsoft UK."— Presentation transcript:

1 Introduction to C # – Part 2 Stephen Turner Software Design Engineer sturner@microsoft.com Microsoft UK

2 Agenda Design goals – Part 2  Fully extensible type system  Enable robust and durable applications  Leverage existing software Standardization

3 Extending the Type System Most users think of two types of objects  “Real” objects – Customer, Order, etc.  Primitive types – int, float, bool Different expectations for each  Real objects are more expensive to create  Primitives always have a value  Primitives have operator support Classes and Structs – best of both worlds! Natural semantics  Operator overloading & User conversions Interface support

4 Rational Numbers (½, ¾, 1½) Rational r1 = new Rational(1,2); Rational r2 = new Rational(2,1); Rational r3 = r1.AddRational(r2); double d = Rational.ConvertToDouble(r3); Rational r1 = new Rational(1,2); Rational r2 = 2; Rational r3 = r1 + r2; double d = (double) r3;

5 Rational Number – Class? Heap allocated Can be null “=” assigns reference not value Arrays allocate references not values public class Rational { public Rational(int n, int d) { … } } … Rational[] array = new Rational[100];

6 Structs Provide an Answer Behavior differences versus Classes  Stored in-line, not heap allocated  Never null  Assignment copies data, not reference Implementation differences  Always inherit from object  Always has a default constructor

7 Rational Number – Struct public struct Rational { public Rational(int n, int d) { … } public int Numerator { get{…} } public int Denominator { get{…} } public override string ToString() { … } } Rational r = new Rational(1,2); string s = r.ToString();

8 Implicit Conversions No loss of data public struct Rational { … public static implicit operator Rational(int i) { return new Rational(i,1); } Rational r = 2;

9 Explicit Conversions Possible loss of precision and can throw exceptions public struct Rational { … public static explicit operator double(Rational r) { return (double) r.Numerator / r.Denominator; } Rational r = new Rational(2,3); double d = (double) r;

10 Operator Overloading Static operators Must take its type as a parameter public struct Rational { … public static Rational operator+ ( Rational lhs, Rational rhs) { return new Rational( … ); } Rational r3 = r1 + r2; r3 += 2;

11 Equality Operators.NET Framework equality support.Equals() should use operator==() public override bool Equals(object o) public static bool operator== (Rational lhs, Rational rhs) public static bool operator!= (Rational lhs, Rational rhs) if ( r1.Equals(r2) ) { … }if ( !r1.Equals(r2)) { … } if ( r1 == r2 ) { … }if ( r1 != r2 ) { … }

12 Structs and Interfaces Structs can implement interfaces to provide additional functionality Why? The same reasons classes can! Examples  System.IComparable  Search and sort support in collections  System.IFormattable  Placeholder formatting

13 System.IFormattable Types can support new formatting options through IFormattable Rational r1 = new Rational(2,4); Console.WriteLine(“Rational {0}", r1); Console.WriteLine(“Rational {0:reduced}", r1);

14 Implementing IFormattable public struct Rational : IFormattable { public string Format( string formatStr, IServiceObjectProvider isop) { s = this.ToString(); if ( formatStr == “reduced" ) { s = … } return s; } Rational r1 = new Rational(2,4); Console.WriteLine("No Format = {0}", r1); Console.WriteLine("Reduced Format = {0:reduced}", r1); No Format = 2/4 Reduced Format = 1/2

15 Agenda Design goals – Part 2  Fully extensible type system  Enable robust and durable applications  Leverage existing software Standardization

16 Robust and Durable Software Garbage collection  No memory leaks and stray pointers Exceptions  Error handling is not an afterthought Type-safety  No uninitialized variables, unsafe casts Versioning  Pervasive versioning considerations in all aspects of language design

17 Language Safety vs. C++ If, while, do require bool condition Goto can’t jump into blocks Switch statement  No fall-through  Break, goto or goto default Checked and unchecked statements Expression statements must do work void Foo() { i == 1; // error }

18 Versioning Overlooked in most languages  C++ and Java produce fragile base classes  Users unable to express versioning intent C# allows intent to be expressed  Methods are not virtual by default  C# keywords “virtual”, “override” and “new” provide context But C# can't guarantee versioning  Can enable (e.g., explicit override)  Can encourage (e.g., smart defaults)

19 Method Versioning in Java class Derived extends Base // v1.0 { public void Foo() public void Foo() { System.out.println("Derived.Foo"); System.out.println("Derived.Foo"); }} class Base // v1.0 {} class Base // v2.0 { public void Foo() public void Foo() { Database.Log("Base.Foo"); Database.Log("Base.Foo"); }} class Base // v2.0 { public int Foo() public int Foo() { Database.Log("Base.Foo"); Database.Log("Base.Foo"); }}

20 Method Versioning in C# class Derived : Base // v1.0 { public virtual void Foo() public virtual void Foo() { Console.WriteLine("Derived.Foo"); Console.WriteLine("Derived.Foo"); }} class Base // v1.0 {} class Base // v2.0 { public virtual void Foo() public virtual void Foo() { Database.Log("Base.Foo"); Database.Log("Base.Foo"); }} class Base // v2.0 { public virtual int Foo() public virtual int Foo() { Database.Log("Base.Foo"); return 0; Database.Log("Base.Foo"); return 0; }} class Derived : Base // v2.0 { public new virtual void Foo() public new virtual void Foo() { Console.WriteLine("Derived.Foo"); Console.WriteLine("Derived.Foo"); }} class Derived : Base // v2.0 { public override void Foo() public override void Foo() { super.Foo(); super.Foo(); Console.WriteLine("Derived.Foo"); Console.WriteLine("Derived.Foo"); }}

21 Agenda Design goals – Part 2  Fully extensible type system  Enable robust and durable applications  Leverage existing software Standardization

22 Calling Into Existing DLLs Runtime enables calling “C-Style” functions Feature known as “Platform Invoke” Attributes define how things work Which library to use [DllImport] How to marshal data [MarshalAs] Structure layout in memory [StructLayout] [FieldOffset]

23 DLL Import Examples [DllImport("gdi32.dll", CharSet=CharSet.Auto)] public static extern int GetObject( int hObject, int nSize,[In, Out] ref LOGFONT lf); [DllImport("gdi32.dll")] public static extern int CreatePen( int style, int width, int color);

24 Platform Invoke Limitations Marshaler can’t handle every case  Ugly dealing with memory allocation  Complex structures can’t be marshalled Complex P/Invoke is hard to debug  Play “convince the marshaller”… Can’t use C++ objects Can waste lots of time and effort  Did I say it was tough to debug? Solution:  Use unsafe C# or Managed C++

25 COM Support.NET Framework provides great COM support  TLBIMP imports existing COM classes  TLBEXP exports.NET types Sometimes you need more control  Methods taking complicated structures  Large TLB – only using a few classes System.Runtime.Interopservices  COM object identification  Parameter and return value marshalling  HRESULT behavior

26 COM Support Sometimes you need more control  Methods with complicated structures as arguments  Large TLB – only using a few classes System.Runtime.InteropServices  COM object identification  Parameter and return value marshalling  HRESULT behavior

27 COM Support Example [Guid(“56A868B1-0AD4-11CE-B03A-0020AF0BA770”)] interface IMediaControl { void Run(); void Pause(); void Stop(); … void RenderFile(string strFilename); }

28 Unsafe Code – Pointers Developers sometime need total control  Performance extremes  Dealing with existing binary structures  Advanced COM Support, DLL Import C# “unsafe” = limited “inline C”  Pointer types, pointer arithmetic  unsafe casts  Declarative pinning (fixed statement) Power comes at a price!  Unsafe means unverifiable code

29 Unsafe Code & P/Invoke 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); }

30 Agenda Extending the type system  Creating a fully functional rational number type Enable robust and durable applications  Language safety & code versioning Leverage existing software investment  COM & DLL interop  Unsafe code and pointers Standardization

31 C# and CLI Standardization Work begun in September 2000  Intel, HP, IBM, Fujitsu, Plum Hall, and others ECMA ratified in December 2001 ISO ratified in April 2003 Several CLI and C# implementations .NET Framework and Visual Studio.NET  SSCLI – Shared source on XP, FreeBSD, OS X  Mono – Open source on Linux Standardization of new features ongoing

32 Useful Resources Web sites  http://www.gotdotnet.com/team/csharp  http://msdn.microsoft.com/vcsharp  http://msdn.microsoft.com/netframework Newsgroups  http://msdn.microsoft.com/newsgroups  microsoft.public.dotnet.languages.csharp Books  C# Language Specification” – MS Press  Inside C# – MS Press

33 C# Momentum Over 50 trade books published  O’Reilly, Addison-Wesley, Prentice Hall, APress, Osborne, Sams, Wrox, MS Press Over 15 dedicated Web sites  http://www.csharp-station.com  http://www.csharphelp.com  http://www.csharptoday.com  http://www.csharpindex.com Other site can be found at  http://msdn.microsoft.com/community

34 © 2003 Microsoft Ltd. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.


Download ppt "Introduction to C # – Part 2 Stephen Turner Software Design Engineer Microsoft UK."

Similar presentations


Ads by Google