C# s A Doddle. Steve Love. ACCU April 2013

Similar documents
Introduce C# as Object Oriented programming language. Explain, tokens,

Rules and syntax for inheritance. The boring stuff

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Introduction To C#.NET

A Crash Course in (Some of) Modern C++

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Upcoming Features in C# Mads Torgersen, MSFT

Microsoft Visual C# Step by Step. John Sharp

TYPES, VALUES AND DECLARATIONS

CMSC 330: Organization of Programming Languages

C# Programming in the.net Framework

Item 18: Implement the Standard Dispose Pattern

Course Hours

QUIZ. What is wrong with this code that uses default arguments?

Introduction to Programming Using Java (98-388)

Programming in C and C++

Programming in Visual Basic with Microsoft Visual Studio 2010

Memory Usage. Chapter 23

CMSC 330: Organization of Programming Languages

PROGRAMMING IN VISUAL BASIC WITH MICROSOFT VISUAL STUDIO Course: 10550A; Duration: 5 Days; Instructor-led

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

Instantiation of Template class

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

The C# Programming Language. Overview

Previous C# Releases. C# 3.0 Language Features. C# 3.0 Features. C# 3.0 Orcas. Local Variables. Language Integrated Query 3/23/2007

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Lesson 13 - Vectors Dynamic Data Storage

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Structure of Programming Languages Lecture 10

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

C# and Java. C# and Java are both modern object-oriented languages

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

September 10,

D Programming Language

Object Oriented Programming in C#

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

DC69 C# &.NET DEC 2015

Objectives. Introduce static keyword examine syntax describe common uses

Short Notes of CS201

Chapter 1 Getting Started

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

What property of a C# array indicates its allocated size? What keyword in the base class allows a method to be polymorphic?

CS201 - Introduction to Programming Glossary By

How to be a C# ninja in 10 easy steps. Benjamin Day

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

Fast Introduction to Object Oriented Programming and C++

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

CS

MCSA Universal Windows Platform. A Success Guide to Prepare- Programming in C# edusum.com

SERIOUS ABOUT SOFTWARE. Qt Core features. Timo Strömmer, May 26,

C#: framework overview and in-the-small features

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

Homework #2 Think in C, Write in Assembly

QUIZ Friends class Y;

C# Adds Useful Features

CS558 Programming Languages

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

CSCI-1200 Data Structures Spring 2016 Lecture 22 Priority Queues, part II (& Functors)

LINQ Language-Integrated Query Introduction

Microsoft. Microsoft Visual C# Step by Step. John Sharp

CE221 Programming in C++ Part 1 Introduction

Preliminaries. Part I

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

Rx is a library for composing asynchronous and event-based programs using observable collections.

Object-Oriented Programming

04-19 Discussion Notes

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

20 Most Important Java Programming Interview Questions. Powered by

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

6. Pointers, Structs, and Arrays. 1. Juli 2011

C++11 and Compiler Update

CS558 Programming Languages

I m sure you have been annoyed at least once by having to type out types like this:

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

G Programming Languages - Fall 2012

Ch. 3: The C in C++ - Continued -

MODERN AND LUCID C++ ADVANCED

The Dynamic Typing Interlude

Ch. 12: Operator Overloading

C# in Depth THIRD EDITION

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Introduction. In this preliminary chapter, we introduce a couple of topics we ll be using DEVELOPING CLASSES

CS 376b Computer Vision

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Chapter 12: How to Create and Use Classes

CS558 Programming Languages

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

What are the characteristics of Object Oriented programming language?

Pointers, Dynamic Data, and Reference Types

Introduction to C# Applications

CS 241 Honors Memory

Tokens, Expressions and Control Structures

C++ (Non for C Programmer) (BT307) 40 Hours

Absolute C++ Walter Savitch

9 A Preview of.net 2.0

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Transcription:

C# s A Doddle Steve Love ACCU April 2013

A run through C# (pronounced See Sharp ) is a simple, modern, object-oriented, and type-safe programming language. C# has its roots in the C family of languages, and will be immediately familiar to C, C++, and Java programmers. Garbage Collected Value Types Generics Declarative Functional LINQ Unified type system Sane operator overloading No pointers No ADL No templates No copy constructors No mutable-const

Simple with respect to what? Key(word) differences Table : Keywords C++ C# Keywords 73 77 Alternate representations 11 0

Simple with respect to what? Key(word) differences Table : Keywords C++ C# Keywords 73 77 Alternate representations 11 0 Context Keywords 2 23

Simple with respect to what? Key(word) differences Table : Keywords C++ C# Keywords 73 77 Alternate representations 11 0 Context Keywords 2 23 86 100 K&R C: 27 VB.Net 2008: 154 + 31 unreserved keywords

Unspecified behaviour It s not complex... Do you know what a List<T> is?

Unspecified behaviour It s not complex... Do you know what a List<T> is? 1 linked list? 2 array like?

Unspecified behaviour It s not complex... Do you know what a List<T> is? 1 linked list? 2 array like? No complexity guarantees Adding and removing... at the end? in the middle? Sorting? Counting? Set operations (except, intersection, union)?

Specified un-behaviour How to generate the docs...or how to write comments

Not just a... gin-fuelled rant

Part 1 Safety first

Enumerate static class Lights enum TrafficLight Red = 1, Amber = 2, Green = 4 public static void Main() var light = TrafficLight.Red TrafficLight.Amber; Console.WriteLine( light );

Enumerate static class Lights enum TrafficLight Red = 1, Amber = 2, Green = 4 public static void Main() var light = TrafficLight.Red TrafficLight.Amber; Console.WriteLine( light ); Output: 3 NB: C++ enum class doesn t allow this.

Enumerate static class Lights [Flags] enum TrafficLight Red = 1, Amber = 2, Green = 4 public static void Main() var light = TrafficLight.Red TrafficLight.Amber; Console.WriteLine( light ); Output: Red, Amber

Definitely assigned SomeType somevalue; If SomeType is a value type, somevalue is definitely assigned. otherwise, somevalue must be assigned before it s used.

Definitely assigned SomeType somevalue; If SomeType is a value type, somevalue is definitely assigned. otherwise, somevalue must be assigned before it s used. What about incomplete types? Always a default Reference types can always be null Value types can always be default-constructed

Whilst we re on the subject... The value of the reference

Whilst we re on the subject... The value of the reference

No const No const parameters No const methods const means constant

No const No const parameters No const methods const means constant readonly means const (initonly in VB) properties with no set cannot be assigned... but that doesn t work for collections you use IEnumerable<T> for that

All bets off Call a private method Access private data Circumvent restrictions on circular references

Name lookup using System; using Extensions; public class Extendable int Munge( int x ) return x; static class Program static void Main() var ext = new Extendable(); Console.WriteLine( ext.munge( 10 ) );

Name lookup using System; using Extensions; public class Extendable int Munge( int x ) return x; static class Program static void Main() var ext = new Extendable(); Console.WriteLine( ext.munge( 10 ) ); namespace Extensions public static class ExtendExtendable public static int Munge( this Extendable ext, int x ) return x * 10;

The final word on safety It must be safe because... you can write explicitly unsafe code public unsafe class Liberated public void ICanDoWhatILikeAndItsOk()

Remedies Don t sweat it 1 Use factories where objects must be complete 2 Make value and value-like types immutable 3 Put your extension methods in one place, or a few obvious ones 4 Be vigilant about use of reflection

Remedies Don t sweat it 1 Use factories where objects must be complete 2 Make value and value-like types immutable 3 Put your extension methods in one place, or a few obvious ones 4 Be vigilant about use of reflection Corollary Unsafe code - Just don t write any, right?

Part 2 Generics...one size to fit them all...

I can t get no specialisation

Constraints where T : new(); where T : new( with, args ); What is this doing? static bool Test< T >( T left, T right ) where T : class return left == right;

Operators? There is no constraint which will allow you to use an arithmetic operator with generic parameters. static bool IsMultiple< T >( T x, T y ) return x % y == 0;

Remedies Clever tricks 1 Once again, factories help with the lack of contraints on constructor arguments 2 No operators - a redesign using lambdas and Func or Action arguments instead e.g. static bool IsMultiple( Func< bool > ismult ) return ismult(); static void Main() Console.WriteLine( IsMultiple( () => 10 % 5 == 0 ) );

Part 3 Memory

4 Big Lies 1 GC is slow 2 Automatic Garbage Collection means not having to think about object ownership or lifetime any more. 3 A managed program cannot leak memory. 4 The language ensures that dangling references/pointers are not possible.

Generation game Free Pointer Free Pointer Create some objects with C a child of B. D C B A

Generation game Free Pointer Free Pointer Create some objects with C a child of B. D C B A Free Pointer D C B A B is GC d, taking C with it, and free space compacted. Free Pointer Generation 1 D A The object A hasn t actually moved.

Disposing It s harder than it looks You have to make sure you re using it correctly It can interfere with the efficient operation of the garbage collector...and don t even think about recycling

Resurrection Did you know...? All classes with custom finalizers make it to (at least) the generation 1 heap. It s possible to prevent an object being finalized

Resurrection Did you know...? All classes with custom finalizers make it to (at least) the generation 1 heap. It s possible to prevent an object being finalized...without using the GC object

Leaking public class Hog public Hog( Expensive thing ) this.hog = thing; public void Use() var count = hog.count; // do something with count Expensive hog;

Leaking public class Hog public Hog( Expensive thing ) this.hog = thing; public void Use() var count = hog.count; // do something with count Expensive hog; public class Hog public Hog( Expensive thing ) this.storedcount = hog. Count; public void Use() // do something with // stored count instead int storedcount;

Dangling Wotsit dangled; using( dangled = new Wotsit() ) dangled.value = 10; Console.WriteLine( dangled.value );

Dangling class Wotsit public Wotsit() mine.append( "Dangled" ); ~Wotsit() global = mine; StringBuilder mine = new StringBuilder(); static void Main() var dangled = new Wotsit(); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine( global.tostring() );

Remedies No magic here 1 Understand the Dispose Pattern. Deeply and completely. 2 Don t write finalizers unless you really really have to 1...and if you do, have them invoke Dispose correctly 3 Understand how the GC works, and why 1...which leads to avoiding GC.Collect() in code

Part 4 Equality (yes, some of you may recognise some of what follows)

My favourite standard 5.10/4 Each of the operators shall yield true if the specified relationship is true and false if it is false. Time for a quick pop quiz...

Values and references redux object o = "some string"; string s = string.format( "0 1", "some", "string" ); Console.WriteLine( o == s );

Values and references redux object o = "some string"; string s = string.format( "0 1", "some", "string" ); Console.WriteLine( o == s ); False - identity comparison is used. int i = 10; object o = i; Console.WriteLine( object.referenceequals( o, i ) );

Values and references redux object o = "some string"; string s = string.format( "0 1", "some", "string" ); Console.WriteLine( o == s ); False - identity comparison is used. int i = 10; object o = i; Console.WriteLine( object.referenceequals( o, i ) ); False, boxed value-type. string s = "some string"; object o = string.format( "0 1", "some", "string" ); Console.WriteLine( s == o );

Values and references redux object o = "some string"; string s = string.format( "0 1", "some", "string" ); Console.WriteLine( o == s ); False - identity comparison is used. int i = 10; object o = i; Console.WriteLine( object.referenceequals( o, i ) ); False, boxed value-type. string s = "some string"; object o = string.format( "0 1", "some", "string" ); Console.WriteLine( s == o ); False, RHS must be cast Things aren t all they seem however...

Equality for all? object a = "10"; string b = "10"; object c = string.format( "0", a ); Console.WriteLine( a.equals( b ) );

Equality for all? object a = "10"; string b = "10"; object c = string.format( "0", a ); Console.WriteLine( a.equals( b ) ); True - Equals is virtual Console.WriteLine( b.equals( c ) );

Equality for all? object a = "10"; string b = "10"; object c = string.format( "0", a ); Console.WriteLine( a.equals( b ) ); True - Equals is virtual Console.WriteLine( b.equals( c ) ); True - Equals is overridden for strings Console.WriteLine( object.referenceequals( a, b ) );

Equality for all? object a = "10"; string b = "10"; object c = string.format( "0", a ); Console.WriteLine( a.equals( b ) ); True - Equals is virtual Console.WriteLine( b.equals( c ) ); True - Equals is overridden for strings Console.WriteLine( object.referenceequals( a, b ) ); True! Equal string constants are interned. Console.WriteLine( object.referenceequals( a, c ) );

Equality for all? object a = "10"; string b = "10"; object c = string.format( "0", a ); Console.WriteLine( a.equals( b ) ); True - Equals is virtual Console.WriteLine( b.equals( c ) ); True - Equals is overridden for strings Console.WriteLine( object.referenceequals( a, b ) ); True! Equal string constants are interned. Console.WriteLine( object.referenceequals( a, c ) ); False...of course. Confused?

Look it up You can use anything you like as a key in a Dictionary. It might not behave as you d expect. Let s see an example...

Remedies Avoid the worst 3 simple rules 1 Don t muck about with equality of reference types - you ll regret it 2 Always override equality for value types you create - it ll be quicker 3 Always, but always, make value types immutable.

Part 5 If it s so easy......it could write itself

Syntax sugar The using statement is a straightforward shortcut static void Main() var u = new Used(); try // use u finally u.dispose();

Syntax sugar The using statement is a straightforward shortcut static void Main() var u = new Used(); try // use u finally u.dispose(); static void Main() using(var u = new Used()) // use u

Some refinement The foreach is a bit more sophisticated. If the iterator implements IDisposable you get a try...finally added IEnumerator<T> extends IDisposable IEnumerable<T> returns IEnumerator<T>

Some refinement The foreach is a bit more sophisticated. If the iterator implements IDisposable you get a try...finally added IEnumerator<T> extends IDisposable IEnumerable<T> returns IEnumerator<T> Modern C# will probably almost always have a finally block and a call to a - probably redundant - Dispose method. Oh yes...dispose is a virtual call, by the way

The grand-daddy of give-away code is... The yield statement For using this keyword, you are rewarded with a complete implementation of IEnumerator<T>

Delegates and lambdas Lambdas and anonymous delegates also provoke the compiler into writing whole classes for you. Why? A captured variable lives for at least as long as any delegate instance referring to it Jon Skeet, C# In Depth Read this book!

Nothing for free The canonical captured trap static void Main() var items = new List< Func< int > >(); for( int item = 0; item!= 3; ++item ) items.add( () => item ); foreach( var item in items ) Console.WriteLine( item() );

What s not free More pet peeves 1 Why can t you say var f = ()=> x;? 2 Why must I always write operator!= if I write operator==? There is only one sensible implementation of it!

Remedies TANSTAAFL 1 Understand what code the compiler is writing for you 2 Be consistent with Dispose and using 3 At the risk of advocating Tool Driven Development, learn to listen to complaints from Resharper, FxCop and similar tools

C# is a doddle Too easy We shall have no better conditions in the future if we are satisfied with all those which we have at present. Thomas Edison