Browse by Tags

All Tags » software C# language (RSS)
Implementing IEnumerable<T> can turn out to be tricky in certain cases. Consider the following code snippet namespace Test { class Program { static void Main( string [] args) { Consume( new List < string >() { "a" , "b"...
Posted by Senthil | with no comments
This is one topic that keeps popping up every now and then. A lot of people seem to be curious about the performance implications of using one versus the other, and not unexpectedly, a lot of different answers come up. To settle it once for all, here's...
Tee is a cool function that "clones" enumerators whatever their current state is - it basically allows you to branch off an enumerator into as many enumerators as you want, all independent of each other. You can do something like List<int>...
Every now and then, you hit some problem that a programming language helps solve very cleanly. Here's the problem. You are writing a series of functions that return a bool depending on some condition (predicates). Something like bool Foo(State t);...
Posted by Senthil | 2 comment(s)
There is a description of the bug here . The following piece of code demonstrates the bug. class Program { interface I { int X { get; set; } } struct S : I { public int X { get; set; } } static void Main(string[] args) { Func<S>(); } static void...
Posted by Senthil | 2 comment(s)
We saw in Part 1 that C# doesn't allow an instance field initializer to refer another field in the class. Before trying to figure out why, let's first see if this is a restriction imposed by C#, rather than by the CLR. Some disassembling and reassembling...
Posted by Senthil | 1 comment(s)
What do you think of the following piece of code? class Program { int x = 1; int y = x + 1; } Looks simple, except that it doesn't compile (error CS0236: A field initializer cannot reference the non-static field, method, or property 'Program.x'...
Local variable type inferencing is a new feature in C# 3.5 - a very "handy" one, in that it saves a lot of typing. Basically, it lets you do var dict = new Dictionary<string, List<int, Dictionary<int, string>>>(); instead of...
Polymorphism, which attempts to hide differences in implementation, and generics, which attemtps to highlight them by providing exact information about types, don't seem to mix very well. Consider the following fairly common pattern. class Base {...
If there was ever a poll conducted for the most favorite C# operator, I'd guess the conditional operator ?:, also known as the ternary operator, will win hands down. I find it to be one of those tools that make things shorter and clearer at the same...
Posted by Senthil | 1 comment(s)