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.

ConcurrentDictionary.GetOrAdd()

Advertisements

Python’s enumerate

enumerate(sequence, start=0)
  • Takes as input a sequence (string, unicode string, list, tuple, bytearray, buffer or xrange) or iterable, and an arbitrary start index
  • Returns an iterator which provides tuples containing the count and value
  • Useful to generate the index corresponding to each element:
for i, animal in enumerate(["cat", "dog", "mouse"], 1):
    print("Animal #{} is a {}".format(i, animal))
PEP 279 — The enumerate() built-in function
enumerate(sequence, start=0)

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

CSS Positioning

Fixed

  • Doesn’t participate in document flow (0 height, 0 width)
  • Fixed position on screen, even after scrolling
  • Actually a special case of absolute, but relative to the viewport

Absolute

  • Doesn’t participate in document flow (0 height, 0 width)
  • Fixed position on initial screen layout, can be scrolled away
  • Will be relative to the closest parent who has a position of absolute, fixed or relative (or the window if none)

Relative

  • Maintains height and width in original position
  • Positioned according to itself (e.g. top: 10px is 10px from the top of the original position, not the container)

Static

  • Default behaviour of elements
  • Laid out according to normal flow (i.e. “not positioned”)
  • Position properties (top, bottom, left, right) don’t have any effect

Inherit

  • Inherits positioning from its (direct) parent element

Javascript Object Literal

  • Comma separated list of name-value pairs surrounded by curly braces
  • Encapsulates data in an object
var person = {
    name : "Jim", 
    age: 100
};
  • Can contain array literals, functions or nested object literals
  • Empty objects can also be created with
var empty = { };

which is identical to var empty = new Object()

  • Usually accessed using dot notation (person.name), but can also be accessed with bracket notation (person[‘name’])
  • Bracket notation must be used to accessfor non-standard identifier names (e.g. ‘!’, ‘’)

Static vs dynamic resources

Static

  • Always loaded on window/page load (even if not used)
  • Only ever loaded once
  • No forward referencing – must be declared above where it is used

Dynamic

  • Not loaded on window/page load (can improve start up time)
  • Reloaded each time the resource is changed (overhead to watch for changes)
  • Can only be used to set dependency properties