SlideShare a Scribd company logo
C# for C++ Programmers A crash course
C#: the basics Lots of similarities with C++ Object-oriented Classes, structs, enums Familiar basic types: int, double, bool,… Familiar keywords: for, while, if, else,… Similar syntax: curly braces { }, dot notation,… Exceptions: try and catch
C#: the basics Actually much more similar to Java Everything lives in a class/struct (no globals) No pointers! (so no ->, * or & notation) Garbage collection: no delete! No header files No multiple inheritance Interfaces Static members accessed with . (not ::) In a nutshell: much easier than C++ 
Hello, world! using System; // Everything's in a namespace namespace HelloWorldApp { // A simple class class Program     { // A simple field: note we can instantiate it on the same line private static String helloMessage = "Hello, world!"; // Even Main() isn't global! static void Main(string[] args)         { Console.WriteLine(helloMessage);         }     } }
C# features Properties Interfaces The foreach keyword The readonly keyword Parameter modifiers: ref and out Delegates and events Instead of callbacks Generics Instead of templates
Properties Class members, alongside methods and fields “field” is what C# calls a member variable Properties “look like fields, behave like methods” By convention, names are in UpperCamelCase Very basic example on next slide
Properties: simple example class Thing { // Private field (the “backing field”) private String name; // Public property public String Name     { get         {             return name;         } set  { // "value" is an automatic             // variable inside the setter name = value;         }     } } class Program { static void Main(string[] args)     { Thing t = new Thing();         // Use the setter t.Name = "Fred";         // Use the getter Console.WriteLine(t.Name);     } }
Properties So far, looks just like an over-complicated field So why bother?
Properties: advanced getter/setter class Thing { // Private field (the “backing field”) private String name; private static intrefCount = 0; // Public property public String Name     { get         {             returnname.ToUpper();         }         set  { name = value; refCount++;         }     } } Can hide implementation detail inside a property Hence “looks like a field, behaves like a method”
Properties: access modifiers class Thing { // Private field (the “backing field”) private String _name; // Public property public String Name     { get         {             return _name;         } private set  { _name = value;         }     } } Now only the class itself can modify the value Any object can get the value
Properties: getter only class Thing {     // Public property public intCurrentHour     { get         {             returnDateTime.Now.Hour;         }     } } In this case it doesn’t make sense to offer a setter Can also implement a setter but no getter Notice that Now and Hour are both properties too (of DateTime) – and Now is static!
Properties: even simpler example class Thing { // Private field (the “backing field”) private String _name; // Public property public String Name     { get         {             return _name;         } set  { _name = value;         }     } } class Thing { // If all you want is a simple     // getter/setter pair, no need for a     // backing field at all public String Name { get; set; } // As you might have guessed, access     // modifiers can be used public boolIsBusy { get; privateset; } }
Properties A really core feature of C# You’ll see them everywhere DateTime.Now String.Length etc. Get into the habit of using a property whenever you need a getter and/or setter Preferred to using GetValue(), SetValue() methods Never use public fields!
Interfaces Very similar to interfaces in Java Or M-classes (mixins) in Symbian Like a class, but all its members are implicitly abstract i.e. it does not provide any method implementations, only method signatures A class can only inherit from a single base class, but may implement multiple interfaces
foreach Simplified for loop syntax (familiar from Qt!) int[] myInts = new int[] { 1, 2, 3, 4, 5 }; foreach (intiinmyInts) { Console.WriteLine(i); } Works with built-in arrays, collection classes and any class implementing IEnumerable interface Stringimplements IEnumerable<char>
readonly For values that can only be assigned during construction class Thing {     private readonlyString name; privatereadonlyintage =42;// OK public Thing() {         name = "Fred";// Also OK } public void SomeMethod() {         name = "Julie";// Error } }
readonly & const C# also has the const keyword As in C++, used for constant values known at compile time Not identical to C++ const though Not used for method parameters Not used for method signatures
Parameter modifiers: ref No (explicit) pointers or references in C# In effect, all parameters are passed by reference But not quite... static void Main(string[] args) { String message = "I'm hot"; negate(message); Console.WriteLine(message); } static void negate(String s) {     s += "... NOT!"; } Result: > I'm hot
Parameter modifiers: ref Although param passing as efficient as “by reference”, effect is more like “by const reference” The ref keyword fixes this static void Main(string[] args) { String message = "I'm hot"; negate(ref message); Console.WriteLine(message); } static void negate(refString s) {     s += "... NOT!"; } Result: > I'm hot... NOT!
Parameter modifiers: out Like ref but must be assigned in the method static void Main(string[] args) { DateTime now; if (isAfternoon(out now)) { Console.WriteLine("Good afternoon, it is now " + now.TimeOfDay.ToString());     } else { Console.WriteLine("Please come back this afternoon.");     } } static boolisAfternoon(out DateTimecurrentTime) { currentTime = DateTime.Now; returncurrentTime.Hour >= 12; }
Delegates Delegates are how C# defines a dynamic interface between two methods Same goal as function pointers in C, or signals and slots in Qt Delegates are type-safe Consist of two parts: a delegate type and a delegate instance I can never remember the syntax for either! Keep a reference book handy… 
Delegates A delegate type looks like an (abstract) method declaration, preceded with the delegate keyword A delegate instance creates an instance of this type, supplying it with the name of a real method to attach to Example on next slide
Delegates // Delegate type (looks like an abstract method) delegate intTransform(intnumber); // The real method we're going to attach to the delegate static intDoubleIt(intnumber) {     return number * 2; } static void Main(string[] args) { // Create a delegate instance Transform transform;     // Attach it to a real method transform = DoubleIt;     // And now call it (via the delegate) intresult = transform(5); Console.WriteLine(result); } Result: > 10
Multicast delegates A delegate instance can have more than one real method attached to it Transform transform; transform += DoubleIt; transform += HalveIt; // etc. Now when we call transform(), all methods are called Called in the order in which they were added
Multicast delegates Methods can also be removed from a multicast delegate transform -= DoubleIt; You might start to see how delegates could be used to provide clean, decoupled UI event handling e.g. handling mouse click events But…
Multicast delegates: problems What happens if one object uses = instead of += when attaching its delegate method? All other objects’ delegate methods are detached! What if someone sets the delegate instance to null? Same problem: all delegate methods get detached What if someone calls the delegate directly? All the delegate methods are called, even though the event they’re interested in didn’t really happen
Events Events are just a special, restricted form of delegate Designed to prevent the problems listed on the previous slide Core part of C# UI event handling Controls have standard set of events you can attach handlers to (like signals in Qt), e.g.: myButton.Click += OnButtonClicked;
Advanced C# and .NET Generics Look and behave pretty much exactly like C++ templates Assemblies Basic unit of deployment in .NET Typically a single .EXE or .DLL Extra access modifier internal(in addition to public, protected and private) gives access to other classes within the same assembly Only really makes sense in a class library DLL
Further reading Reference documentation on MSDN:http://msdn.microsoft.com/en-us/library Microsoft’s Silverlight.net site:http://www.silverlight.net/ StackOverflow of course!http://stackoverflow.com/ C# in a Nutshell – highly recommended!http://oreilly.com/catalog/9780596800956/ Free MS Press eBook: ProgrammingWindows Phone 7http://www.charlespetzold.com/phone/

More Related Content

What's hot

Presentation refactoring large legacy applications
Presentation refactoring large legacy applications Presentation refactoring large legacy applications
Presentation refactoring large legacy applications
Jorge Capel Planells
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
Jan Rüegg
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
Eyob Lube
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Shuo Chen
 
DDL And DML
DDL And DMLDDL And DML
DDL And DML
pnp @in
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
Olve Maudal
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
Adeel Rasheed
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
Vishwa Mohan
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
Hawkman Academy
 
C++ How to program
C++ How to programC++ How to program
C++ How to program
Mohammad Golyani
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
Theo Jungeblut
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
Sumant Tambe
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and Variables
Intro C# Book
 
LLVM Register Allocation (2nd Version)
LLVM Register Allocation (2nd Version)LLVM Register Allocation (2nd Version)
LLVM Register Allocation (2nd Version)
Wang Hsiangkai
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning material
ArthyR3
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
Jyothishmathi Institute of Technology and Science Karimnagar
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 

What's hot (20)

Presentation refactoring large legacy applications
Presentation refactoring large legacy applications Presentation refactoring large legacy applications
Presentation refactoring large legacy applications
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
 
Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++Essentials of Multithreaded System Programming in C++
Essentials of Multithreaded System Programming in C++
 
DDL And DML
DDL And DMLDDL And DML
DDL And DML
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
C++ How to program
C++ How to programC++ How to program
C++ How to program
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Clean Code I - Best Practices
Clean Code I - Best PracticesClean Code I - Best Practices
Clean Code I - Best Practices
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
02. Primitive Data Types and Variables
02. Primitive Data Types and Variables02. Primitive Data Types and Variables
02. Primitive Data Types and Variables
 
LLVM Register Allocation (2nd Version)
LLVM Register Allocation (2nd Version)LLVM Register Allocation (2nd Version)
LLVM Register Allocation (2nd Version)
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning material
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
Loops c++
Loops c++Loops c++
Loops c++
 

Viewers also liked

C# interview
C# interviewC# interview
C# interview
Thomson Reuters
 
Abstraction in java
Abstraction in javaAbstraction in java
Abstraction in javasawarkar17
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
Haris Bin Zahid
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
Oum Saokosal
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objectsrahulsahay19
 
C++ classes
C++ classesC++ classes
C++ classes
imhammadali
 
difference between c c++ c#
difference between c c++ c#difference between c c++ c#
difference between c c++ c#
Sireesh K
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract Class
OUM SAOKOSAL
 

Viewers also liked (10)

C# interview
C# interviewC# interview
C# interview
 
Abstraction in java
Abstraction in javaAbstraction in java
Abstraction in java
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
 
C++ classes
C++ classesC++ classes
C++ classes
 
difference between c c++ c#
difference between c c++ c#difference between c c++ c#
difference between c c++ c#
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
Chapter 9 Abstract Class
Chapter 9 Abstract ClassChapter 9 Abstract Class
Chapter 9 Abstract Class
 

Similar to C# for C++ programmers

CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
frwebhelp
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
tarun308
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
HelpWithAssignment.com
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
HelpWithAssignment.com
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
Effecient javascript
Effecient javascriptEffecient javascript
Effecient javascript
mpnkhan
 
C++ idioms.pptx
C++ idioms.pptxC++ idioms.pptx
C++ idioms.pptx
Janani Anbarasan
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
georgebrock
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
Synapseindiappsdevelopment
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
rehan16091997
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
Sasha Goldshtein
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
R696
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy codeShriKant Vashishtha
 
Java Generics
Java GenericsJava Generics
Java Generics
jeslie
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
Michael Stal
 
golang_refcard.pdf
golang_refcard.pdfgolang_refcard.pdf
golang_refcard.pdf
Spam92
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_typesAbed Bukhari
 

Similar to C# for C++ programmers (20)

CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Effecient javascript
Effecient javascriptEffecient javascript
Effecient javascript
 
C++ idioms.pptx
C++ idioms.pptxC++ idioms.pptx
C++ idioms.pptx
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
1204csharp
1204csharp1204csharp
1204csharp
 
Oop2010 Scala Presentation Stal
Oop2010 Scala Presentation StalOop2010 Scala Presentation Stal
Oop2010 Scala Presentation Stal
 
golang_refcard.pdf
golang_refcard.pdfgolang_refcard.pdf
golang_refcard.pdf
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
 

Recently uploaded

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 

Recently uploaded (20)

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 

C# for C++ programmers

  • 1. C# for C++ Programmers A crash course
  • 2. C#: the basics Lots of similarities with C++ Object-oriented Classes, structs, enums Familiar basic types: int, double, bool,… Familiar keywords: for, while, if, else,… Similar syntax: curly braces { }, dot notation,… Exceptions: try and catch
  • 3. C#: the basics Actually much more similar to Java Everything lives in a class/struct (no globals) No pointers! (so no ->, * or & notation) Garbage collection: no delete! No header files No multiple inheritance Interfaces Static members accessed with . (not ::) In a nutshell: much easier than C++ 
  • 4. Hello, world! using System; // Everything's in a namespace namespace HelloWorldApp { // A simple class class Program { // A simple field: note we can instantiate it on the same line private static String helloMessage = "Hello, world!"; // Even Main() isn't global! static void Main(string[] args) { Console.WriteLine(helloMessage); } } }
  • 5. C# features Properties Interfaces The foreach keyword The readonly keyword Parameter modifiers: ref and out Delegates and events Instead of callbacks Generics Instead of templates
  • 6. Properties Class members, alongside methods and fields “field” is what C# calls a member variable Properties “look like fields, behave like methods” By convention, names are in UpperCamelCase Very basic example on next slide
  • 7. Properties: simple example class Thing { // Private field (the “backing field”) private String name; // Public property public String Name { get { return name; } set { // "value" is an automatic // variable inside the setter name = value; } } } class Program { static void Main(string[] args) { Thing t = new Thing(); // Use the setter t.Name = "Fred"; // Use the getter Console.WriteLine(t.Name); } }
  • 8. Properties So far, looks just like an over-complicated field So why bother?
  • 9. Properties: advanced getter/setter class Thing { // Private field (the “backing field”) private String name; private static intrefCount = 0; // Public property public String Name { get { returnname.ToUpper(); } set { name = value; refCount++; } } } Can hide implementation detail inside a property Hence “looks like a field, behaves like a method”
  • 10. Properties: access modifiers class Thing { // Private field (the “backing field”) private String _name; // Public property public String Name { get { return _name; } private set { _name = value; } } } Now only the class itself can modify the value Any object can get the value
  • 11. Properties: getter only class Thing { // Public property public intCurrentHour { get { returnDateTime.Now.Hour; } } } In this case it doesn’t make sense to offer a setter Can also implement a setter but no getter Notice that Now and Hour are both properties too (of DateTime) – and Now is static!
  • 12. Properties: even simpler example class Thing { // Private field (the “backing field”) private String _name; // Public property public String Name { get { return _name; } set { _name = value; } } } class Thing { // If all you want is a simple // getter/setter pair, no need for a // backing field at all public String Name { get; set; } // As you might have guessed, access // modifiers can be used public boolIsBusy { get; privateset; } }
  • 13. Properties A really core feature of C# You’ll see them everywhere DateTime.Now String.Length etc. Get into the habit of using a property whenever you need a getter and/or setter Preferred to using GetValue(), SetValue() methods Never use public fields!
  • 14. Interfaces Very similar to interfaces in Java Or M-classes (mixins) in Symbian Like a class, but all its members are implicitly abstract i.e. it does not provide any method implementations, only method signatures A class can only inherit from a single base class, but may implement multiple interfaces
  • 15. foreach Simplified for loop syntax (familiar from Qt!) int[] myInts = new int[] { 1, 2, 3, 4, 5 }; foreach (intiinmyInts) { Console.WriteLine(i); } Works with built-in arrays, collection classes and any class implementing IEnumerable interface Stringimplements IEnumerable<char>
  • 16. readonly For values that can only be assigned during construction class Thing { private readonlyString name; privatereadonlyintage =42;// OK public Thing() { name = "Fred";// Also OK } public void SomeMethod() { name = "Julie";// Error } }
  • 17. readonly & const C# also has the const keyword As in C++, used for constant values known at compile time Not identical to C++ const though Not used for method parameters Not used for method signatures
  • 18. Parameter modifiers: ref No (explicit) pointers or references in C# In effect, all parameters are passed by reference But not quite... static void Main(string[] args) { String message = "I'm hot"; negate(message); Console.WriteLine(message); } static void negate(String s) { s += "... NOT!"; } Result: > I'm hot
  • 19. Parameter modifiers: ref Although param passing as efficient as “by reference”, effect is more like “by const reference” The ref keyword fixes this static void Main(string[] args) { String message = "I'm hot"; negate(ref message); Console.WriteLine(message); } static void negate(refString s) { s += "... NOT!"; } Result: > I'm hot... NOT!
  • 20. Parameter modifiers: out Like ref but must be assigned in the method static void Main(string[] args) { DateTime now; if (isAfternoon(out now)) { Console.WriteLine("Good afternoon, it is now " + now.TimeOfDay.ToString()); } else { Console.WriteLine("Please come back this afternoon."); } } static boolisAfternoon(out DateTimecurrentTime) { currentTime = DateTime.Now; returncurrentTime.Hour >= 12; }
  • 21. Delegates Delegates are how C# defines a dynamic interface between two methods Same goal as function pointers in C, or signals and slots in Qt Delegates are type-safe Consist of two parts: a delegate type and a delegate instance I can never remember the syntax for either! Keep a reference book handy… 
  • 22. Delegates A delegate type looks like an (abstract) method declaration, preceded with the delegate keyword A delegate instance creates an instance of this type, supplying it with the name of a real method to attach to Example on next slide
  • 23. Delegates // Delegate type (looks like an abstract method) delegate intTransform(intnumber); // The real method we're going to attach to the delegate static intDoubleIt(intnumber) { return number * 2; } static void Main(string[] args) { // Create a delegate instance Transform transform; // Attach it to a real method transform = DoubleIt; // And now call it (via the delegate) intresult = transform(5); Console.WriteLine(result); } Result: > 10
  • 24. Multicast delegates A delegate instance can have more than one real method attached to it Transform transform; transform += DoubleIt; transform += HalveIt; // etc. Now when we call transform(), all methods are called Called in the order in which they were added
  • 25. Multicast delegates Methods can also be removed from a multicast delegate transform -= DoubleIt; You might start to see how delegates could be used to provide clean, decoupled UI event handling e.g. handling mouse click events But…
  • 26. Multicast delegates: problems What happens if one object uses = instead of += when attaching its delegate method? All other objects’ delegate methods are detached! What if someone sets the delegate instance to null? Same problem: all delegate methods get detached What if someone calls the delegate directly? All the delegate methods are called, even though the event they’re interested in didn’t really happen
  • 27. Events Events are just a special, restricted form of delegate Designed to prevent the problems listed on the previous slide Core part of C# UI event handling Controls have standard set of events you can attach handlers to (like signals in Qt), e.g.: myButton.Click += OnButtonClicked;
  • 28. Advanced C# and .NET Generics Look and behave pretty much exactly like C++ templates Assemblies Basic unit of deployment in .NET Typically a single .EXE or .DLL Extra access modifier internal(in addition to public, protected and private) gives access to other classes within the same assembly Only really makes sense in a class library DLL
  • 29. Further reading Reference documentation on MSDN:http://msdn.microsoft.com/en-us/library Microsoft’s Silverlight.net site:http://www.silverlight.net/ StackOverflow of course!http://stackoverflow.com/ C# in a Nutshell – highly recommended!http://oreilly.com/catalog/9780596800956/ Free MS Press eBook: ProgrammingWindows Phone 7http://www.charlespetzold.com/phone/