C C T B H P N

Lecture #6 - Advanced C# Features

More Example Code

Advanced C#:

This lecture focuses on some of the newer additions to the C# language, e.g., LINQ, Extension Methods, and delegate shortcuts.

Web Resources:

UML XML, HTML Code Project Microsoft Developer's Network DevelopMentor XML Sells Brothers Windows Developer's Journal WindowsClient.net DotNetJunkies dotnet quickstart tutorials C# Corner Mono Project Performance Counters
C# Tutorials
MSDN C# ref
University of Linz C# Tutorial
Win32 API to .Net Map
Intro to C# Tutorial and Advanced C# Tutorial

Lecture Specifics:

  1. The Syllabus describes topics to be covered, reading material for you to digest before coming to class, and due dates for the assigned projects.
  2. Project Helpers:

    Helper Code and Other Miscellania:

    Project #1, Project #1 Helper Code
    Project #2, Project #2 Helper Code Blog.Testing, Code Dependencies - pain point with classic config mgrs like git Find Visual Studio Command Window used to test Compile.bat and Run.bat files
    Substitute 2017 for 2015, cited in pdf
  3. Uses of reflection, delegates, and lambdas:

    Reflection: - Reflection Demos is used to:
    • Discover type information from an existing assembly by examining its metadata. For example, in the TestHarness, we need to use reflection to distinguish between test driver libraries, defined to be types that implement the ITest interface, from other libraries that contain code to be tested.
    • We also use reflection to learn about the structure of an existing assembly so that we can write code that uses the assembly effectively. The lecture today provides an example of that use in Reflection Demos.
    Delegates: - delegates.htm are used for two purposes:
    • To allow reusable library code to signal application specific code of a library processing event. Examples are button clicks in a GUI and the Directory Navigation component we discussed in the last lecture. Use of delegates allows us to remove all application specific code from our library components so that they can be reused without change.
    • To bind to lambdas, allowing your code to transport a lambda's processing and data to some other part of your code that needs to use the lambda. An example is the construction of thread pools where an application defines processing for a thread in a lamda, then enqueues a delegate bound to the lambda. When a thread is available it dequeues the delegate and invokes its lambda processing. We will say a lot more about lambdas when we cover threading.
    Lambdas: - lambdas.htm Lamdas provide means to define processing in a block of code and provide data that processing needs. We then can store the lambda for later use or transport it to some other piece of code that needs it. Lambdas are used to:
    • Encapsulate processing and data to be used in some other code location.
    • Define a snippet of processing that is used in several places in some function without cluttering up the using class with private functions and private data that are only used locally in that one function.
    • Define anonymous methods, often bound to a delegate for access, that syntactically are very similar to C++'s global (unbound) functions.
    If that sounds like creating a class and sending an instance of the class somewhere, that's exactly what it is. Lambdas are a quick and easy way to define objects and use them, letting the compiler generate code that defines the class.
  4. Reflection Examples:

    Assembly Structure, vsdx
    This diagram illustrates an Assembly's parts. Each assembly holds type metadata put there during compilation. Reflection extracts the type metadata and stores in a Type instance. The Type class defines a container for metadata and provides a number of methods and properties to access that information.
    Reflection Demos This demo defines a ReflectionTest class that has a lot of structure including a delegate. It also, in main, defines a lambda bound to an Action<string>.
    The demonstration:
    • Reflects on the ReflectionTest class to illustrate how reflection works.
    • Reflects on a delegate hosted by the ReflectionTest class to examine the delegate structure.
    • Reflects on the lambda to examine the structure of a class the compiler builds from the lambda source code.
  5. C# Examples:

    Delegates and Lambdas - All Right!

    Delegates:

    Lambdas:

    • Lambda capture
      Fairly simple example of lambda definition and use.
    • Lambda Demos
      Contains three demos:
      • LambdaDemo: illustrates delegate and lambda structures using reflection
      • MoreLambdas: Discloses behavior of lambda's variable capture
      • PredicateLambda: Shows one way to build query processing for Project #2

    Other Demos:

  6. More C# Code Examples and Tutorials:

  7. C# Programming Environment

  8. LINQ and related topics:

  9. Components:

    Just in case we didn't get to these yet ...

  10. Using Dynamic Link Libraries (DLLs)
    Shows how to build and execute dynamic link libraries - make sure you study both the code and comments in this demonstration.
  11. Lecture Take-away:

    Most important topics are:
    • Reflection
    • Delegates
    • Lambdas