Posts

Showing posts from August, 2017

Hashset, HashTable in c#

Image
HashSet -  This is an optimized set collection. It helps eliminates duplicate strings or elements in an array. This internally calls the UnionWith method to eliminate the duplications.  It has OverLap,  SymmetricExceptWith OverLap - This method returns true or false. It tests to see if any of the HashSet's elements are contained in the IEnumerable argument's elements. Only one equal element is required. SymmetricExceptWith - This method returns only those don't match in the collection. HashTable - The  Hashtable  class represents a collection of key-and-value pairs that are organized based on the  hash  code of the key. It uses the key to access the elements in the collection.  A  hash table  is used when you need to access elements by using key, and you can identify a useful key value. This optimizes lookups. It computes a hash of each key you add. It then uses this hash code to look up the element ver...

Difference between IEnumerable, ICollection, IList and List in c#

IEnumerable<T>  is the base interface that the following extend or implement. It doesn't allow for direct access and is readonly. So use this only if you intend to iterate over the collection. ICollection<T>  extends IEnumerable<T>  but in addition allows for adding, removing, testing whether an element is present in the collection and getting the total number of elements. It doesn't allow for directly accessing an element by index. That would be an O(n) operation as you need to start iterating over it until you find the corresponding element. IList<T>  extends  ICollection<T>  (and thus it inherits all its properties) but in addition allows for directly accessing elements by index. It's an O(1) operation. List<T>  is just a concrete implementation of the  IList<T>  interface.

Await Async in c#

      It is introduced in .net framework 4.5.  async make the method asynchronous and await suspend the calling function and returns the control to the UI. There are two ways to implement async and await. I/O bound C# Copy private readonly HttpClient _httpClient = new HttpClient(); downloadButton.Clicked += async (o, e) => { // This line will yield control to the UI as the request // from the web service is happening. // // The UI thread is now free to perform other work. var stringData = await _httpClient.GetStringAsync(URL); DoSomethingWithData(stringData); }; CPU bound C# Copy private DamageResult CalculateDamageDone ( ) { // Code omitted: // // Does an expensive calculation and returns // the result of that calculation. } calculateButton.Clicked += async (o, e) => { // This line will yield control to the UI while CalculateDamageDone() // performs its work. The UI thread is f...