Browse by Tags

Tagged Content List
  • Blog Post: 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...
  • Blog Post: 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 { /* ... */ } void M<X>...
  • Blog Post: 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 that same program...
  • Blog Post: 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>...
  • Blog Post: 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 bool...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: Why do ref and out parameters not allow type variation?

    Here's a good question from StackOverflow : If you have a method that takes an "X" then you have to pass an expression of type X or something convertible to X . Say, an expression of a type derived from X. But if you have a method that takes a "ref X", you have to pass a ref to a variable of type...
  • Blog Post: Not everything derives from object

    I hear a lot of myths about C#. Usually the myths have some germ of truth to them, like " value types are always allocated on the stack ". If you replace "always" with "sometimes", then the incorrect mythical statement becomes correct. One I hear quite frequently is "in C# every type derives from...
  • Blog Post: The void is invariant

    [UPDATES below] A while back I described a kind of variance that we’ve supported since C# 2.0. When assigning a method group to a delegate type, such that both the selected method and the delegate target agree that their return type is a reference type, then the conversion is allowed to be covariant...
  • Blog Post: Representation and Identity

    (Note: not to be confused with Inheritance and Representation .) I get a fair number of questions about the C# cast operator. The most frequent question I get is: short sss = 123; object ooo = sss; // Box the short. int iii = (int) sss; // Perfectly legal. int jjj = (int) (short) ooo; // Perfectly...
  • Blog Post: Covariance and Contravariance, Part Eleven: To infinity, but not beyond

    UPDATE: Andrew Kennedy, author of the paper I reference below, was good enough to point out some corrections and omissions, which I have addressed. Thanks Andrew! As I've discussed at length in this space, we are considering adding covariance and contravariance on delegate and interface types parameterized...
  • Blog Post: Even More Conversion Trivia, Part Two

    Reader Barry Kelly came up with the solution I was thinking of for my trivia question yesterday. If the value in question is, say, a nullable integer which has no value then casting it explicitly to object results in the null object. Calling a virtual method on a null object results in an exception at...
  • Blog Post: Even More Conversion Trivia

    I learn something new about C# every day. This is a subtle and tricky language. Pop quiz: foo is of a type which has a base class which has made a normal, everyday virtual override of System.Object.ToString . There are no additional overrides, hides, etc of ToString anywhere in foo 's inheritance...
  • Blog Post: Chained user-defined explicit conversions in C#, Part Three

    Jeroen Frijters knew the answer to my challenge of last time: how is it that Foo foo = new Foo(); can cause a runtime conversion failure? And how is it that Bar bar = (Bar)(new Baz()); can succeed even if there is no user-defined conversion or built-in implicit conversion between Baz and Bar ? The...
  • Blog Post: Chained user-defined explicit conversions in C#, Part Two

    Reader Larry Lard asks a follow-up question regarding the subject of Monday’s blog entry . Why is it that the compiler knows that (int)(new Base()) will always fail, and therefore makes the conversion illegal, but does not know that (Derived)(new Base()) will always fail, and make that conversion illegal...
  • Blog Post: Chained user-defined explicit conversions in C#

    Reader Niall asked me why the following code compiles but produces an exception at runtime: class Base {} class Derived : Base {} class Castable { public static explicit operator Base() { return new Base(); } } // ... Derived d = (Derived)(new Castable()); It should be clear why this produces an...
Page 1 of 1 (20 items)