C# Refreshers

I had an interview recently that really challenged me. The interview was a collaborative coding challenge using C#. I was reminded of many C# features (datatypes, operators, etc), and I was taught about principles & performance characteristics. I outline these below because they inspired me!

Reserved Keywords

Some reserved keywords:

Null Coalescing Operator ?? and ??=

More here Some examples:


Null-Conditional Operator ?.

A variation on the . member access operator It can also be used for index access operator [...] becomes ?[...]

Error with Chaining Null-Conditional

I thought this operator couldn’t be used on every type. I got an error on this line:

SortedEventsByTicker // a SortedList<TKey,TValue>
            ?.Value // <--------- error

The error was:

Operator ‘?’ cannot be applied to operand of type ‘KeyValuePair<long, Solution.TradingEvent>’

When I look closer, it’s not complaining about the operator ?. being used on a KeyValuePair, it’s complaining about operator ? (the conditional/ternary operator?, maybe the nullability operator?)

I fixed the error by adding parentheses, which is why I thought associativity was part of the problem…

(SortedEventsByTicker // a SortedList<TKey,TValue>
            ?.LastOrDefault()) // <------- parentheses


It’s also interesting to learn that ?. is “short-circuiting”.


More on the LastOrDefault method here

The default value for reference and nullable types is null. The LastOrDefault method does not provide a way to specify a default value. If you want to specify a default value other than default(TSource), use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method as described in the Example section.

Classes (Reference Types) are Nullable

More here. Just a quick reminder. Other “primitive” types must be made nullable using the ? operator like float -> float?

System.Collections.Generic Datatypes & Performance Characteristics

More on the System.Collections.Generic here and performance characteristics here


Inserting into a List is:




I hadn’t used SortedList before… TODO