Download - A C# Primer
Obligatory Strong Start
• “C# still on track to become number 1 [programming language] within 2 years time”
• Tremendous Momentum:– DirectX API– Compact devices– Next version of Office– Platform for XML– If you’ve used Visual
Studio…
What’s on tap today
Part I:Language Evolution(C, C++, Java, C#)
Part II:About C#
Pros and Cons
A closer look at speed
Execution
Development
Part III:Proof by Example
Ching and collaboration possibilities
• In Summary: Why would you consider using C# for your next project?
Disclaimer
• Haven’t traditionally been a huge Microsoft fan
• But, when they’re right…
• So, if you can, put aside biases, religious attachment to (or disdain for) an OS, etc.
Part I: Language Evolution: C
• Intimate with hardware, therefore very fast language
• Primarily used primitive types for working with data
• You handle all memory allocation and deallocation
#include <stdio.h>
void main(void) { printf("Hello world!"); }
Image: Kernigan, Ritchie et al, C Programming Language
class Greeter {public:
virtual void ExtendGreeting() = 0;};
Part I: Language Evolution: C++
• Superset of C• Introduction of
objects and object-oriented programming to C
• Inheritance, etc.• A very powerful
language– the power comes at a
cost of complexity
class EnglishGreeter : public Greeter {
public:
void ExtendGreeting() {
printf("Hello!");
}
};
class FrenchGreeter : public Greeter {public:
void ExtendGreeting() {printf("Bonjour!");
}};
int _tmain(int argc, _TCHAR* argv[]) {Greeter *myGreeter = new FrenchGreeter();myGreeter->ExtendGreeting();delete myGreeter;return 0;
}
Image: Stroustrup, The C++ Programming Language
Part I: Language Evolution: Java (1/3)
• Purely object-oriented• Handles all memory
deallocation – “garbage collection”
• (Geek alert) “Elimination” of pointers
• Polished set of libraries (API) available on all platforms
public interface Greeter {public void extendGreeting();
}
public class EnglishGreeter implements Greeter {public void extendGreeting() {
System.out.println("Hello!");}
}
public class GraciousApplication {public static void main(String[] args) {
new EnglishGreeter().extendGreeting();}
}
Part I: Language Evolution: Java (2/3)
• Possible to interact with “native” C/C++ code.
• Very useful for:– Integrating “legacy” code– Speed: optimizing a critical bit of code
• But, difficult to debug, for example:– Primitive type size can change between
platforms– No common debugging environment
Part I: Language Evolution: Java (3/3)
• Garbage collection and cross-platform made possible by the Java Virtual Machine (JVM)– Compiles to interpreted
bytecode instead of machine instructions
• Multiple benefits:• Cross-platform• Garbage collection
• Downsides:• Abstraction between you and
hardware, therefore less deep control
• A little slower, but “Just-In-Time” (JIT) compiling helps Operating System
JVM (Virtual Machine)
Your Java app Java API
Part II: Enter C#
Operating System
CLR (Common Language Runtime)
Your C# app .NET FrameworkClass Libraries
• A hybrid language incorporating features from C++ and Java (and Smalltalk, and…)
• Looks a lot like Java, with keywords from C/C++
• Object oriented• Has a virtual machine,
and garbage collection, among other Java parallels
Comparative Language Studies
C C++ Java C#
Object-Oriented No Yes, partially
Yes, fully
Yes, fully
Compiled? Yes Yes If JIT Yes, JIT
Virtual Machine with garbage collection?
No No JVM CLR
Does programmer handle dealloc., pointers?
Yes Yes No In ‘Un-managed’ Code
C# Pros and Cons
• Biggest Cons– Compared to C++: Simpler programming model
comes at cost of some geek-power– Compared to Java: Currently, no CLR implementation
for Mac• (but, a word on cross-platform: “Mono,” for Linux, in the pipe)
• Biggest Pros– Compared to Java: Execution Speed
• Java JVM code is designed to be interpreted• C# CLR code is always compiled
(despite common misconception)
– Compared to C, C++, Java: Development Speed
The Need for Execution Speed
• “98% of the speed, for 50% of the code”– That’s the Managed (C#) DirectX 9.0 stat
• Well, Dolphin demo:565 lines C# / 738 lines C++ = 76% of the code, but who’s counting.
– DEMO: • Dolphin 37 fps C++, 36 fps C#• PointSprites 37 fps C++, 37 fps C#
• Something hard to quantify - the smooth, smooth lovin’ of the CLR’s garbage collector.– StillLife’s 1600 particles/s, smooth, is unthinkable
using existing Java JVM implementations
Development Speed (because our time is worth it)™
• C# language design theme: making the intent explicit in situations which can easily lead to subtle bugs.– (geek alert) e.g. ‘Versioning’ keywords (override, new, virtual)
• Language features not available to Java– Delegates– Properties– Type system
• Rich capacity for interop with native code…3 different methods:– 1. "unsafe" (unverifiable) code blocks provide access to pointers– 2. “Platform invoke" - access other DLLs (Java's only option)– 3. COM interop - tap into old COM stuff
Development Environment:Language doesn’t exist in a vacuum!• The .NET Framework
Class Library• New set of very polished
libraries• Accessible from C++, C#,
Visual Basic.NET• C#’s objects and
interfaces perfectly tailored
• Common development environment hand-holds you
• Comments• step into native code. etc.
(DEMO)
Deployment
• And, importantly, the deployment method:– You make .EXEs and .DLLs.
• What does someone else need to run a C# .exe? – Just the free .NET Framework
How to demo Development Speed?
• Ching (Dec 2001 - present)– libraries for signal processing, as
well as serial port communication, audio, video...
– the foundation of all the projects I've built this year
• Sponge (Half a week)– uses UDP to poll for computers on
the LAN, considers their processor speed and available processing power, and distributes signal processing tasks between them. has the capacity to deploy signal processors to the other machines first
• Still Life (<6 days)– built on an early version of Ching.
Particle system, video input, video processing effects all in C#, using OpenGL
How to demo interoperability, powerful libraries, collaboration?
• Contributions to Breathing Space– Great demo of interop with
existing software in C++. – Daragh has built and worked with
an extensive amount of graphics code in C++. Used remote signal processing. He just calls a DLL’s get function
• Heartheard (crappy working title) (2 days)
– Exposed DirectSound3D and made an application with 3D audio and effects, which maps heart rate and breathing to audio.
• Eva's Anthropos robot– Uses vision tracking modified from
Still Life, using Signal Processing Network from Ching (which leads nicely to...)
Collaboration?
• If there is interest, I would manage the source tree more carefully
• Could it lead to more complex projects?
• 90% of the codebase compiled for the CompactFramework… how about that?
Summary: Why would you use C# for your next project?
• In a word: productivity– Just as Java learned from C++…– Nominal speed hit, tremendous productivity
boost– Possibility of making use of existing Ching
and MiniChing libraries
Some props, and where to go next
• Thanks– Eric Gunnerson and the GotDotNet team– MindGames, for letting me get away with this– Eva for being the world’s most patient guinea pig– You, for expressing interest…
• Places to learn more– Jesse Liberty’s Programming C#, O’Reilly– http://www.gotdotnet.com (semi-official site)– http://www.csharphelp.com (community site)– news://microsoft.public.dotnet.languages.csharp– The third floor
• Thoughts?