In this article I present an introduction to generic List types in the C# language. I assume an audience familiar with the principles of structured programming, including parameters, arrays and procedures, but with little experience with object-oriented programming. I will motivate an example for an encapsulated collection class, and then introduce generics as a means to improve our collection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function sequence<T>( | |
map: Map<string, Promise<T>> | |
): Promise<Map<string, T>> { | |
return Promise.all( | |
Array.from(map, ([key, promisedValue]) => | |
promisedValue.then<[string, T]>(value => [key, value]) | |
) | |
).then((entries) => new Map(entries)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Keybase proof | |
I hereby claim: | |
* I am guhou on github. | |
* I am guhou (https://keybase.io/guhou) on keybase. | |
* I have a public key ASAZNEe0DRKu9xFDlHFgr6TGZPyE9zmMdBt86gMWZixpUQo | |
To claim this, I am signing this object: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
public class Cache<K, V> where V : class | |
{ | |
private readonly Dictionary<K, WeakReference<V>> _data; | |
public Cache() | |
{ | |
_data = new Dictionary<K, WeakReference<V>>(); |