When to dispose CancellationTokenSource?

Speaking about whether it’s really necessary to call Dispose on CancellationTokenSource… I had a memory leak in my project and it turned out that CancellationTokenSource was the problem. My project has a service, that is constantly reading database and fires off different tasks, and I was passing linked cancellation tokens to my workers, so even … Read more

How does the C# compiler detect COM types?

By no means am I an expert in this, but I stumbled recently on what I think you want: the CoClass attribute class. [System.Runtime.InteropServices.CoClass(typeof(Test))] public interface Dummy { } A coclass supplies concrete implementation(s) of one or more interfaces. In COM, such concrete implementations can be written in any programming language that supports COM component … Read more

Why doesn’t C# infer my generic types?

A bunch of people have pointed out that C# does not make inferences based on constraints. That is correct, and relevant to the question. Inferences are made by examining arguments and their corresponding formal parameter types and that is the only source of inference information. A bunch of people have then linked to this article: … Read more

Covariance and contravariance real world example

Here’s what I put together to help me understand the difference public interface ICovariant<out T> { } public interface IContravariant<in T> { } public class Covariant<T> : ICovariant<T> { } public class Contravariant<T> : IContravariant<T> { } public class Fruit { } public class Apple : Fruit { } public class TheInsAndOuts { public void … Read more