Tag Archives: interview question

Left-Shift Operator Is Limited?

Why is left-shift operator with 32 result in lower number?

int i = 1;
(i << 0).Dump(); // Result is 1
(i << 1).Dump(); // Result is 2
(i << 2).Dump(); // Result is 3
(i << 3).Dump(); // Result is 8
(i << 10).Dump(); // Result is 1024
(i << 20).Dump(); // Result is 1048576
(i << 30).Dump(); // Result is 1073741824
(i << 31).Dump(); // Result is -2147483648
(i << 32).Dump(); // Result is 1 -> WHY?
Leave a comment

Posted by on March 6, 2018 in General


Tags: , ,

Indexer-Supported Collection in C#

Indexer is a faster way to search a list / collection in C#, no looping is necessary.

Classes that support indexer:

  • Array []
  • List<T>
  • Dictionary<Key, Value>
  • ILookup<Key, Value>

Classes that don’t support indexer:

  • IEnumerable<T>
  • HashSet<T>
  • ICollection<T>

For classes that don’t support indexer, there’s ElementAt LINQ extension method.

Leave a comment

Posted by on September 28, 2017 in General


Tags: , ,

Why Must Extension Methods Be Declared in a Static Class?

Some possible answers:

  • The purpose of Extension Methods is to make LINQ work and LINQ only needs extension methods to be in a static, non-generic, non-nested class.
  • Easier for compiler to work with. Compiler and VS’s Intellisense can easily locate the methods.
  • Grouping the helper methods together.


Leave a comment

Posted by on September 20, 2017 in General


Tags: ,

Mutual Exclusion

The requirement of ensuring that no two concurrent processes are in their critical section at the same time; it is a basic requirement in concurrency control, to prevent race conditions.

Source: Wikipedia

Leave a comment

Posted by on June 23, 2015 in General


Tags: ,

Producer-consumer Problem

Two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer’s job is to generate a piece of data, put it into the buffer and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time. The problem is to make sure that the producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to remove data from an empty buffer.

While it may sound like a simple problem, the solution is not as easy. Not even simple. There are many sub-impediments you have to watch:

  • Locking / unlocking mechanism.
  • Multiple access to the semaphore.
  • Concurrency and mutual exclusion / race conditions.

Source: Wikipedia

Leave a comment

Posted by on June 17, 2015 in General


Tags: ,

Boxing x Unboxing

Boxing and unboxing is not to be confused with downcast / upcast.

  • Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type.
  • When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap.
  • Unboxing extracts the value type from the object.
  • Boxing is implicit
  • Unboxing is explicit.
  • The concept of boxing and unboxing underlies the C# view that a value of any type can be treated as an object.



int i = 123;
// The following line boxes i. 
object o = i;


o = 123;
i = (int)o;  // unboxing

Reference: MSDN

Leave a comment

Posted by on June 5, 2015 in General


Tags: , , , ,

Observer vs Publish Subscribe (Pub Sub) Pattern


  • The recipient knows who’s the sender of the message.
  • Unicast, send one, receive by one.
  • Asynchronous.
  • Straight forward, from sender to recipient directly.

Pub Sub

  • Both sender and recipient don’t know who send or receive the message. They just don’t need to know.
  • Multicast approach, send one, every one receives.
  • Asynchronous, messages can be sent to multiple recipients at the same time.
  • Message queue based, there’s some kind of container to queue a message.
Leave a comment

Posted by on June 3, 2015 in General


Tags: , , ,

%d bloggers like this: