PDC 2008 Experience: Day 1: Session = "The Future of C#"
Co-Evolution of languages: When one feature or capability is added to one language, the feature or ability to have that feature will be placed in the other language.
This is a bit incomplete. I could not type as fast as slides move. I will update it when I get the slides.
C# 4.0: Dynamic Programming.
C# 4.0 Language Enhancements
- Dynamically Typed Objects
- Optional and Named Parameters
- Improved COM Interoperability
- Co and Contra variance
Dynamically Typed Objects
Dynamic Language Runtime
- Expression Trees
- Dynamic Dispatch
- Call Site caching
C# and VB.NET will have support for dynamic language. The Dynamic Runtime will provide the ability to talk to IronPython, Ruby, JavaScript and others.
1: dynamic calc = new Calculator();
2: int result = calc.Add(10, 20);
Andres took some Silverlight JavaScript methods and with search and replace made it into C# using the "dynamic" language runtime.
IDynamic Object
TODO: FIX Code; I could not type it fast enough.
1: using System.Dynamic;
2:
3: public class Bag: DynamicObject
4: {
5: Dictionary<string, object> items = new Dictionary,string, object>();
6: public override object GetMember(System.Scripting.ActionsGetMemberAction action, it)
7: {
8:
9: }
10:
11: }
12:
13: class Program
14: {
15: static void Main(string[] args)
16: {
17: dynamic x= new Bag();
18: x.ID = 12345;
19: x.Name = "Lawn Mower";
20: x.Price = 795;
21:
22: }
23: private static void WriteBag(dynamic x) {
24: Console.WriteLine ();
25: }
26: }
Optional and Named Parameters
Optional Parameters
1: public StreamReader OpenTextFile(
2: string path;
3: Encoding = null;
4: bool detectEncoding = true;
5: int bufferSize = 1024)
Named Arguments
1: OpenTextFile("foo.txt", Encoding.UTF8, buffersize: 4096);
Improved COM Interoperability
- Automatic object -> dynamic mapping
- Optional and named parameters
- Indexed Properties
- Optional ref "modifier
- Interop type embedding ("NoPIA")
Interop calls do not need to be cast.
TODO: GET Excel Interop sample.
Co and Contra variance
1: string[] strings = GetStringArray();
2: Process(strings);
3:
4: ...
5:
6: void Process (string[] input) {...}
can be done using the new C# 4.0 support for safe co- and contra-variance
1: // Covariant
2: public interface IEnumerable<out T>
3: {
4: IEnumerable<T> GetEnumerator();
5: }
6:
7: // Can be treated a less derived
8: public interface IEnumerable<out t>
9: {
10: T Current get;}
11: bool MoveNext();
12: }
Variance in C#
- Support for interface and delegate types
- "statically checked definitions"
- TODO: Add missing elements
Coming Soon to C#
A new compiler, compiler as a service... Source files -> compiler -> .NET Assembly
This will allow applications to hook into the compiling service for testing, checks, and more.