Continuing on from my previous blog entry, in this entry I will talk about variant generic delegates. Just as with generic interfaces, generic delegates can use out and in to specify covariant and contravariant parameters respectively. And the MSIL equivalents are the + and – symbols. Here are three variant generic delegate declarations: delegate T [...]
Entries from May 2009
C# 4.0 and variant generic delegates
May 29th, 2009 · 1 Comment
Tags: C#/.NET
C# 4.0 and variant generic interfaces
May 27th, 2009 · 2 Comments
The CLI supports variant generic parameters for interfaces as well as delegates, and C# 4.0 has added support for that. In this blog entry I’ll talk a little about variance in generic interfaces, and in a later entry I will talk about variance in delegates. Covariance allows you to use a more derived type than [...]
Tags: C#/.NET
Named and optional parameters in C# 4.0
May 25th, 2009 · 3 Comments
One commonly heard grievance about C# was how it did not support optional parameters. Well C# 4.0 not only has that, but you can also specify parameters by name. Consider the following two examples : public void RepeatText(string text, int count = 3) { while (count– > 0) { Console.WriteLine(text); } Console.WriteLine(); } public void [...]
Tags: C#/.NET
A quick look at the C# 4.0 dynamic type
May 22nd, 2009 · 1 Comment
VS 2010 Beta 1 includes some C# 4.0 features and though I have been reading about some of the new stuff on various blogs and forums, I only got to play with it very recently. Put simply, the dynamic keyword allows you to declare and use types that are not type-checked during compilation. They are [...]
Tip : Flush the keyboard buffer from C#
May 19th, 2009 · 1 Comment
Here’s another tip from an MSDN forum discussion. Someone wanted to know how to flush the keyboard buffer in a C# console application. Here’s a simple hack to do this : private static void FlushKeyboard() { while (Console.In.Peek() != -1) Console.In.Read(); } Now you can use it as follows : char x = (char)Console.Read(); FlushKeyboard(); [...]
Tip – Sending multi-line email using the mailto: protocol
May 15th, 2009 · No Comments
A few weeks ago someone asked on the MSDN forums how he could use the mailto: protocol to send a multi-line email. The trick is to use %0D%0A in lieu of \r\n. Here’s a small C# code snippet that shows how to do this : string command = “mailto:abc@abc.com?subject=The Subject&body=Ln 1%0D%0ALn 2″; Process.Start(command);
Tags: C#/.NET