Optimistic concurrency control

Optimistic concurrency control is a technique used for concurrency.

  • It is used in databases and internally by concurrent data structure libraries
  • It assumes that actions will generally complete without contention, so proceeds to perform the update immediately, then checks afterwards if the action can be committed or must be rolled back
  • Avoiding the use of locks provides a performance boost, especially for .NET code where a lock could require an expensive entry into kernel mode
  • Care must be taken in use and implementation, as high load can lead to increased contention, which increases the load etc.




Using an alternative namespace in LINQPad

LINQPad is a great tool which hides the complexity of a .NET app to allow fast development and testing of arbitrary pieces of code.

Unfortunately, it also hides the namespace definition, so it’s not possible out of the box to place a type in another namespace.

Fortunately there is a workaround, which consists of closing the implicit namespace which Main lives in, adding your custom namespace code, then opening a class EOF at the end:

	void Main()

namespace AlternativeNamespace
	public static class Foo
		public static void PrintNamespace()
			Console.WriteLine($"Foo namespace: {MethodBase.GetCurrentMethod().DeclaringType.Namespace}");

class EOF

Foo namespace: AlternativeNamespace

Culture aware DateTime formatting

There are various DateTime.ToString() format specifiers which are culture aware, which can be very useful to show the date in a native format, e.g. Christmas day 2017

Culture ToString(“d”) ToString(“g”)
en-GB 25/12/2017 25/12/2017 00:00
en-US 12/25/2017 12/25/2017 12:00 AM
en-CA 2017-12-25 2017-12-25 12:00 AM

However, note the ‘official’ format isn’t always that most used by the people – see en-CA above.

ConcurrentDictionary.GetOrAdd() facts

In any combination of any number of threads running for any values with the same key:

  • It is guaranteed that only one item will be added
    • and this will be the only call to which true is returned.
  • It is not guaranteed that the delegate overload will only execute the delegate once
    • because the class uses optimistic concurrency control.
    • The implicit assumption is that it’s rare for multiple concurrent calls to occur in reality, so more efficient to compute anyway, then replace or throw away as appropriate.
    • If the delegate is expensive or has unwanted side effects, a Lazy<> can be used instead.


User Control vs Custom Control

Custom Control

  • Extends or creates an entirely custom, usually complex, control
  • Derives from Control or any of its subclasses
  • Usually has a pluggable interface
  • Usually defined in C# with a style inside the theme resource dictionary
  • Includes implementation and usually default template interface and style, but expected to be changed/skinned on by consumer

 User Control

Weakly vs. Strong Named Assembly

A Weakly-Named Assembly is an assembly is an assembly which includes the following information:

  • Name
  • Version number
  • Culture information (i.e. the locale used for things like date format, calendar etc. e.g. en-gb)

Strongly-Named Assembly is an assembly which in addition to the above, includes:

  • A signature
  • The public key of the signatory

This extra information ensures that:

  • The correct assembly can always be located, even if there happens to be two identically weakly named assemblies (the chances of the authors having the same key pair in addition to naming their assembly identically are negligible).
  • Anyone about to run the assembly can check whether the stated author did in fact write and sign the code (assuming their private key has not been compromised).