C# 2.0 Language Features — Brown Bag Lunch Transcript

Download Example Code

  1. Introduction
  2. What will be covered
    1. A lot of language features were introduced in the C# 2.0.
      1. Partial Classes
      2. Generics
      3. Static classes
      4. New Iterator and the Yield Statement
      5. Covariance and Contravariance for signatures of delegates.
      6. Accesibility of property accessors can be set independently.
      7. Nullable value types
    2. I will be covering
      1. Generics
      2. Delegates
      3. Iterators and Yield Statement
  3. Generics
    1. Generics allow you to create type safe data structures without committing to a data type
      1. Code Sample One – Wrong way
        1. Here is typical array list, I want to store a collection of dogs.
        2. What is to stop someone from adding a cat to my list?
        3. Nothing since this is not type safe the compiler will throw no error.
        4. But when we run it, it will blow up.
      2. Code Sample One – Generic Way
        1. Here is a similar list but instead we are using generics.
        2. Notice the angle brackets this is where we pass in the type.
        3. A list is merely a generic version of a list collection that takes a type T as an argument.
    2. Performance
      1. When you use value types you have to box them in order to store them and unbox them when they are popped off of the stack.
        1. This leads to a performance penalty and increased pressure on the garbage collector.
        2. Even if you use reference types you have to cast to and from an object, which incurs a performance penalty.
        3. When using generics for value types expect to see a 200% performance gain, when using for reference type expect to see a 100% performance gain.
      2. Using the type argument
        1. Code Sample Two
      3. Being lazy with Aliasing
        1. Modify Code Sample Two
      4. Constraints in the Generic world.
        1. Inheritance Constraint
          1. Code Sample Three
        2. You can also use constraints to enforce interface implementations and constructors.
  4. Delegates
    1. What is a delegate
      1. The delegate keyword is used to declare a reference type that can be used to encapsulate a named or anomymous method. Once a delegate is assigned a method, it behaves exactly like any other method, with parameters and a return value.
        1. Example One
      2. Delgates work great for callback methods.
        1. Example Two
        2. An example of this would be to define a method to transform a DataRow into an object like an account.
      3. Multicasting
        1. In other words you can assign multiple methods to one method.
        2. Example Three
    2. Fun with big words
      1. Covariance and Contravariance
        1. Provide a degree of flexibility when matching method signatures with delegate types.
        2. Covariance permits a method to have a more derived return type that what is defined in the delegate.
        3. Example One
        4. Contravariance permits a method with a parameter types that are less derived than in the delegate type.
    3. Delegates and Generics
      1. A can define its own type parameters.
      2. Example Four
    4. When to use delegates
      1. When to use delegates instead of interfaces.
        1. Both allow a class designer to separate type declarations and implementation. A given interface can be inherited and implemented by any class, a delegate can be created method on any class as long as the signatures fit. An interface reference or a delegate can be used by an object with no knowledge of the class the implements the interface or delegate method. They seem pretty similar when should.
          1. Use a delegate when an eventing pattern is used.
          2. You want to encapsulate a static method.
          3. You don’t need to access other properties, methods or interfaces.
          4. A class may need more than one implementation of the method.
          5. http://msdn2.microsoft.com/en-us/library/ms173173(VS.80).aspx
  5. Iterators and the Yield Statement
    1. In 1.1 you can iterate over data structures such as arrays and collections using a foreach loop.
    2. You can use any custom data collection in the foreach as long as it implements a GetEnumerator method that returns a IEnumerator interface.
    3. ExampleOne
      1. Why does this suck?
      2. Boxing and unboxing happen, so performance is bad.
      3. Use the generic implementation instead.
    4. How does an iterator work?
      1. Using iterators you can have the c# compiler generate the implementation of IEnumerator for you. The C# compiler can automatically generate a nested class to maintain the iteration state. All you need to do tell the compiler what to yield in each iteration.
      2. Example Two
      3. The compiler generated nested class maintains its iteration state. When the foreach loop is first called the compiler generated code GetEnumerator creates a new iterator object. Every time the foreach object loops and calls the iterators MoveNext method it begins execution where the previous yield return statement left off. As long as the foreach loop executes the iterator maintains its state.
      4. Example Three
      5. The nested iterators is implemented behind the scenes as a simple state machine. The simple state machine will resume execution after the previous yield statement.
  6. What is coming up in 3.0
    1. Ling
    2. Object Initializers
    3. Collection Initializers
    4. Anomymous Types
    5. Local variable type inference
    6. Implicitly typed arrays
    7. Lambda Arrays
    8. Automatic Properties
    9. Extension Methods
    10. Partial Methods
  7. Discussion



3 thoughts on “C# 2.0 Language Features — Brown Bag Lunch Transcript”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s