Browse by Tags

Tagged Content List
  • Blog Post: Why is deriving a public class from an internal class illegal?

    In C# it is illegal to declare a class D whose base class B is in any way less accessible than D. I'm occasionally asked why that is. There are a number of reasons; today I'll start with a very specific scenario and then talk about a general philosophy. Suppose you and your coworker Alice are developing...
  • Blog Post: Dynamic contagion, part two

    Last time I discussed how "dynamic" tends to spread through a program like a virus : if an expression of dynamic type "touches" another expression then that other expression often also becomes of dynamic type. Today I want to describe one of the least well understood aspects of method type inference...
  • Blog Post: A method group of one

    I'm implementing the semantic analysis of dynamic expressions in Roslyn this week, so I'm fielding a lot of questions within the team on the design of the dynamic feature of C# 4. A question I get fairly frequently in this space is as follows: public class Alpha { public int Foo(string x) { ... } } ...
  • Blog Post: Is C# a strongly typed or a weakly typed language?

    Presented as a dialogue, as is my wont! Is C# a strongly typed or a weakly typed language? Yes. That is unhelpful. I don't doubt it. Interestingly, if you rephrased the question as an "and" question, the answer would be the same. What? You mean, is C# a strongly typed and a weakly typed...
  • Blog Post: Foolish consistency is foolish

    Once again today's posting is presented as a dialogue, as is my wont. Why is var sometimes required on an implicitly-typed local variable and sometimes illegal on an implicitly typed local variable? That's a good question but can you make it more precise? Start by listing the situations in which an implicitly...
  • Blog Post: Implementation-defined behaviour

    As I've mentioned several times on this blog before , C# has been carefully designed to eliminate some of the "undefined behaviour" and "implementation-defined behaviour" that you see in languages like C and C++. But I'm getting ahead of myself; I should probably start by making a few definitions. Traditionally...
  • Blog Post: null is not false, part three

    Returning now to the subject at hand: we would like to allow user-defined "overloads" of the & and | operators in C#, and if we are going to have & and | be overloadable, it seems desirable to have && and || be overloadable too. But now we have a big design problem. We typically overload...
  • Blog Post: null is not false, part two

    In Raymond Smullyan 's delightful books about the Island of Knights and Knaves -- where, you'll recall, knights make only true statements and knaves make only false statements -- the knights and knaves are of course clever literary devices to explore problems in deductive (*) logic. Smullyan, to my recollection...
  • Blog Post: null is not false

    The way you typically represent a "missing" or "invalid" value in C# is to use the "null" value of the type. Every reference type has a "null" value; that is, the reference that does not actually refer to anything. And every "normal" value type has a corresponding "nullable" value type which has a null...
  • Blog Post: Following the pattern

    Here's a question I got from a user recently: The foreach loop in C# uses a pattern-based approach to semantic analysis. LINQ in C# also uses a pattern-based approach. Why don't other features, such as the "using" statement, also use a pattern-based approach? What a great question! First off, what do...
  • Blog Post: Ref returns and ref locals

    "Ref returns" are the subject of another great question from StackOverflow that I thought I might share with a larger audience. Ever since C# 1.0 you've been able to create an "alias" to a variable by passing a "ref to a variable" to certain methods: static void M(ref int x) { x = 123; } ... int...
  • Blog Post: Optional argument corner cases, part four

    (This is the fourth and final part of a series on the corner cases of optional arguments in C# 4; part three is here .) Last time we discussed how some people think that an optional argument generates a bunch of overloads that call each other. People also sometimes incorrectly think that void M(string...
  • Blog Post: Optional argument corner cases, part three

    (This is part three of a series on the corner cases of optional arguments in C# 4; part two is here . Part four is here .) A lot of people seem to think that this: void M(string x, bool y = false) { ... whatever ... } is actually a syntactic sugar for the way you used to have to write this in C#, which...
  • Blog Post: Optional argument corner cases, part two

    (This is part two of a series on the corner cases of optional arguments in C# 4. Part one is here . Part three is here . This portion of the series was inspired by this StackOverflow question .) Last time we saw that the declared optional arguments of an interface method need not be optional arguments...
  • Blog Post: Optional argument corner cases, part one

    (This is part one of a series on the corner cases of optional arguments in C# 4. Part two is here .) In C# 4.0 we added "optional arguments"; that is, you can state in the declaration of a method's parameter that if certain arguments are omitted, then constants can be substituted for them: void M(int...
  • Blog Post: Compound Assignment, Part Two

    Last time I discussed how the compound assignment operators of the form “ x op= y ” have some perhaps unobvious behaviours in C#, namely: (1) though logically this is expanded as “ x = x op y ”, x is only evaluated once (2) for built-in operators, if necessary, a cast is inserted...
  • Blog Post: Dot NET Rocks!

    We interrupt our series on compound assignment operators with a quick note to say check out my interview on .NET Rocks! http://www.dotnetrocks.com/default.aspx?showNum=649 We have a long, rambling conversation about languages, tools, AI, you name it. Thanks to Carl and Richard for the interview;...
  • Blog Post: Never Say Never, Part Two

    Whether we have a "never" return type or not, we need to be able to determine when the end point of a method is unreachable for error reporting in methods that have non-void return type. The compiler is pretty clever about working that out; it can handle situations like int M() { try { while(true...
  • Blog Post: Never Say Never, Part One

    Can you find a lambda expression that can be implicitly converted to Func<T> for any possible T? . . . . . . . . . . . Hint : The same lambda is convertible to Action as well. . . . . . . . . . Func<int> function = () => { throw new Exception(); }; The rule for assigning lambdas to delegates...
  • Blog Post: No Backtracking, Part Two

    As i was saying last time, the nice thing about "no backtracking" is that it makes the language much easier to understand. Simple rules benefit both the compiler and the code reader; both are attempting to read the code to make sense of it. It is not always a good idea to take advantage of the compiler...
  • Blog Post: No backtracking, Part One

    A number of the articles I’ve published over the years involve “backtracking” algorithms; most recently my series on how to solve Sudoku puzzles (and more generally, all graph colouring problems) by backtracking. The idea of a backtracking algorithm is really simple and powerful: when faced with a choice...
  • Blog Post: Ambiguous Optional Parentheses, Part Three

    (This is part three of a three-part series on C# language design issues involving elided parentheses in constructor calls. Part two is here .) Last time I discussed why a particular syntactic sugar would have been rejected by the design team: because it introduces the costs of a nasty semantic analysis...
  • Blog Post: Ambiguous Optional Parentheses, Part Two

    (This is part two of a three-part series on C# language design issues involving elided parentheses in constructor calls. Part one is here . Part three is here .) Last time I discussed why in C# 3.0 the design team decided to permit elision of the argument list on a constructor call that uses the...
  • Blog Post: Ambiguous Optional Parentheses, Part One

    (This is part one of a three-part series on C# language design issues involving elided parentheses in constructor calls. Part two is here .) Another amusing question from StackOverflow , which I present here as a dialogue, as is my wont: In C# 3.0 the object initializer (and collection initializer...
  • Blog Post: Travelogue

    I've just returned from a week in Oslo, Norway, where I spoke at NDC 2010, immediately following a week in New Orleans, where I spoke at TechEd. I'm about nine hours jetlagged and glad to be home, though I had a lovely time. Many thanks in particular to all the NDC organizers who made it all happen;...
Page 1 of 3 (68 items) 123