Click here to Skip to main content
15,861,168 members
Articles / Web Development / HTML
Tip/Trick

Life after Death: Nullified Reference Looks Still Active

Rate me:
Please Sign up or sign in to vote.
4.80/5 (19 votes)
21 Apr 2014CPOL 20.5K   5   18
After reference to instance of a class was set to null, it seemed it was still active

One morning, still drinking my usual strong black coffee I was debugging some code that seemed fairly routine. But behavior of a code fragment looking simple appeared somewhat abnormal. Soon I realized that I was dealing with something like this (after removing all irrelevant code):

C#
var c = new C();

//...........................

c = null;

//...........................

c.Foo();  // no exception, execution goes on!

After reference to instance of a class was set to null, it seemed it was still active! And I was pretty sure that there were no tricks with regeneration on finalization.

Well, may be coffee was not strong enough. Or perhaps something wrong with my new glasses... I had repeated debugging - with the same result.

It took me some time to realize what's going on. Extension methods were added to C# for quite a time already, but not very wide spread. This is simplified code allowing the above behavior:

C#
class C
{
    //...................
}

static class CExtension
{
    public static void Foo(this C c) 
    {
        if (c == null)
            return;

        //...................
    }
}

Such a simple explanation! I found it funny and happy to share it with you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Israel Israel


  • Nov 2010: Code Project Contests - Windows Azure Apps - Winner
  • Feb 2011: Code Project Contests - Windows Azure Apps - Grand Prize Winner



Comments and Discussions

 
GeneralMy vote of 4 Pin
Sebastien GASPAR6-May-14 6:16
Sebastien GASPAR6-May-14 6:16 
GeneralMy vote of 5 Pin
Rajesh Buddaraju22-Apr-14 19:52
Rajesh Buddaraju22-Apr-14 19:52 
GeneralRe: My vote of 5 Pin
Igor Ladnik22-Apr-14 20:16
professionalIgor Ladnik22-Apr-14 20:16 
GeneralNothing new Pin
PIEBALDconsult22-Apr-14 9:57
mvePIEBALDconsult22-Apr-14 9:57 
GeneralRe: Nothing new Pin
Igor Ladnik22-Apr-14 20:18
professionalIgor Ladnik22-Apr-14 20:18 
GeneralMy vote of 5 Pin
Bruno Tagliapietra22-Apr-14 4:37
Bruno Tagliapietra22-Apr-14 4:37 
GeneralRe: My vote of 5 Pin
Igor Ladnik22-Apr-14 7:30
professionalIgor Ladnik22-Apr-14 7:30 
GeneralI don't think its funny.. Pin
Suvabrata Roy21-Apr-14 19:10
professionalSuvabrata Roy21-Apr-14 19:10 
QuestionMy vote of 5 Pin
Emre Ataseven21-Apr-14 10:21
professionalEmre Ataseven21-Apr-14 10:21 
AnswerRe: My vote of 5 Pin
Igor Ladnik21-Apr-14 16:31
professionalIgor Ladnik21-Apr-14 16:31 
GeneralThank you Pin
Tesfamichael G.21-Apr-14 6:04
Tesfamichael G.21-Apr-14 6:04 
GeneralRe: Thank you Pin
Igor Ladnik21-Apr-14 6:14
professionalIgor Ladnik21-Apr-14 6:14 
GeneralMy vote of 4 Pin
onelopez21-Apr-14 5:42
onelopez21-Apr-14 5:42 
GeneralRe: My vote of 4 Pin
Igor Ladnik21-Apr-14 5:53
professionalIgor Ladnik21-Apr-14 5:53 
GeneralRe: My vote of 4 Pin
onelopez21-Apr-14 13:10
onelopez21-Apr-14 13:10 
GeneralRe: My vote of 4 Pin
Igor Ladnik21-Apr-14 16:35
professionalIgor Ladnik21-Apr-14 16:35 
GeneralRe: My vote of 4 Pin
Oleg A.Lukin21-Apr-14 19:20
Oleg A.Lukin21-Apr-14 19:20 
GeneralRe: My vote of 4 Pin
Igor Ladnik21-Apr-14 22:32
professionalIgor Ladnik21-Apr-14 22:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.