Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to determine the Type of a null-Reference ?

How to determine the Type of a null-Reference ?

Scheduled Pinned Locked Moved C#
tutorialquestion
12 Posts 8 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MarkPhB

    Hi can anybody tell me how i determine the Type of a null-Reference ?

    MyClass mc = null;
    
    mc.GetType() // throws a NullReferenceException
    
    typeof(mc) // work only with Classnames
    

    I just want to know the Type of "mc" when it is null.

    S Offline
    S Offline
    SeMartens
    wrote on last edited by
    #2

    Hmmm, what about using the statement "is"?

    if(mc is MyClass) {
    }

    Does this work? Just a guess. Sebastian

    M 1 Reply Last reply
    0
    • S SeMartens

      Hmmm, what about using the statement "is"?

      if(mc is MyClass) {
      }

      Does this work? Just a guess. Sebastian

      M Offline
      M Offline
      MarkPhB
      wrote on last edited by
      #3

      no this doesn't work

      ( ( ( MyClass ) null ) is MyClass )
      

      returns false.

      1 Reply Last reply
      0
      • M MarkPhB

        Hi can anybody tell me how i determine the Type of a null-Reference ?

        MyClass mc = null;
        
        mc.GetType() // throws a NullReferenceException
        
        typeof(mc) // work only with Classnames
        

        I just want to know the Type of "mc" when it is null.

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #4

        How can you possibly call a method in an object if it's null?

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        C M 2 Replies Last reply
        0
        • realJSOPR realJSOP

          How can you possibly call a method in an object if it's null?

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #5

          John Simmons / outlaw programmer wrote:

          How can you possibly call a method in an object if it's null?

          When it is a crazy extension method[^]

          Recent blog posts: *Method hiding Vs. overriding *Microsoft Surface *SQL Server / Visual Studio install order My Blog

          D 1 Reply Last reply
          0
          • realJSOPR realJSOP

            How can you possibly call a method in an object if it's null?

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            M Offline
            M Offline
            MarkPhB
            wrote on last edited by
            #6

            no such thing but i thought there would be some reflection methode wich can tell me this

            1 Reply Last reply
            0
            • M MarkPhB

              Hi can anybody tell me how i determine the Type of a null-Reference ?

              MyClass mc = null;
              
              mc.GetType() // throws a NullReferenceException
              
              typeof(mc) // work only with Classnames
              

              I just want to know the Type of "mc" when it is null.

              S Offline
              S Offline
              Simon P Stevens
              wrote on last edited by
              #7

              You can't. A null object doesn't have a type, that's why it throws an exception. Reflection is for viewing the runtime state of objects and during runtime a null object is null and has no type. If you need a Type object, you can use typeof(MyClass). It's like asking if a pen is made from plastic or metal before it's been made. You can't. You can ask afterwards. But before, all you can do is look at the construction materials in the blueprint for a PlasticPen or a MetalPen (the classes), you don't know which it is until after you've created it.

              Simon

              M 1 Reply Last reply
              0
              • C Colin Angus Mackay

                John Simmons / outlaw programmer wrote:

                How can you possibly call a method in an object if it's null?

                When it is a crazy extension method[^]

                Recent blog posts: *Method hiding Vs. overriding *Microsoft Surface *SQL Server / Visual Studio install order My Blog

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #8

                Interesting! I never considerd using extension methods for that sort of stuff, as you say in the blog, probably not the best idea though. The OP could do this then:

                public static class Extensions
                {
                public static Type GetTypeIfNull(this MyClass instance)
                {
                return typeof(MyClass);
                }
                }

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                1 Reply Last reply
                0
                • S Simon P Stevens

                  You can't. A null object doesn't have a type, that's why it throws an exception. Reflection is for viewing the runtime state of objects and during runtime a null object is null and has no type. If you need a Type object, you can use typeof(MyClass). It's like asking if a pen is made from plastic or metal before it's been made. You can't. You can ask afterwards. But before, all you can do is look at the construction materials in the blueprint for a PlasticPen or a MetalPen (the classes), you don't know which it is until after you've created it.

                  Simon

                  M Offline
                  M Offline
                  Mark Churchill
                  wrote on last edited by
                  #9

                  Correct. I gave you 5. As in the rating, not the gesture. I can give you 5 as in the gesture if you like though: *holds up palm*

                  Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                  Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                  S 1 Reply Last reply
                  0
                  • M Mark Churchill

                    Correct. I gave you 5. As in the rating, not the gesture. I can give you 5 as in the gesture if you like though: *holds up palm*

                    Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                    Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                    S Offline
                    S Offline
                    Simon P Stevens
                    wrote on last edited by
                    #10

                    Thanks. *slaps raised palm* :laugh:

                    Simon

                    M 1 Reply Last reply
                    0
                    • S Simon P Stevens

                      Thanks. *slaps raised palm* :laugh:

                      Simon

                      M Offline
                      M Offline
                      Mark Churchill
                      wrote on last edited by
                      #11

                      :laugh: Glad you didn't leave me hanging :D

                      Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                      Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                      1 Reply Last reply
                      0
                      • M MarkPhB

                        Hi can anybody tell me how i determine the Type of a null-Reference ?

                        MyClass mc = null;
                        
                        mc.GetType() // throws a NullReferenceException
                        
                        typeof(mc) // work only with Classnames
                        

                        I just want to know the Type of "mc" when it is null.

                        S Offline
                        S Offline
                        Scott Dorman
                        wrote on last edited by
                        #12

                        One thing no one really addressed is why you need to be able to do this? What is the problem you are trying to solve where you think you need to know the type of a null object?

                        Scott Dorman

                        Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


                        Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups