C# Language Design: Peter Hallam Software Design Engineer C# Compiler Microsoft Corporation
C# Language Design: Peter Hallam Software Design Engineer C# Compiler Microsoft Corporation
Peter Hallam
Software Design Engineer
C# Compiler
Microsoft Corporation
Overview
Introduction to C#
Design Problems
Future Directions
Questions
Hello World
using System;
class Hello
{
static void Main()
{
Console.WriteLine("Hello, world!");
}
}
C# Program Structure
Namespaces
Type declarations
Members
Organization
Predefined Types
C# predefined types
The root
Logical
Signed
Unsigned
Floating-point
Textual
object
bool
sbyte, short, int, long
byte, ushort, uint, ulong
float, double, decimal
char, string
C# Classes
Single inheritance
Can implement multiple interfaces
Members
Member access
Interfaces
Statements and
Expressions
// error
void Foo() {
if (i = 1) // error
...
C# Design Goals
Problem:
How to Unify the Type System
Unification enables:
MemoryStream
object
Hashtable
int
double
FileStream
How to Unify:
A traditional approach (SmallTalk)
Performance implications
How to Unify:
Good performance
Cant convert int to Object the
primitive types are in a separate world
Requires special wrapper classes (e.g.,
Integer) to wrap a primitive type so
that it works in the Object world.
Not extensible the set of primitive types
is fixed.
How to Unify:
C# Approach
Unification
Everything is an object
123
int
123
123
} Boxing
}
Unboxing
User-Defined Types
Structs (value)
C# Type System
Problem:
Additional Declarative Information
Other Approaches
C# Solution: Attributes
Attributes - Examples
public class OrderProcessor {
[WebMethod]
public void SubmitOrder(PurchaseOrder order) {...}
}
public class PurchaseOrder
[XmlElement("shipTo")]
[XmlElement("billTo")]
[XmlElement("items")]
[XmlAttribute("date")]
}
{
public
public
public
public
Address ShipTo;
Address BillTo;
Item[] Items;
DateTime OrderDate;
Attributes
Attributes
Extensible
Type-safe
Extensively used in .NET Frameworks
Creating an Attribute
Querying Attributes
Problem : Versioning
Versioning Problems
Versioning: C# solution
Versioning Example
class Base
// version 2
1
{
} public virtual void Foo() {
Console.WriteLine("Base.Foo");
}
}
Interface Implementation
interface I {
void foo();
}
interface J {
void foo();
}
class C: I, J {
void I.foo() { /* do one thing */
}
void J.foo() { /* do another thing */ }
}
foreach Statement
Iteration of arrays
public static void Main(string[] args) {
foreach (string s in args) Console.WriteLine(s);
}
Extending foreach
IEnumerable
interface IEnumerable {
IEnumerator GetEnumerator();
}
interface IEnumerator {
bool MoveNext();
object Current { get; }
}
Extending foreach
IEnumerable
foreach (int v in collection) {
// use element v
}
(IEnumerable) ie = (IEnumerable)
collection;
IEnumerator e = ie.GetEnumerator();
while (e.MoveNext()) {
int v = (int) e.Current;
foreach
foreach - Summary
Some Complexity
Extensible
User Model
interface
pattern
Performance
Future Directions
Generics - Prototype
Don Syme
Andrew Kennedy
Generics - Prototype
class Stack<T>
{
T[ ] data;
void Push(T top) { }
T Pop() { }
}
Stack<string> ss = new Stack<string>;
ss.Push(Hello);
Stack<int> si = new Stack<int>;
ss.Push(4);
Type Erasure
No VM modifications
Compile time type checking
No instantiations on primitive types
Execution Speed Casts
Good Code Size
Type Identity Problems
Runtime Experience