Static analysis of "is"

Before I get into the subject of today’s fabulous adventure, I want to congratulate the whole rest of Developer Division on the tremendously exciting product that we are formally launching today. (I’ve done very little actual coding on Visual Studio 2012 and C# 5.0, being busy with the long-lead Roslyn project.) Asynchronous programming support in…


An "is" operator puzzle, part two

As I said last time, that was a pretty easy puzzle: either FooBar, or the type of local variable x, can be a type parameter. That is: void M<FooBar>(){  int x = 0;  bool b = x is FooBar;  // legal, true if FooBar is int.  FooBar fb = (FooBar)x; // illegal} or struct FooBar…


An "is" operator puzzle, part one

It is possible for a program with some local variable x: bool b = x is FooBar; to assign true to b at runtime, even though there is no conversion, implicit or explicit, from x to FooBar allowed by the compiler! That is, FooBar foobar = (FooBar)x; would not be allowed by the compiler in…


When is a cast not a cast?

I’m asked a lot of questions about conversion logic in C#, which is not that surprising. Conversions are common, and the rules are pretty complicated. Here’s some code I was asked about recently; I’ve stripped it down to its essence for clarity: class C<T> {}class D{  public static C<U> M<U>(C<bool> c)  {    return something;  }}public…


Is is as or is as is?

Today a question about the is and as operators: is the is operator implemented as a syntactic sugar for the as operator, or is the as operator implemented as a syntactic sugar for the is operator? More briefly, is is as or is as is? Perhaps some sample code would be more clear. This code…


Cast operators do not obey the distributive law

Another interesting question from StackOverflow. Consider the following unfortunate situation: object result;bool isDecimal = GetAmount(out result);decimal amount = (decimal)(isDecimal ? result : 0); The developer who wrote this code was quite surprised to discover that it compiles and then throws “invalid cast exception” if the alternative branch is taken. Anyone see why? In regular algebra,…


Ignoring parentheses

Yet another amusing question from StackOverflow: is there a difference between “return something;” and “return (something);” in C#? In practice, there is no difference. In theory there could be a difference. There are three interesting points in the C# specification where this could present a problem. First, conversion of anonymous functions to delegate types and…


It’s the most wonderful time of the year

Here’s a little holiday cheer for you all. Or, at least for you all in Commonwealth countries. static object M<T>(T t) where T : struct{  return t;} int ii = 10;int? jj = 20;object xx = ii;object yy = jj;System.ValueType zz = ii;IComparable aa = ii;System.Enum bb = MidpointRounding.ToEven;object cc = M(ii); I hope you’re having…


What’s the difference between "as" and "cast" operators?

Most people will tell you that the difference between “(Alpha) bravo” and “bravo as Alpha” is that the former throws an exception if the conversion fails, whereas the latter returns null. Though this is correct, and this is the most obvious difference, it’s not the only difference. There are pitfalls to watch out for here….


Why does char convert implicitly to ushort but not vice versa?

Another good question from StackOverflow. Why is there an implicit conversion from char to ushort, but only an explicit conversion from ushort to char? Why did the designers of the language believe that these asymmetrical rules were sensible rules to add to the language? Well, first off, the obvious things which would prevent either conversion…