Skip to content

Instantly share code, notes, and snippets.

/*
* 引入voidable修饰符,用于修饰泛型参数。
* 被修饰的泛型参数T,可以在代码里具体化为void类型。
*/
class Task<voidable T> { } // 带有voidable修饰
class List<T> { } // 不带voidable修饰
/*
public class AsyncCompletion
{
private static readonly TaskCompletionSource<Unit> CompletedSource;
static AsyncCompletion()
{
CompletedSource = new TaskCompletionSource<Unit>();
CompletedSource.SetResult(Unit.Default);
}
public class LimitConcurrencyLevelScheduler : LocalScheduler
{
private readonly ActionBlock<Action> _actionBlock;
public LimitConcurrencyLevelScheduler(int concurrencyLevel = 0, bool singleProducerConstrained = false)
{
var options = new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = concurrencyLevel <= 0 ? 1 : concurrencyLevel,
SingleProducerConstrained = singleProducerConstrained
// 其实这题可以写的很简单,它的关键根本不是CAS操作。
// 使用一个标志(flag也好,status判断也罢)来避免Start和Dispose重入是很容易
// 想到的手段,很多同学也这么做了。大部分同学遇到的问题是:假如StartCore已经
// 启动,那么如何让Dispose方法等待其完成。有些同学用while (true),有些同学用
// 锁,但都避免不了让Dispose线程等待Start线程。
// 这里“避免等待”的关键在于要跳出思维界限:谁说DisposeCore方法一定要在Dispose
// 方法里执行的?我们就不能在Start里销毁对象吗?
///////////////////////////////////////////////////////
// Talk is cheap. Show me the code. - Linus Torvalds
///////////////////////////////////////////////////////
public abstract class CalculatorBase : IDisposable {
protected void StartCore() {
// ...
}
struct MyKey {
private readonly int _a;
private readonly int _b;
public MyKey(int a, int b) {
_a = a;
_b = b;
}
}
html, body {
height: 100%;
}
.reeder-page {
min-height: 100%;
position: relative;
}
body {
public static int Fac(int n, Func<int, int> cont) {
if (n == 0) return cont(1);
return Fac(n - 1, r => cont(n * r));
}
public class MyClass<T>
{
public class Nested { }
}
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(typeof(MyClass<>).GetNestedType("Nested")); // MyClass<>.Nested
// As we all know, the generic List<T> class in .NET doesn't
// have a RemoveMultiple method. Could you implement it for me?
// Say the elements are kept in the _items field, which is an
// array of type T. Also, use _count to keep the current number
// of elements.
// PS: You can compare two items with "==" operator.
namespace System.Collections.Generic
{
public class List<T>