This document summarizes the history of C# and .NET Framework versions from Visual Studio .NET Rainer in 2002 to Visual Studio 2010 Dev10. It covers features introduced in each new version of C# and .NET Framework including generics, LINQ, lambda expressions, extension methods, and more. It also briefly discusses the Reactive Extensions for .NET Framework 3.5 which added asynchronous programming capabilities using an observer pattern.
5. Agenda
Visual Studio .NET Rainer
.NET Framework 1.0 C# 1.0
Visual Studio .NET 2003 Everett
.NET Framework 1.1, C# 1.2
Visual Studio 2005 Whidbey
.NET Framework 2.0, C# 2,0
.NET Framework 3.0 WinFX
Visual Studio 2008 Orcas
.NET Framework 3.5, C# 3.0
Visual Studio 2010 Dev10
.NET Framework 4.0, C# 4.0
Reactive Extensions for .NET Framework 3.5 SP1
6. Visual Studio .NET Rainer
.NET Framework 1.0 / C# 1.0
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
7. C# 1.0 – Managed Code
Common Type System
Classes
Structures
Enumerations
Interfaces
Delegates
Attributes
Directives
Visual Studio® .NET
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
10. Field
// C#
const int constant = 0;
readonly int readOnlyField;
// C++
static const int constant = 0;
const int readOnlyField;
mutable int mutableField;
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
11. Field (cont.)
Field ≠ variable
default(T) if not initialized
• null if reference type
• 0 if value type
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
12. Method
Member function with CV-qualifier
class Object
{
public:
String ToString() const
{
return const_cast<Object*>(this)
->ToString();
}
virtual String ToString();
};
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
13. Method (cont.)
// C#
void CallByRef(ref int value);
bool TryGetValue(out int value);
// C++
void CallByRef(int& value);
bool TryGetValue(/*out*/int& value);
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
14. Property
Get Method/Set Method ≠ reference to type
// C#
variable = obj.PropertyName;
obj.PropertyName = value
// C++
variable = obj->get_PropertyName();
obj->setPropertyName(value);
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
15. Property (cont.)
Indexed property ≠ index operator []
int this[int index]
{
get { return array[index]; }
set { array[index] = value; }
}
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
16. Property (cont.)
Implementation: lazy evaluation class
class Property<typename T>
{
operator T() const;
Property& operator=(const T&
value);
Property& operator=(const
Property& value);
};
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
17. Property (cont.)
VC 7.1 or higher: property extended storage-
class modifier
virtual TypeName get_PropertyName();
virtual void set_PropertyName(const
TypeName& value);
__declspec(property(get=get_Property
Name, put=set_PropertyName))
TypeName PropertyName;
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
18. Event
Multicast delegate
// C#
obj.EventName += someDelegate;
obj.EventName -= someDelegate;
if (obj.EventName != null) obj.EventName();
// C++
obj->add_EventName(someDelegate);
obj->remove_EventName(someDelegate);
obj->raise_EventName();
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
20. Classes (cont.)
Prevent R6025 – Pure virtual function call
__forceinline void Release()
{
if (--referenceCounter == 0)
{
Finalize();
delete this;
}
}
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
21. Classes (cont.)
Type initializer a.k.a. static constructor
namespace
{
class Static : … ;
Pointer<Static> _static;
}
…
if (_static == NULL)
{
_static.CompareExchange(Pointer<Static>(
new Static), Pointer<Static>(NULL));
}
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
22. Classes (cont.)
Sealed class
template <typename T>
class Sealed
{
private:
friend typename T;
Sealed() { }
~Sealed() { }
};
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
23. Structures
Lightweight – Value types
Default constructor / finalizer
• NEVER let contain unmanaged resources
Expert only
• Box/Unbox cost
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
24. Enumerations
Value types
string[] Enum.GetNames()
TEnum[] Enum.GetValues()
TEnum Enum.Parse(string s)
bool Enum.TryParse(
string s, out TEnum @enum)
TEnum TEnum.MaxValue { get; }
TEnum TEnum.MinValue { get; }
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
26. Delegates
Closures or managed functors
Calling conventions:
• cdecl
• stdcall
• fastcall
• thiscall
• function object
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
27. Delegates (cont.)
#define DEFINE_DELEGATE(TPARAMS, PARAMS,
ARGS)
template <typename TResult TPARAMS>
class delegate<TResult(PARAMS)> …
#define DELEGATE_TPARAMS , typename T1
#define DELEGATE_PARAMS T1 arg1
#define DELEGATE_ARGS arg1
DEFINE_DELEGATE(DELEGATE_TPARAMS,
DELEGATE_PARAMS, DELEGATE_ARGS);
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
28. Attributes
template <typename T>
class CustomAttribute
{
public:
static const int get_Value() { return
value; }
private:
static int value;
};
#define CUSTOM_ATTRIBUTE(Type, _v)
int CustomAttribute<Type>::value = _v
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
29. Directives
using directive (C#) ≈ using directive (C++)
Assemblies are referenced.
#include directive (C++)
package / import declarations (Java)
#define directive
defines a symbol
assigns a value to a symbol
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
30. Visual Studio .NET 2003 Everett
.NET Framework 1.1 / C# 1.2
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
31. C# 1.2 – Bug Fix
IDisposable interface
• IEnumerator interface
foreach (object obj in collection)
// TEnumerator enumerator =
// collection.GetEnumerator();
{
}
// if (enumerator is IDisposable)
// enumerator.Dispose();
Visual Studio® .NET 2003
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
32. Visual Studio 2005 Whidbey
.NET Framework 2.0 / C# 2.0
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
33. C# 2.0 – Generic
Generics
Iterators
Partial Classes
Nullable Types
Anonymous Methods
Static Classes
Property Accessor Accessibilities
Delegates
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
Visual Studio® 2005
34. Generics
System.Collections.Generic
• List<T> std::vector<T>
• Queue<T> std::queue<T>
• Stack<T> std::stack<T>
• LinkedList<T> std::list<T>
• Dictionary<TKey, TValue>
std::map<Key, T>
• HashSet<T> std::set<T>
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
37. Nullable Types
Nullable<T> where T : struct
Coalescing operator ??
int? nullable = null;
int value = nullable ??
default(int);
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
38. Anonymous Methods
// C# 2.0
Func<Type, bool> predicate =
// nested type in C# 1.2
delegate(Type value)
{
return value == default(Type);
};
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
39. Static Classes
Classes which are both abstract and sealed.
public static class Helper
{
public static int GetHash(object obj)
{
return obj.GetHashCode();
}
}
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
40. Property Accessor
Accessibilities
// C# 1.2
public Type PropertyName
{ get { return field; } }
internal void SetPropertyName(Type value)
{ fieldName = value; }
// C# 2.0
public Type PropertyName
{
get { return fieldName; }
internal set { fieldName = value; }
}
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
41. Delegates
string Covariance() …
Func<object> function =
new Func<object>(Covariance);
void Contravariance(object arg);
Action<string> action =
new Action<string>(
Contravariance);
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
42. Delegates (cont.)
// C# 1.2
Action action =
new Action(obj.MethodName);
// C# 2.0
Action action = obj.MethodName;
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
43. Visual Studio 2008 Orcas
.NET Framework 3.5 / C# 3.0
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
44. C# 3.0 – LINQ
Implicitly Type Local Variables and Arrays
Object Initializers
Collection Initializers
Extension Methods
Anonymous Types
Lambda Expressions
Query Keywords
Auto-Implemented Properties
Partial Method Definitions
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
Visual Studio® 2008
45. Implicitly Type Local
Variables and Arrays
// C# 2.0
Dictionary<string, object> dict =
new Dictionary<string, object>();
Type[] types = new Type[]
{ typeof(int) };
// C# 3.0
var dict =
new Dictionary<string, object>();
var types = new[] { typeof(int) };
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
46. Object Initializers
// C# 2.0
SomeKeyValue pair = new SomeKeyValue();
value.Name = “anonymous”;
value.Value = null;
// C# 3.0
var value = new SomeKeyValue()
{
Name = “anonymous”,
Value = null,
};
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
47. Collection Initializers
// C# 2.0
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
// C# 3.0
var list = new List<int> { 1, 2 };
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
48. Extension Methods
static class ComponentExtensions
{
static void DoSomething(
this IComponent component,
T arg) { … }
}
ComponentExtensions.Do(component, arg);
component.DoSomething(arg);
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
49. Anonymous Types
foreach (var value in from row in table
select new
{
Type = row.GetType(),
Hash = row.GetHashCode(),
})
{
Console.WriteLine(“{{ Type = {0}, Hash = {1} }}”,
value.Type, value.Hash);
Console.WriteLine(value);
}
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
50. Lambda Expressions
// C# 2.0
Func<Type, bool> predicate =
delegate(Type value)
{
return value == default(Type);
};
// C# 3.0
Func<Type, bool> predicate =
value => value == default(Type);
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
51. Query Keywords
var q = from c in categories
join p in products
on c.ID equals p.CID
orderby c.ID,
p.Price descending
group p.Price by c.ID into g
let cid = g.Key
where Predicate(cid)
select g.Average();
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
52. Query Keywords (cont.)
var q = categories
.Join(products,
c => c.ID, p => p.CID,
(c, p) =>
new { c = c, p = p })
.OrderBy(_ => _.c.ID)
.ThenByDescending(_ => _.p.Price)
.GroupBy(_ => _.c.ID, _.p.Price)
.Select(g =>
new { g = g, cid = g.Key })
.Where(_ => Predicate(_.cid))
.Select(_ => _.g.Average());
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
53. Auto-Implemented
Properties
// C# 2.0
public Type PropertyName
{
get { return fieldName; }
internal set { fieldName = value; }
}
// C# 3.0
public Type PropertyName
{ get; internal set; }
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
54. Partial Method
Definitions
// Generated.Designer.cs
partial class Generated
{
Generated() { OnInitialized(); }
partial void OnInitialized();
}
// Generated.cs
partial class Generated
{
partial void OnInitialized() { … }
}
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
55. Reactive Extensions for .NET Framework 3.5 SP1
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
More LINQ with System.Interactive – Getting Started by Bart De Smet
59. Observer Design Pattern
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx
More LINQ with System.Interactive – Getting Started by Bart De Smet
60. Observer Design Pattern
(Further reading)
Duality of IEnumerable<T> / IObservable<T>
Reactive Framework Finally Explained
More LINQ with System.Interactive series
VS .NET VS .NET 2003 VS 2005 VS 2008 Rx