RSS

Tag Archives: interview question

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.

Advertisements
 
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.

Sources:
https://stackoverflow.com/questions/3930335/why-are-extension-methods-only-allowed-in-non-nested-non-generic-static-class

https://stackoverflow.com/questions/7163823/why-must-c-sharp-extension-methods-be-defined-in-static-classes

 
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.

Example:

Boxing

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

Unboxing

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

Oberserver

  • 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: , , ,

ASP.Net Web API Pipeline

Similar to ASP.Net MVC but not exactly the same. Pedro Felix has this self-explanatory graph depicting ASP.Net Web API

asp-net-web-api-pipeline

Head over to the full article, ASP.Net Web API Processing Architecture 

 
Leave a comment

Posted by on June 1, 2015 in General

 

Tags: ,

 
%d bloggers like this: