Using an alternative namespace in LINQPad

LINDPad 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

Default empty delegate “no-op” event

Assigning a default empty delegate to an event on declaration like so

public event EventHandler FooEvent = delegate { };

+ Cannot cause a NullReferenceException by doing FooEvent(sender, args), as always at least one event subscriber

+ No need for null check, which makes code intent clearer

– Uses extra memory to create this empty event handler which is always called but never does anything useful

– Bypasses well known code pattern, which may confuse people later

if (FooEvent != null)


    FooEvent(this, BarArgs);