Title: Visual C
1Visual C 2005Language Enhancements
2C Language Enhancements
- Generics
- Anonymous methods
- Nullable types
- Iterators
- Partial types
- and many more
- 100 backwards compatible
3Generics
public class List private object
elements private int count public void
Add(object element) if (count
elements.Length) Resize(count 2)
elementscount element public
object thisint index get return
elementsindex set elementsindex
value public int Count get
return count
public class ListltTgt private T elements
private int count public void Add(T
element) if (count elements.Length)
Resize(count 2) elementscount
element public T thisint index
get return elementsindex set
elementsindex value public int
Count get return count
List intList new List() intList.Add(1) intLis
t.Add(2) intList.Add("Three") int i
(int)intList0
List intList new List() intList.Add(1)
// Argument is boxed intList.Add(2) //
Argument is boxed intList.Add("Three") //
Should be an error int i (int)intList0 //
Cast required
Listltintgt intList new Listltintgt() intList.Add(
1) // No boxing intList.Add(2) // No
boxing intList.Add("Three") // Compile-time
error int i intList0 // No cast required
4Generics
- Why generics?
- Type checking, no boxing, no downcasts
- Reduced code bloat (typed collections)
- How are C generics implemented?
- Instantiated at run-time, not compile-time
- Checked at declaration, not instantiation
- Work for both reference and value types
- Complete run-time type information
5Generics
- Type parameters can be applied to
- Class, struct, interface, delegate types
class DictionaryltK,Vgt ... struct
HashBucketltK,Vgt ... interface IComparerltTgt
... delegate R FunctionltA,Rgt(A arg)
Dictionaryltstring,Customergt customerLookupTable
Dictionaryltstring,ListltOrdergtgt orderLookupTable
Dictionaryltstring,intgt wordCount
6Generics
- Type parameters can be applied to
- Class, struct, interface, delegate types
- Methods
class Utils public static T
CreateArrayltTgt(int size) return new
Tsize public static void
SortArrayltTgt(T array) ...
string names Utils.CreateArrayltstringgt(10) na
mes0 "Jones" ... Utils.SortArray(names)
7Generics
- Type parameters can be applied to
- Class, struct, interface, delegate types
- Methods
- Type parameters can have constraints
class DictionaryltK,Vgt public void Add(K key,
V value) ... if (((IComparable)key).
CompareTo(x) 0) ... ...
class DictionaryltK,Vgt where K IComparable
public void Add(K key, V value) ...
if (key.CompareTo(x) 0) ... ...
class DictionaryltK,Vgt IDictionaryltK,Vgt where
K IComparableltKgt where V IKeyProviderltKgt,
IPersistable, new() public void Add(K key, V
value) ...
8Generics
- Zero or one primary constraint
- Actual class, class, or struct
- Zero or more secondary constraints
- Interface or type parameter
- Zero or one constructor constraint
- new()
class LinkltTgt where T class ... class
NullableltTgt where T struct ... class
RelationltT,Ugt where T class where U T ...
9Generics
ListltTgt DictionaryltK,Vgt SortedDictionaryltK,Vgt Stac
kltTgt QueueltTgt
- Collection classes
- Collection interfaces
- Collection base classes
- Utility classes
- Reflection
IListltTgt IDictionaryltK,Vgt ICollectionltTgt IEnumerab
leltTgt IEnumeratorltTgt IComparableltTgt IComparerltTgt
CollectionltTgt KeyedCollectionltTgt ReadOnlyCollectio
nltTgt
NullableltTgt EventHandlerltTgt ComparerltTgt
10Anonymous Methods
class MyForm Form ListBox listBox
TextBox textBox Button addButton public
MyForm() listBox new ListBox(...)
textBox new TextBox(...) addButton
new Button(...) addButton.Click new
EventHandler(AddClick) void
AddClick(object sender, EventArgs e)
listBox.Items.Add(textBox.Text)
class MyForm Form ListBox listBox
TextBox textBox Button addButton public
MyForm() listBox new ListBox(...)
textBox new TextBox(...) addButton
new Button(...) addButton.Click
delegate listBox.Items.Add(textBox.Text
)
11Anonymous Methods
- Allows code block in place of delegate
- Delegate type automatically inferred
- Code block can be parameterless
- Or code block can have parameters
- In either case, return types must match
button.Click delegate MessageBox.Show("Hello"
)
button.Click delegate(object sender, EventArgs
e) MessageBox.Show(((Button)sender).Text)
12Anonymous Methods
- Method group conversions
- Delegate type inferred when possible
using System using System.Threading class
Program static void Work() ...
static void Main() Thread t new
Thread(new ThreadStart(Work))
t.Start()
using System using System.Threading class
Program static void Work() ...
static void Main() Thread t new
Thread(Work) t.Start()
13Nullable Types
- System.NullableltTgt
- Provides nullability for any value type
- Struct that combines a T and a bool
public struct NullableltTgt where T struct
public Nullable(T value) ... public T Value
get ... public bool HasValue get ...
...
Nullableltintgt x new Nullableltintgt(123) ... if
(x.HasValue) Console.WriteLine(x.Value)
14Nullable Types
- T? same as System.NullableltTgt
- null literal conversions
- Nullable conversions
int? x 123 double? y 1.25
int? x null double? y null
int i 123 int? x i // int --gt int?double?
y x // int? --gt double? int? z (int?)y //
double? --gt int? int j (int)z // int? --gt int
15Nullable Types
- Lifted conversions and operators
- Comparison operators
- Null coalescing operator
int? x GetNullableInt() int? y
GetNullableInt() int? z x y
int? x GetNullableInt() if (x null)
Console.WriteLine("x is null") if (x lt 0)
Console.WriteLine("x less than zero")
int? x GetNullableInt() int i x ?? 0
16Iterators
- foreach relies on enumerator pattern
- GetEnumerator() method
- foreach makes enumerating easy
- But enumerators are hard to write!
foreach (object obj in list)
DoSomething(obj)
Enumerator e list.GetEnumerator() while
(e.MoveNext()) object obj e.Current
DoSomething(obj)
17Iterators
public class ListEnumerator IEnumerator
List list int index internal
ListEnumerator(List list) this.list
list index -1 public bool
MoveNext() int i index 1 if (i
gt list.count) return false index i
return true public object Current
get return list.elementsindex
public class List internal object
elements internal int count public
IEnumerator GetEnumerator() return new
ListEnumerator(this)
public class List internal object
elements internal int count public
IEnumerator GetEnumerator() for (int i
0 i lt count i) yield return
elementsi
18Iterators
public IEnumerator GetEnumerator() return
new __Enumerator(this) private class
__Enumerator IEnumerator object current
int state public bool MoveNext()
switch (state) case 0
current "Hello" state 1
return true case 1
current "World" state 2
return true default
return false public object
Current get return current
- Method that incrementally computes and returns a
sequence of values - yield return and yield break
- Must return IEnumerator or IEnumerable
public class Test public IEnumerator
GetEnumerator() yield return "Hello"
yield return "World"
19Iterators
public class ListltTgt public IEnumeratorltTgt
GetEnumerator() for (int i 0 i lt
count i) yield return elementsi
public IEnumerableltTgt Descending()
for (int i count - 1 i gt 0 i--)
yield return elementsi public
IEnumerableltTgt Subrange(int index, int n)
for (int i 0 i lt n i) yield return
elementsindex i
ListltItemgt items GetItemList() foreach (Item x
in items) ... foreach (Item x in
items.Descending()) ... foreach (Item x in
Items.Subrange(10, 20)) ...
20Partial Types
public partial class Customer private int
id private string name private string
address private ListltOrdersgt orders
public class Customer private int id
private string name private string address
private ListltOrdersgt orders public void
SubmitOrder(Order order)
orders.Add(order) public bool
HasOutstandingOrders() return
orders.Count gt 0
public partial class Customer public void
SubmitOrder(Order order)
orders.Add(order) public bool
HasOutstandingOrders() return
orders.Count gt 0
21Static Classes
- Only static members
- Cannot be used as type of variable, parameter,
field, property, - Examples include System.Console,
System.Environment
public static class Math public static
double Sin(double x) ... public static
double Cos(double x) ... ...
22Property Accessors
- Different accessor accessibility
- One accessor can be restricted further
- Typically set more restricted
public class Customer private string id
public string CustomerId get return id
internal set id value
23External Aliases
- Enables use of identically named types in
different assemblies
namespace Stuff public class Utils
public static void F() ...
foo.dll
extern alias Foo extern alias Bar class
Program static void Main()
Foo.Stuff.Utils.F() Bar.Stuff.Utils.F()
namespace Stuff public class Utils
public static void F() ...
bar.dll
C\gtcsc /rFoofoo.dll /rBarbar.dll test.cs
24Namespace Alias Qualifiers
- Enables more version resilient code
- AB looks up A only as alias
- globalX looks up in global namespace
using IO System.IO class Program static
void Main() IOStream s new
IOFile.OpenRead("foo.txt")
globalSystem.Console.WriteLine("Hello")
25Inline Warning Control
using System class Program Obsolete
static void Foo() static void Main()
pragma warning disable 612
Foo() pragma warning restore 612
26Fixed Size Buffers
- C style arrays in unsafe code
public struct OFSTRUCT public byte cBytes
public byte fFixedDisk public short
nErrCode private int Reserved public
fixed char szPathName128
27Want to know more?
http//msdn.microsoft.com/vcsharp/language