Monday, 24 August 2015

Automated Testing a WCF Service

Slightly, ok very, off topic but this is something I've been working on today and I need to blog about it before I forget what I did!

I need to create a test harness for testing one method on a pre-existing web service.  That web service even comes with a test harness in the code but it's very old-school .Net and I can't fathom it - or learn for myself if I just try and pick it up.

There were several things I had to learn to do:-
1. Consume/wire up to the WCF service.
2. Read in test data from an input file.

Consuming a WCF Service

This was harder - or at least took me longer - than it should have been.  It helps if you're in a Unit Test project, rather than a class library (doh!) and in the end was relatively simple - when you know how.  I referred to the section "Accessing a service" at this page https://msdn.microsoft.com/en-us/library/bb386386.aspx to help.  It was the first I found that talked about adding a Service Reference.  Prior to that I was using the svcutil tool to create a client class, but still had some problems with that.  It looked like it was going to work but I went the "Add a Service Reference" route.

The beauty of programmatically testing a service is that in code it is just another class, and its public methods exposed are methods of that class.  So you just need to create an instance of that class and you can call its methods.  Don't forget to close  it!  I used [TestInitalize] and [TestCleanup] methods to ensure I did this properly.

A few quick tests proved I was able to all the service, receive an answer and Assert if it was right or not.  Now to automate the data input from a file.

Read in test data from an input file

Again a microsoft page was a huge help - Walkthrough: Using a configuration file to define a data source.  It struck me as odd that I found Microsoft resources more useful that StackOverflow.  I guess there's a first time for everything...

Several steps are required here, and it took some jiggery pokery to run correctly on my system but it worked.  Perhaps most imporantly, I don't (yet) understand all the decorators that were suggested.


  • Update app.config with custom configuration settings.  Follow through the notes in the above link, bear in mind that at the end of that page is a complete example you can copy and paste.  In summary you need to create something like this:-

A <configuration> section containing
        <configSections> /// Standard text for a named section
        <connectionStrings>
        <named section>  /// <microsoft.visualstudio.testtools> is the standard text
              <dataSources>

I've used the full path to my input file in the connectionString attribute of the add element, with escaped backslashes \\ in the path.  I've used an excel spreadsheet as the input file because this Microsoft page gives the correct info for connecting to a spreadsheet.  I'd rather connect to a csv file, to be honest, but needs must.


  • Add a TestContext into the TestClass 
The document doesn't describe what a TestContext is, or why, but you need one.  The sample text shows a private field with a public member that implements getters and setters.  You'll refer to the private field in tests so it's handy to be able to name it (with a short name).  I'd guess it's used to keep track of what row you're on in the spreadsheet.
  • Decorate your Test Method
A couple of decorators are all you need - though again I'm not clear what they each do.
[DeploymentItem("TestProjectFolder\\InputFile.xlsx")]
[DataSource("UseTheNameAttributeFromTheAddElementInDataSourcesInAppConfig")]


  • Import System.Data
I'd to add a reference to System.Data and pop a using statement at the top of my TestHarness.cs file.
  • Write your test.
Data is imported from your spreadsheet as an object, and it seems to need you to hard code the column name.
         //Arrange
            string MPString = context.DataRow["MP"].ToString();
            if (!(String.IsNullOrEmpty(MPString)))
            {
                mp = Int32.Parse(MPString);
            }
 
            string idPrefix = context.DataRow["Prefix"].ToString();

Invoke the DataRow method of the TestContext to get the current row of your input file.  The ToString() method is available which is useful.

As you can see above, to get an int you first call the ToString method and then parse it into an int.  As I may not have any value in that column in the input spreadsheet I have to perform a check first.

You can read the expected result in from this spreadsheet too.

The Act part of the test is to call your web service, passing in whatever input parameters you require.
            //Act
            string actualResult = client.GetData(mp, idPrefix);
 
            //Assert
            Assert.AreEqual(expected, actualPrefix);

I'm using Assert.AreEqual but I find if a row fails that the only clue I get from the output is "(Data Row x)" where the first test is Row 0 (the heading row is ignored in the counting).  Perhaps a different Assert method that lets me output what the expected result or some input was would help.  For now I've added a column of numbers into my test input file as a guide.


Useful Note

I've also found that storing the test input file in my test project's folder means I can add it into the solution easily.  Then when I want to edit the input file I just double click on it in solution explorer and it launches Excel.

Summary

Add a service reference to your existing WCF service.
Import System.Data and include it in your test class file
Add a TestContext member into the test class
Use TestInitialize and TestCleanup to ensure you instantiate the service and close it properly at the end of a test.
Add a custom section into your app.config file in the test project's folder
Write your tests, with two additional decorators.  Use TestContext.DataRow to read a value from a column into a variable.

Saturday, 15 August 2015

Asynchronous Programming

Old async programming with callbacks/delegates/promises

Synchronous programming blocks the main thread, which in web apps typically results in the UI blocking and being unresponsive while some backend processing occurs.

Asynchronous programming (such as C#'s Task<T> or javascript's promises) implement non-blocking asynchronous programming that frees up the UI to remain responsive by allowing the slow back end processing to occur in parallel (on another thread) and once it has completed proceeding with the program.

Historically this was achieved by the use of callbacks to signal the completion of the background operation.  If you begin to execute a long running process you invoked it with as an input parameter a callback function / pointer-to-function / delegate that would be signalled (execution would jump to) on completion of the long running process.   If program calls a long-running operation a you might pass in a delegate/the address of method b.  When the operation a completes your program resumes in method b.  If method b calls an async call to a long running process it may invoke it with a delegate of method c.  It can quickly become confusing and hard to keep track of the program execution flow.

C# 5 New model

C#5 handles much of this behind the scenes,  New asynchronous methods turn your code into a callback state machine that tracks and handles the program flow for you.

Mark your method with a new "async" modifier
Return void or Task<T> (where T is the type you would have returned normally)
Use the await operator to yield control, i.e. await <methodname>  where methodname is an asynchronous method.
Program flow resumes - in the same method -  after the awaited method has completed - but in the mean time your UI has been responsive as normal.  Thus your code looks and fields like good old synchronous code, even through it is executing asynchronously.

        public Form1()
        {
            InitializeComponent();
            ComputeStuffAsync();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        async void ComputeStuffAsync()
        {
            var sum = 0.0;

            await Task.Run(() =>
            {
                for (var i = 0; i < 200000000; i++)
                {
                    sum += Math.Sqrt(i);
                }
            } );
            lblSum.Text = sum.ToString();
            lblSumTxt.Text = "Once";

            await Task.Run(() =>
            {
                for (var i = 0; i < 200000000; i++)
                {
                    sum += Math.Sqrt(i);
                }
            });
            lblSum.Text = sum.ToString();
            lblSumTxt.Text = "Twice";
        }

Friday, 14 August 2015

Starting LINQ

Language Integrated Query was introduced in C# 3.0 and it allows you to query any collection - in memory, a database, etc with standard powerful logic / operators.  Historically developers had to learn multiple APIs to deal with data from different sources e.g. XML, database or in memory.  LINQ brings data access into the .Net languages (that support generics, specifically support IEnumerable<T>) and provides both intellisense and static type checking within the IDE.  These were lacking in previous data access coding APIs.   This one common way of accessing data (LINQ) is akin to the one common way of accessing external communications (WCF) which unified calls queues, web services, pipes, etc.

Out of the box LINQ provides approximately 50 standard query operators:-

  • Filtering (e.g. where)
  • Projection (e.g. select. Projection returns just some data attributes in an object/table, not all)
  • Joining
  • Partitioning (skip, take.  Useful for pagination e.g. give me results 11-20).
  • Ordering (order by)
  • Aggregating (group by)
Many of these are similar to SQL.

The same standard operators work everywhere.  There are additionally custom query operators / providers produced by the user community (e.g. LINQ to Amazon).

  • LINQ to SQL supports SQL Server only.  LINQ to entities provides ORM features.
  • LINQ to XML supports XML, with a new XML API (System.XML.Linq) that is less formal and easier to use.
  • LINQ to Objects supports working with objects in memory.  This is often overlooked but is very powerful, and can replace foreach loops and selections with much more succinct code.


You can return results as collections of anonymous classes.  There are two styles/syntaxes for writing LINQ queries, Method Syntax and Query Syntax.

Method Syntax

(Very like SQL, just with the select statement at the end of the expression)
var results = from x in staff where x.Grade = "Manager"
select new {x.Name, x.PhoneNumber}

Query Syntax


(Uses a lambda expression - input parameter(s) to an anonymous method that returns a result).
var results = staff.Where(x=>x.Grade=="Manager");


LINQ Query Operators

LINQ contains some familiar and some new (but very useful) operators:

orderby

This is the same as the SQL order by clause.

            var doubleQuery = from method in typeof(double).GetMethods()
                        orderby method.Name
                        group method by method.Name into groups
                        select new { MethodName = groups.Key, NumberOfOverLoads = groups.Count() };

            foreach (var item in doubleQuery)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();

group

Same as the SQL group by clause.  (See orderby example, above).

any

Returns true if a collection has any members.
var largeList = Enumerable.Range(1,20);
var largeListEmpty = largeList.Any();
Console.WriteLine("largeList contains items? " + largeListEmpty);

contains

Returns true if a collection contains the item(s) provided in the statement
Console.WriteLine("largeList contains 7? " + largeList.Contains(7));
Console.WriteLine("largeList contains 27? " + largeList.Contains(27));

take

Takes (copies) the first n items out of the collection.
var largeList = Enumerable.Range(1,20);
var smallList = largeList.Take(5).Select(x => x * 10);

foreach (var item in smallList)
{
    Console.WriteLine(item);
}


foreach (var item in largeList)
{
    Console.WriteLine(item);
} //largeList is unchanged as the items were copied out.

Take can be a useful way of limiting your result set.

zip

Zips together two collections (presumably they need to have a matching number of items).
string[] shortMonth = { "Jan", "Feb", "Mar", "Apr", "May" };
string[] longMonth = { "January", "February", "March", "April", "May" };

var monthMapping = shortMonth.Zip(longMonth, (sMonth, lMonth) => sMonth + ": " + lMonth);

foreach (var m in monthMapping)
{
     Console.WriteLine(m);
}
Console.ReadLine();

Deferred Execution

LINQ query expressions usually do not execute until we access the result, i.e. they are lazy operators.  (Certain LINQ operators such as count are greedy and execute immediately).  Thus you can build up queries as user input changes, and only execute it once, when needed.  In the order by example, above, the LINQ query only executes when the foreach loop calls it.  It is defined first but execution is deferred until needed.  This is not unlike deploying a stored procedure in a database that only executes when called.

Summary

LINQ allows you to query any collection using one standard API
LINQ standard operators work against IEnumerable<T>
It has many useful operators
It has a powerful chaining syntax to combine operators together powerfully.

Friday, 17 July 2015

Delegates and Events

Delegates

Delegates are used to decouple the class that uses a method from the class that declares it.  A delegate wraps a method, so that you can call the method through the delegate.

public delegate int DelegateName(object inputParam)

When specifying a delegate you define the input type(s) and return type of a method that will be wrapped by the delegate.  You don't name the method, and hence the delegate can wrap any method that matches the input type(s) and return type specified in the delegate.  The delegate has no method body - that is provided/defined by the encapsulated method.

You can create a class that contains members and define a delegate.  Now, any client using your class can perform some action with the object instance of your class.  The action will depend upon the method encapsulated by the delegate - it may not be defined in your class containing the delegate.  You can pass delegates as parameters into methods - typically used to check the return type of the delegate.  You pretty much need a method in your class that takes as its parameter your delegate, so that you can use the delegate in a calling class.

The implementing method won't contain anything that indicates it is related to the delegate or your class that contains the delegate.

The calling class instantiates an object of your class.  If the implementing method is actually in a third class then your calling class needs to instantiate an object of that class too.  To instantiate the delegate, create a variable and assign to it yourclass.delegate and specify the class to be encapsulated by the delegate.


Class containing method = Document
Implementing method is "PostToBlog" in a class "BlogPoster".
Calling class:-

Document doc = new Document();  //Instantiate an object of your class
var blogPoster = new BlogPoster();  //Instantiate an object that contains the implementing method

var blogDelegate = new Document.SendDoc(blogPoster.PostToBlog);  //Instantiate the delegate, telling it to encapsulate the method PostToBlog

You're now ready to invoke the delegate.  As it is in your class you execute the method of your class that takes the delegate as its input parameter.

doc.ReportSendingDoc(blogDelegate);

This has dynamically instantiated the delegate and passed it to a method of your class, instructing it to use this delegate instance.  Your delegate will in turn call the implementing method.

You could instantiate a second delegate that encapsulates a different implementing method.  This would be invoked by calling the same method in your class and passing the second delegate.

Events

Events fire (or are raised) when something happens to trigger the event, e.g. a button click.  In C# events are implemented with delegates.  The subscribing class is encapsulated by the delegate so that the subscribing class is invoked when the event fires (delegate is called).

The convention in .Net for events means events always:-

  • have a return type of void
  • take two parameters - An object (the sending object) and EventArgs (or something derived from EventArgs) that could contain helper data.


Events can be used in the publish-subscribe pattern.  The publishing class (your class, above) contains a delegate so that it can raise events.  Remember the delegate won't return anything and the methods encapsulated will take two input parameters.  As well as defining the signature of methods to be encapsulated by the delegate you need to also create a member instance of the delegate that the subscriber methods will subscribe to.  This will be null unless or until someone subscribes to the delegate.  This is a useful feature as you can detect if anybody is subscribed/listening for your events and only raise them if someone is subscribed ok.

The subscribing class needs to subscribe itself to the delegate of the publishing class.  The simplest way to do this is to create a method Subscribe in your subscribing class.  This method takes as its input parameter an object of type publishing class.  In that method you add/append to the delegate your subscription, specifying the name of the class to be encapsulated by the delegate, i.e. the method to be invoked within the subscriber when the event is raised.

public void Subscribe(PublishingClass thePublisher)
{
   thePublisher.DelegateName += new PublishingClass.DelegateDefinition(SubscribingMethod);
}


The += operator adds you to the delegate's subscription list.  -= unsubscribes you.  You can have multiple subscriber to an event.  These need to be added to the list of classes that are subscribed to the delegate, rather than overwrite/replace the list.  The event keyword enforces this at compile time as it prevents you from incorrectly using just the = keyword.  The = operator would set the delegate's subscriber list (handler) to just this method, overwriting any previous subscriptions.

The solution is to use the event keyword into the publishing class at the instantiation of the delegate.

public delegate void DelegateHandler (object publisher, EventArgs otherInfo);
public event DelegateHandler delegate;

Accidentally using the wrong operator = instead of += will be detected and noted as an error at compile time.



Anonymous Methods

A delegate wraps a method so that you can call that method through the delegate.  You register a delegate and then instantiate the delegate, passing it the name of the method to wrap.  Rather than all of that - which forces you to have a separate method - you can just pass in the method itself.  You don't name the method, hence it is anonymous, but you do still use the keyword delegate.

In the events example, above, any class subscribing to an event registered itself with the event and provides the name of the method to be wrapped.  Instead of this

public void Subscribe(PublishingClass thePublisher)
{
   thePublisher.DelegateName += new PublishingClass.DelegateDefinition(SubscribingMethod);
}

you could replace the line after the += with the keyword delegate and the implementation (including signature) of SubscribingMethod thus:-


public void Subscribe(PublishingClass thePublisher)
{
   thePublisher.DelegateName += delegate(object sender, EventArgs e)
   {
              // code of SubscribingMethod.
              // resolve the naming conflict thePublisher by renaming the input param to sender
              // Put a semicolon at the end, below
   };
}

This can be a valid way to do this, provided nothing else was calling your (now anonymous) SubscibingMethod.

A more popular way of doing this is now lambda expressions.

Lambda Expressions

Lambda expressions are a more flexible and powerful syntax for anonymous methods.  The syntax is thus:
(input parameters) => {expression or block evaluating to a value}

=> is "goes to"
Input types are not provided, but type safety is assured.  There's no return keyword because it evaluates to a value.

(x,y) => { x*y };

Read as "x y goes to x times y".

To convert the above implementation of an anonymous method into a lambda expression you:-

  • take out the delegate keyword and input param types
  • Reformat as per coding standards for indentation
  • insert the => goes to operator

public void Subscribe(PublishingClass thePublisher)
{
   thePublisher.DelegateName += 
   (sender, e) =>
   {
              // code of SubscribingMethod.
              // resolve the naming conflict thePublisher by renaming the input param to sender
              // Put a semicolon at the end, below
   };
}


You now have a lambda expression in the registration for the DelegateName event.
You read this as "Register with DelegateName the anonymous delegate represented by this lambda expression that takes two parameters.

Summary


  • Defining and using delegates
    • Delegates wrap/encapsulate any method that matches the signature and return type
    • The delegate has no method body
    • Delegates decouple the declaring class from the consuming class
  • Events and how they 'fix' delegates
  • Anonymous methods
  • Introduction to Lambda Expressions





Wednesday, 8 July 2015

Interfaces

Interfaces are similar to base classes in that they create a contract that implementing classes must follow.

However, a base class is usually a "thing" (noun) e.g. Control in the previous post on abstract base classes.

An interface is usually a "capability" e.g. Enumerable, or Readable, Storable, etc.

You can implement as many interfaces as you like, to simulate multiple inheritance. (C# doesn't support multiple inheritance of classes).


Interfaces don't have access modifiers - they aren't legal.  By definition an interface has to be public.

Casting

You can't create an instance of an interface but you can create a variable of an interface that points to an instance of a class that implements the interface.  (Just like you can create a variable of an abstract base class that points to an instance of a concrete class).   You may wish to cast that variable into the type of class that it points to.  There are several ways to do this, the safest is the as keyword.

The as keyword

var myClass = new ImplementingClass();
ICapability capable = myClass;

ImplementingClass theNewClass = capable as ImplementingClass;
if (theNewClass != null)
{
    //do something class specific;
}

If the cast fails the variable will be set to null so you must always check that.



Friday, 19 June 2015

Abstract and Sealed Classes

Sealed class

A sealed class cannot be inherited from.  It is recommended for classes that won't be derived from, or only contain static methods / properties.  Typically only used if writing a library.

Abstract class

An abstract class is used to represent something (an abstraction) that should never be instantiated.  You must derive from an abstract class.  All abstract classes must be overridden.  An abstract class establishes a "contract" for all derived classes.


public abstract class Control
{
   protected int top;
   protected int left;
   public Control(int top, int left)
  {
       this.top = top;
        this.left = left;
}

public class Button : Control
{
     public string Contents;
     public class Button(int top, int left, string contents)
            : base(top, left)
     {
            Contents = contents;
     }
}

In your program you cannot do this
Control c = new Control();
because you cannot create an instance of a control as it is declared abstract.

However you can create an instance of a derived class - i.e. create the concrete class - and assign that to a variable of type Control.
Control button = new Button(2, 3, "New Button");


The benefit of this is that you can create a collection of Control objects and iterate over them because all variables are of the same type, control.   You can execute the abstract method in the base class, and each concrete class will execute that correctly because they override the abstract base method in their own way.  Polymorphism is at work again.

Polymorphism: Inheritence, Overriding and Overloading

When a class inherits/derives from a parent class it inherits all protected and public methods in the base class.

Hiding methods and Overriding


If I implement a method in the child class with the same signature as one in the base class it will hide the base class's implementation.   The compiler alerts me to this and suggests a fix of adding the new keyword.  That will work as expected in some but not all conditions.

It is perfectly legal to declare a new variable of the base class and populate it with a new child class:

Base newbie = new child();

This is because the child class has an "Is A" relationship with the base class.  A child is a base so the above works.  However if you invoke the method that has been hidden in the child class, what executes and is output is the implementation in the base class (because newbie is of type Base).  This isn't what was expected because newbie is really a child.   We want, instead of hiding the child's implementation to override the base implementation with the child implementation.

To override instead of hide a method, in the base class mark the method as virtual (which indicates we expect it to be overriden in a child class) and in the child class replace new with override in the signature of the method.

This allows the methods to implement polymorphism as the method behaves differently/correctly depending on the type of each object.
Overloading = virtual methods in a derived class.

Method Overloading

It's important to remember that the signature of a method is its name and parameter list (but not its return type).
You can overload methods - methods in a class can have the same name provided:
Different number of parameters
Different type of parameters

Overloading = same method name in one class.
You can both overload and override methods

You can also overload constructors - very handy and saves duplicating code




Monday, 1 June 2015

Default and Named Parameters

Default and Named Parameters were new features in C# 4.0

Default Parameters

Methods can take parameters that are optionally nullable like thus:-

public void SomeMethod(DateTime? time)

In C# 4.0 This can be called by this code
SomeMethod();

As the parameter is nullable it is now also optional.  Optional parameters can only be listed after all required parameters.  There are two syntaxes for doing this


  1. Optional Keyword
    Requires the addition of another using statement as well as the optional attribute

    using System.Runtime.InteropServices;

    public void SomeMethod([Optional] DateTIme? time)

    If the caller doesn't provide a value for an optional parameter then it will be assigned the type's default value (null for reference types, 0 for many numeric types).
  2. Specify a default value
    Doesn't require the using statement.  This also allows you to specify what the default value is

    public void SomeMethod(DateTime? time = null, int value = 3) 

    Note the compiler must be able to compute the value provided; It must be a compile time constant.   You couldn't use DateTime.Now() as a constant for example. 

Named Parameters

When calling a method and passing in parameters it can be unclear what the parameters are for, particularly if you pass null as one of the parameters.  You can name the parameters in the call (the name must match that given in the signature) with a colon and the value to pass to that parameter in the method call.

SomeMethod(time: DateTime.Now);
SomeMethod(value: 3);

In the second example above the time parameter is omitted and hence it gets the default value, null.
This is useful for the situation where parameters or values passed aren't very descriptive and make reading the code harder.

Wednesday, 27 May 2015

Generics

Generics allow code reuse with type safety.  A generic is a class that defers specifying the type until it is instantiated by the client.

Generics

In ArrayLists, you store references to objects, and so when retrieving an object from an ArrayList you typically had to cast it into the type you want.  Also, you couldn't prevent objects of the wrong/a different type being added into your array (because the array holds refs to objects).  So an ArrayList of employees would happily accept you adding a string into the array.  You'd probably get an error when retrieving that array item though.  You lost type safety and had to do lots of checks and casts.  If you wanted different types of ArrayLists, e.g. Employees and Projects, you'd have to write practically identical code to create your own custom ArrayList type.

Generics avoid all of that.  Also produce better performance due to no boxing for value types

Generic Collections:-
  • HashSet<T> - Set of items; no duplicates allowed.  (Compiler calculates the hash value of every item being added to ensure no other objects with the same hash)
  • List<T> - Like an array that can dynamically array.  No restrictions on type, duplicates.  Methods to sort, order list.
  • Queue<T> - FIFO list  (Enqueue and Dequeue operations)
  • Stack<T> - LIFO (push and pop operations).
  • Dictionary<Tkey, Tvalue> - Key, Value pair.  Look up by Key using hash codes, not index over a list.

Generic Types for value types, e.g. List<int>.  CLR generates a specialised generic type for that value type.  (So the generic type for a List<int> is different from the generic type for a List<datetime>.  It pre-allocates space on the heap for these types.  Thus the List<int> stores ints, not references to objects, so there is no boxing/unboxing required.

It is slightly different for ref types, e.g. List<employee>.  The Run time creates a specialised generic of ref types, irrespective of what the ref type is.  So List<projects> will create the same specialised generic type as List<employee>.  (Like templates in C++).  Pointers are always the same size for a ref type, hence you can use one specialised ref type for generics holding a ref types.

Public class MagicHat<T>
{
   public void Add(T thing)
  {
      _things.Add(thing);
  }
  List<T> _things;
}

//
Rabbit rabbit = new Rabbit { Name = "Fuffy" };
MagicHat<Rabbit> _rabbitHat = new MagicHat<Rabbit>();
_rabbitHat.Add(rabbit);

Or
Class MagicHat<T> : Queue<T>
{}


Generic Constraints

Restrictions on the type of parameter
Force the type to be a struct or class
Force the type to have a public default constructor
Force the type to implement an interface or derive from a base class

Public class MagicHat<Tanimal> where Tanimal : Ianimal
{
  …
}

Need at least 1 constraint when you want to perform some operation within the generic collection class that isn't supported by System.Object.

Null references and Generic Types

It's ok to compare value types with null (It's always false)
But it's not ok to assign null to a <T> because you can't assign null to a value type.  (Set a constraint where T: class enforces ref types).

Where T: class, new()    <- new() enforces a default constructor (0 params)
To ensure a type has a method, force it to implement an interface.  The interface defines that method.  (Could also be a base class).

Generic Terminology : Type Classifications

Unbound generic types.  Type param hasn’t been specified - List<>
Can be used in reflective code with the typeof operator.  It's a blueprint to create other types.  The opposite is a constructed generic type

Constructed Generic type - two forms
Open Generic Type - has type parameter that still needs to be substitued - List<T>
Closed Generic Type - has no type parameters - List<int>

An object constructed from an instance of a generic type is a concrete object
At runtime, code executes in a closed, constructed type.

Thus in inheritance - when a generic class inherits, you can't inherit from an unbound generic type - must be constructed (but could be open or closed).
Thus:
Public class AnimalCollection<Tanimal> : Icollection<Tanimal> {}
Or
Public class AnimalCollection<Tanimal> : Icollection<int> {}


When defining a non-generic type that inherits, it can only inhert from a closed constructed generic type.  Thus:
Public class RabbitCollection : Icollection<Rabbit> {}


Might see this in Reflection or Inversion of control container programming.

The default keyword

The default keyword can be used to assign a default value to a generic when you don't know if it will be using a value or reference type.  Useful for returning results from a method instead of raising an exception.     You can't return a null as you can't assign a null to a value type.

C# supports Generic Interfaces and Generic Delegates (EventHander, Func, Action and Predicate).
Generics and Variance
Before C# 4.0 generic collections were invariant.  That is they didn't support polymorphism.  If a generic class Dog implemented IAnimal you couldn't pass an object of type Dog into a method that expected an input parameter that implemented IAnimal  Generics were invariant.  This changed in C# 4.0, with exceptions.

  • <T> must be a reference type
  • It only works with some specific generic interfaces - IEnumerable<T>, IEnumerator<T>, IQueryable<T> and IGrouping<T> (These are popular with Linq)




C# and the CLR. Compilation & GC, threads, asyncrhonous programming, reflection and interoperability

JIT Compilation and Garbage Collection

GC cleans up unused memory (on heap, i.e. no refs to memory location). Visits global variables and local variables to determine what is in use.

Frees up and compresses memory - defrag for memory - and fixes refs of objects moved in memory.

You can use the GC problematically via  a class GC with static members and properties. sssss CollectionCount(0), (1), (2) reflect the three generations

When an object is first created it is in generation 0.  If it survives a GC, i.e. still referenced when the GC runs, then they are promoted to generation 1.   If it survives another GC then it is promoted to GC 2.   Generation 0 is  GC'd much more frequently that generations 1 and 2.  Many objects are short lived (with scope only for  method) and hence stay in generation 0 so it is GC'd more frequently.

Threads

System.Threading - low level API for starting, stopping and joining threads
System.Threading.Tasks - high level API for concurrent and asynchronous programming. (New .net 4)

using System.Threading.Tasks
string[] urls = {"http://website1.com", "http://website2.com", "http://website3.com"};

Parallel.ForEach(urls, url => {
   var client = new WebClient();
   var result = client.DownloadString(url);
   // ...
});

Parallel is a higher level API

Asynchronous

WCF Service proxies and other libraries such as the web client class have asynchronous versions of methods.  These are preferable generally as they can use threads from the thread pool and easily recycle them, rather than having to create a new thread.

Low Level API System.Threading


using System.Threading
string[] urls = {"http://website1.com", "http://website2.com", "http://website3.com"};

foreach (var url in urls)
{
    Download(url);
}

private static void Download( string url)
{
  var client = new WebClient();
  client.DownloadStringCompleted +=  client_DownloadStringCompleted;
  client.DownloadStringAsync(new Uri(url), url);
}


public static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    var html = e.Result;
    var url = e.UserState as string;
    //Do something here
}

You need to wire up an event handler to DownloadStringCompleted to execute when the thread has finished downloading the string.  The compiler can figure out the type of event handler so you just need to give it the name of the method to execute when ready.

The second parameter in DownloadStringAsync is a user token that you can use to "tunnel" additional information, such as the url being downloaded, into the event handler.

You don't need to fiddle around creating threads, and could potentially reuse a thread if one task completes very quickly.  But your code is splattered through three methods.

High Level API System.Threading.Tasks

Parallel can invoke something directly, or you can iterate over a collection with a .ForEach.  Provide the collection and an expression to tell the task parallel library what to do (using a lambda expression).

using System.Threading.Tasks
string[] urls = {"http://website1.com", "http://website2.com", "http://website3.com"};

Parallel.ForEach(urls, url => {
   var client = new WebClient();
   var result = client.DownloadString(url.ToString());
   // ...
});

However the main thread will block until all parallel tasks complete. (The lower level async coding didn't block).

Reflection and Metadata

Reading metadata of the assembly / code running by reflecting on itself.  Typically starts by getting a reference to a System.Type object using either (at run time) the .GetType method available on every object or (at compile time) the TypeOf operator.

With this type reference you can query the type to get info, e.g. properties, members, etc.  Most only return public members/properties by default.  There is an overloaded version that allows you to pass in binding flags (an enumeration) you can OR together.

using System.Reflection;

static class Program
{
    static void Main(string[] args)
    {
         var type = typeof (Program);
          var members = type.GetMembers(BindingFlags.Static |
                                                                 BindingFlags.Instance | 
                                                                 BindingFlags.NonPublic |
                                                                 BindingFlags.Public);
        foreach (Var memberInfo in members)
        {
              Console.WriteLine(memberInfo.Name);
        }
    }
}

Can also look at attributes (DataAnnotations) on a member or property.  They are baked into the assembly by the compiler.
Attributes are very useful so you can create your own - custom metadata
Your custom attribute is a public class that derives from the System.Attribute class.  Naming convention is to end the name of your class with Attribute.   Specify usage with an attribute [AttributeUssage(AttributesTargets.) enumeration.  You can create a constructor and also define public Properties as appropriate.

To use, just decorate the target thus
[YourAttribute{any, input, params)]


Any input params in the constructor must be provided when using the attribute.  Unlike a class, however, you can additionally provide any additional public properties available in the custom attribute by specifying its name.

You can not only inspect objects using reflection you can create objects and invoke methods and properties too.

Get a reference to a type.
Get a propertyinfo for one of the type's properties.
Get the value of that property held by an object of that type.  It returns an object so cast it as needed.

Could also use GetMethod to get the MethodInfo of a method available on a type.
Invoke the same.  Any input params for the method are passed as an object[].

dynamic keyword
Bypasses any static type checking by the compiler.  It also coerces the returned type into what you want.  The result is cleaner code for the same functionality.  

dynamic someObject = o;
int length = someObject.length;

However there is the risk of a runtime error if the property or method can't be found on the object.

Create Objects using the activator class.
Get a reference to a type
var type = o.GetType();
var newInstance = Activator.CreateInstance(type)

Can create code on the fly!  You create a new DynamicMethod, passing in the name, its return type and any input parameters.  Then you lay out the instructions to execute within the method.  To do this you write IL code by calling the ILGenerator class to load in OpCodes.  Not C#, these are IL instructions.

Processor architecture

There are two CLRs - 32bit or 64bit.  32bit can run on a 64 bit system (as a 32 bit process via emulator), but not vice-versa.  Set these by setting build properites on the project.

x86 platform = 32 bit.
AnyCPU will load your program into the best type of process available.  So on a 32 bit machine it will run your program in a 32 bit process.  On a 64bit platform it will run your progam in a 64 bit process.

The default in VS for projects that create .exe executables is x86 i.e. 32 bit as these can run in most places.   x64 can only run on a 64 bit process (but does have a greater memory address available)

The default in VS for class projects or projects that create DLL assemblies is AnyCPU so that it can load into any executable, i.e. an executable running in either 32bit or 64 bit.

For AnyCPU, if you build an executable under this and load it into a 64 bit process it will run, but if it attempts to load a 32bit dependency this will fail.  You can't load a 32bit library into a running 64 bit process.

Ineroperability

COM was a historical foundation building block of older windows operations.
Mark up code with interop attributes that COM needs.  Then run Assembly registrtation tool (regasm) to register information into the registry for the assembly, so that COM programs can access and use your code.

You can also consume COM components by adding a reference to the COM component.
Less and less important as most functionality in COM is now available in .Net.

Unmanaged code not packaged into a COM component.
Use Platform Invoke - PInvoke - to call into Windows APIs and unmanaged code.

See wiki http://pinvoke.net to see what signatures, etc needed to invoke an API.

Monday, 18 May 2015

Control Flow


Branching

If - else if - else


or for shorthand there's the turnary operator   ? : string pass = score >80? "Yes" : "No"

The values in the result (strings) must match the type of variable being assigned to.

Switch

Restricted to Integers, Characters, Strings and Enums
No "fall throughs" - must have a break;
Case labels are actually constants (hence the restriction)
Default label is optional

switch(name)
{
  case "Name1":
     DoThis();
     break;
  case "Name2":
     DoThat();
     break
  default:
     DoTheOther();
     break; // Even need this break!
}

Can stack multiple cases that lead to the same code.

Iterating

for, while, do-while, foreach

for, and while both execute 0 or more times
do-while executes at least once.

foreach iterates over a collection.   Compiler is looking for a GetEnumerator (that is derived from IEnumerator) method on a type.  If found you can use foreach to iterate over it.
While in a foreach loop you cannot modify the collection.


Jumping

break; continue; goto; return; throw
break - can use from within any loop/iteration (e.g. for or while) as well as selection like switch.
continue - similar but it doesn't exit the loop it just goes to the next iteration (used within loops)
goto - go to somewhere else in code marked with a label.

return - can be used in a void method (just return statement rather than return <variable>)
yield return - lazily build up a collection - return type must be IEnumerable.  Does a continuation.  Can return one item to the calling app, which does some processing then calls into sub routine again.  The subroutine picks up where it left off, e.g. in the middle of a loop.  Handy if the time taken to produce the whole list is long - it returns each result as it is calculated rather than making the caller wait until it is all completed.

yield break - ends the IEnumerable.

Exceptions

Throw

Used to raise an exception.  Type safe, structured error handling
Runtime unwinds the stack until it finds a handler.  May terminate an app if no suitable handler is found.  (ASP.Net won't terminate the webserver.  It catches the exception - optionally returning an error page - so that the web server doesn't get torn down).

Try Catch

Don't need to name a variable when catching an exception, i.e. this is ok:-
try
{
  DoSomething();
}
catch (Exception)
{
}

but you can't get any info from the exception unless you name the exception variable.

Catching System.Exception catches everything - except a few special exceptions.
Chain catch blocks with most specific first.

finally verses using statements
Using statements are another way of ensuring resources are cleaned up properly even if an exception occurs.  However the Type of variable within the using block must expose the IDisposable interface

Re-throwing Exceptions

You can catch and rethrow an exception - either the original or a different exception.
keyword throw;  

You could type in throw ex but that would show the exception as originating in your catch block.  Essentially you've thrown a new exception of the same type.

You could throw an exception of a new type, e.g. for security reasons.  Catch database errors but throw a general exception error to users.  It may be a custom, more meaningful exception type.

Custom Exceptions

Derive from a common base exception, e.g. System.Exception
Classname - suffix of Exception
Make it serializable (Serializable decorator and special constructor that uses serialization information)

Insert Snippet -> Visual C# -> Exception 
and type and tab information I need.







Strings

Strings
Strings are reference types but behave like value types.  They have been optimised within the .Net framework.

Checking for equality performs a string comparison, not an Object.ReferenceEquals that is normally performed when you compare two reference type variables.

When comparing two strings for equality the framework compares the sequence of characters in each string to compare if they have the same value (rather than the same object).

Strings are immutable - can't be modified.  So performing operations that appear to modify a string, such as Trim, don't.  Rather they return a new string with the result, which must be captured if you want to see the effects of the modification.  The original string remains unaltered.  It is immutable.

Types and Passing Parameters

Types

Types are either value types or reference types.

Value Types

A variable is created on the stack and value is held in that variable itself.  Value types are typically small as you don't want huge amounts of stack memory used.  Value types are immutable (but can be overwritten, so an int variable can hold a 4 and that be overwritten with 6).  Aim for <16 bytes of memory used.  Assigning to value types makes an expensive copy operation.

Reference Types

A reference type sees memory used in both the stack and the heap.  Memory is allocated on the heap to hold all the data stored in an instance of a reference type.   (This may be a large amount of memory).  A variable is also created on the stack.  This stack variable holds a pointer to the location on the heap where the actual instance data is held.

Multiple reference variables can reference the same object.
Single variable can point to multiple objects over its lifetime
Objects are allocated on the heap by the new operator

Classes are reference types (as they may be large).
Structs and enums are value types.
(Structs are sealed so you can't inherit from them, nor can you inherit from a base class; They all inherit from System.Object).

Employee e1  = new Employee();
Employee e2;
e2  = e1;

Using either variable to update the object works ok.



String is a special case - Strings are reference types but they act like Value types.

Passing variables is by default pass by value - that is a copy / new instance of variables is created in the called method.  This copy is amended and destroyed once the called method has completed.  A variable of the same name in the "parent" method is unaffected.  This is true even for ref  types.


Passing Parameters

Parameters pass "by value"
Ref types pass a copy of the reference
Value types pass a copy of the value.  Changes to the value don't propagate to the caller

ref and out are used to pass "by reference"
ref parameters require initialized variable (because it will be used by the caller and hence needs to be initialized by the caller).

Invoice invoice = new Invoice();
invoice.ID = 1;
int value = 1;

DoWork(invoice, value);
Assert.IsTrue(invoice.ID == 5);
Assert.IsTrue(value == 1);

void DoWork(Invoice invoice, int value)
{
   invoice.ID = 5;
   value = 3;
}

The above works because when DoWork is called you get a copy of the variable invoice, i.e. a second pointer to the same object on the heap.  Setting the ID via the second pointer affects the memory which is still pointed to by the calling code.  Hence the first Assert.IsTrue passes ok.

But you didn't truly work on the variable in the initial calling code.  You got a copy and that copy pointed to the same object/memory location.  The following would cause the first Assert to fail

void DoWork(Invoice invoice, int value)
{
   invoice = new Invoice();
   invoice.ID = 5;
   value = 3;
}

It fails because you are working on a copy in DoWork.


If you really want to work with that variable, use pass by ref.  Add the ref keyword into both the call and the method signature.  This means you pass a reference to the reference, a reference to the initial variable Invoice (which still points at the same object on the heap).  This also works for value types; you'd in effect pass a ref to the value variable (which holds the value).

public bool DoWork(ref Invoice invoice, out int value)
{
       invoice.ID = 5;
       value++;
       return (value == 2);
}

The ref and out keywords both mean pass by reference.  The difference being that ref parameters must be initialized outside the method as they will be used in the method.  out methods do not need to be initialized.  out alerts the compiler that the value may not be used within the method.  Rather we will be storing the result of some operation in this parameter for returning to the calling method.




Classes

Classes


Classes comprise of members (to hold data essentially) and methods (behaviours).

Fields and Properties

Fields are the actual storage, requiring memory and typically were private.  Naming convention _name
Properties are different; These implement means of getting/setting fields.  Today you usually just implement the property and behind the scenes the compiler quietly creates the underlying fields for you (following the naming convention _name).  Properties have the naming convention Name

Events and Delegates.

Events can be raised (and subscribed to) in order to alert other objects in your (or other) applications that something has happened.  If you define a delegate anyone could add themselves to the subscribed list - but they could also wipe out the subscribed list.  Safer therefore to use events.  Subscribers can only add (subscribe) themselves to listen to your event, or remove themselves from the list.  They can't do anything else to the subscribed list.


Class Features

Static Classes

Static classes are associated with the type/class and not an instance of the type.
E.g. Math.PI.  PI is a static property of the class Math.
A static class can have only static members and can't instantiate a static class.
Only one copy of a static class.
This is done for convenience

Sealed Classes

Sealed classes cannot be inherited.  This is for performance benefits.
String is sealed and cannot be inherited from.

Partial Classes

For extensibility
Allows you to define a class across multiple files - within the same project.
Compiler merges them together into a single class.
Provides extensibility points and is optimized away if no implementation is provided.

Access Modifiers

public - Applies to Class, Member - Meaning: No restrictions
protected - Member - Access limited to class and derived classes
internal - Class & Member - Access limited to the current assembly (Default of a class)
protected internal - Member - Access limited to the current assembly and derived types
private - Member - Access limited to the class (Default of a member)

Abstract

Applies to a class or members
Cannot be instantiated - An abstract class is designated as a base class (e.g. Animal)
Implementation of an abstract method is in the derived class and should use override to implement it.

Virtual

(Implement polymorphism)
Override members in a derived class.
Members are non-virtual by default.
Virtual method could have code in the base/parent class (unlike an abstract method).
Regular methods are dispatched based on the type of the variable
Virtual methods are dispatched based on the type of the object

If you don't override a virtual method in the base class, then the definition in the derived class hides or overlays the definition in the base class.  To get true polymorphism you need to both mark the base method virtual and the derived method as virtual.

Always use the pointy end - C#

After too many years away from regular coding I needed a refresher to get back up to speed with C#, and the newer language features that didn't exist when I was programming.   I'm going to use this blog to keep reminder notes for myself of what I've learned.