131

Due to a bug that was fixed in C# 4, the following program prints true. (Try it in LINQPad)

void Main() { new Derived(); }

class Base {
    public Base(Func<string> valueMaker) { Console.WriteLine(valueMaker()); }
}
class Derived : Base {
    string CheckNull() { return "Am I null? " + (this == null); }
    public Derived() : base(() => CheckNull()) { }
}

In VS2008 in Release mode, it throws an InvalidProgramException. (In Debug mode, it works fine)

In VS2010 Beta 2, it doesn't compile (I didn't try Beta 1); I learned that the hard way

Is there any other way to make this == null in pure C#?

16
  • 3
    It's most likely a bug in C# 3.0 compiler. It works the way it should in C# 4.0. Oct 21, 2009 at 13:07
  • 83
    @SLaks: Problem with bugs is that you can expect them to be fixed at some point so finding them "useful" is probably not wise. Oct 21, 2009 at 13:13
  • 6
    thanks! didn't know about LINQPad. it's cool!
    – thorn0
    Oct 21, 2009 at 13:19
  • 8
    In what way, exactly, is this useful?
    – Allen Rice
    Oct 21, 2009 at 13:42
  • 6
    how was this bug useful? Oct 21, 2009 at 13:42

6 Answers 6

75

This observation has been posted on StackOverflow in another question earlier today.

Marc's great answer to that question indicates that according to the spec (section 7.5.7), you shouldn't be able to access this in that context and the ability to do so in C# 3.0 compiler is a bug. C# 4.0 compiler is behaving correctly according to the spec (even in Beta 1, this is a compile time error):

§ 7.5.7 This access

A this-access consists of the reserved word this.

this-access:

this

A this-access is permitted only in the block of an instance constructor, an instance method, or an instance accessor.

3
  • 2
    I do not see, why in the code presented in this question, usage of keyword "this" is invalid. The method CheckNull is a normal instance method, nonstatic. Using "this" is 100% valid in such method, and even comparing this to null is valid. The error is in the base init line: it is the attempt to pass instance-bounded delegate as a parameter to the base ctor. This is the bug (a hole in sematic checks) in the compiler: it should NOT be possible. You are not allowed to write : base(CheckNull()) if CheckNull is not static, and alike you should not be able to inline an instance-bound lambda. Aug 14, 2012 at 0:29
  • 5
    @quetzalcoatl: this in CheckNull method is legal. What is not legal is the implicit this-access in () => CheckNull(), essentially () => this.CheckNull(), which is running outside the block of an instance constructor. I agree that the part of spec I cite is mostly focused on the syntactic legality of this keyword, and probably another part addresses this issue more precisely, but it is easy to conceptually extrapolate from this part of spec as well. Aug 14, 2012 at 0:46
  • 2
    Sorry, I disgree. While I know that (and written that in the comment above) and you also know that - you have not mentioned the actual cause of the problem in your (accepted) answer. The answer is accepted - so seemingly the author grapsed it too. But I doubt that all readers will be as bright and fluent in lambdas to recognize a instancebound-lambda versus static-lambda at first sight and map that to 'this' and problems with emitted IL :) This is why I added my three cents. Aside of that, I agree with everything else what was found, analyzed and described by you and others:) Aug 14, 2012 at 1:05
24

The raw decompilation (Reflector with no optimizations) of the Debug mode binary is:

private class Derived : Program.Base
{
    // Methods
    public Derived()
    {
        base..ctor(new Func<string>(Program.Derived.<.ctor>b__0));
        return;
    }

    [CompilerGenerated]
    private static string <.ctor>b__0()
    {
        string CS$1$0000;
        CS$1$0000 = CS$1$0000.CheckNull();
    Label_0009:
        return CS$1$0000;
    }

    private string CheckNull()
    {
        string CS$1$0000;
        CS$1$0000 = "Am I null? " + ((bool) (this == null));
    Label_0017:
        return CS$1$0000;
    }
}

The CompilerGenerated method doesn't make sense; if you look at the IL (below), it's calling the method on a null string (!).

   .locals init (
        [0] string CS$1$0000)
    L_0000: ldloc.0 
    L_0001: call instance string CompilerBug.Program/Derived::CheckNull()
    L_0006: stloc.0 
    L_0007: br.s L_0009
    L_0009: ldloc.0 
    L_000a: ret 

In Release mode, the local variable is optimized away, so it tries to push a non-existant variable on to the stack.

    L_0000: ldloc.0 
    L_0001: call instance string CompilerBug.Program/Derived::CheckNull()
    L_0006: ret 

(Reflector crashes when turning it into C#)


EDIT: Does anyone (Eric Lippert?) know why the compiler emits the ldloc?

11

I have had that! (and got proof too)

alt text

2
  • 2
    Was late, was a sign I should stop coding :) Was hacking our with DLR stuff IIRC.
    – leppie
    Oct 21, 2009 at 17:43
  • make a debugger visualizer (DebuggerDisplay) for whatever 'this' is, and make that fool you that's null? :D just sayin'
    – user90843
    Apr 12, 2013 at 9:10
10

This isn't a "bug". This is you abusing the type system. You are never supposed to pass a reference to the current instance (this) to anyone within a constructor.

I could create a similar "bug" by calling a virtual method within the base class constructor as well.

Just because you can do something bad doesn't mean its a bug when you get bit by it.

10
  • 14
    It is a compiler bug. It generates invalid IL. (Read my answer)
    – SLaks
    Oct 21, 2009 at 13:11
  • The context is static, so you should not be allowed an instance method reference at that stage.
    – leppie
    Oct 21, 2009 at 13:12
  • 10
    @Will: It's a compiler bug. The compiler is supposed to generate valid, verifiable code for that code snippet or spit out an error message. When a compiler does not behave according to the spec, it is buggy. Oct 21, 2009 at 13:22
  • 2
    @Will#4: When I wrote the code, I hadn't thought about the implications. I only realized that it didn't make sense when it stopped compiling in VS2010. –
    – SLaks
    Oct 21, 2009 at 13:48
  • 3
    By the way, the virtual method call in constructor is a completely valid operation. It's just not recommended. It may result in logical disasters but never an InvalidProgramException. Oct 21, 2009 at 14:02
3

I could be wrong, but I'm pretty sure if your object is null there's never going to be a scenario where this applies.

For instance, how would you call CheckNull?

Derived derived = null;
Console.WriteLine(derived.CheckNull()); // this should throw a NullReferenceException
5
  • 3
    In a lambda in the constructor argument. Read the entire code snippet. (And try it if you don't believe me)
    – SLaks
    Oct 21, 2009 at 13:01
  • I agree although I do remember faintly something about how in C++ an object didn't have reference within it's constructor and I'm wondering if the (this == null) scenario is used in those cases to check whether a call to a method was made from the object's constructor before exposing a pointer to "this". Though, as far as I know in C#, there shouldn't be any cases where "this" would ever be null, not even in the Dispose or finalization methods.
    – jpierson
    Oct 21, 2009 at 13:05
  • I guess my point is that the very idea of this is mutually exclusive of the possibility of being null--sort of a "Cogito, ergo sum" of computer programming. Therefore your desire to use the expression this == null and ever have it return true strikes me as misguided.
    – Dan Tao
    Oct 21, 2009 at 14:19
  • In other words: I did read your code; what I'm saying is that I question what you were trying to accomplish in the first place.
    – Dan Tao
    Oct 21, 2009 at 14:20
  • This code simply demonstrates the bug, and, as you pointed out, is utterly useless. To see real useful code, read my second answer.
    – SLaks
    Oct 21, 2009 at 16:23
-1

Not sure if this is what you are looking for

    public static T CheckForNull<T>(object primary, T Default)
    {
        try
        {
            if (primary != null && !(primary is DBNull))
                return (T)Convert.ChangeType(primary, typeof(T));
            else if (Default.GetType() == typeof(T))
                return Default;
        }
        catch (Exception e)
        {
            throw new Exception("C:CFN.1 - " + e.Message + "Unexpected object type of " + primary.GetType().ToString() + " instead of " + typeof(T).ToString());
        }
        return default(T);
    }

example: UserID = CheckForNull(Request.QueryString["UserID"], 147);

2
  • 13
    You completely misunderstood the question.
    – SLaks
    May 7, 2010 at 16:24
  • 1
    I figured as much. Thought I'd try anyway. May 7, 2010 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.