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. why should we assigne an object to null?

why should we assigne an object to null?

Scheduled Pinned Locked Moved C#
question
12 Posts 5 Posters 0 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.
  • F Offline
    F Offline
    fateme_developer
    wrote on last edited by
    #1

    what's the diffrence between these two declarations: ObjectType objectName; ObjectType objectName=null;

    A N 2 Replies Last reply
    0
    • F fateme_developer

      what's the diffrence between these two declarations: ObjectType objectName; ObjectType objectName=null;

      A Offline
      A Offline
      arslanjatt
      wrote on last edited by
      #2

      The first statement means that you are going to declare an object and the second statement mean you are going to dispose an object. If you want to dispose an object then you assign null to that object.

      N 1 Reply Last reply
      0
      • F fateme_developer

        what's the diffrence between these two declarations: ObjectType objectName; ObjectType objectName=null;

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        They both are same. If you are using first one, compiler will give error. But for second one, you will get error only in the runtime.

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

        modified on Tuesday, June 17, 2008 3:18 AM

        1 Reply Last reply
        0
        • A arslanjatt

          The first statement means that you are going to declare an object and the second statement mean you are going to dispose an object. If you want to dispose an object then you assign null to that object.

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          arslanjatt wrote:

          The first statement means that you are going to declare an object and the second statement mean you are going to dispose an object. If you want to dispose an object then you assign null to that object.

          This not correct. Both means same. Also you don't need to assign NULL to an object which you want to dispose. It is handled automatically and an object will be garbage collected when nothing is referring it.

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

          M 1 Reply Last reply
          0
          • N N a v a n e e t h

            arslanjatt wrote:

            The first statement means that you are going to declare an object and the second statement mean you are going to dispose an object. If you want to dispose an object then you assign null to that object.

            This not correct. Both means same. Also you don't need to assign NULL to an object which you want to dispose. It is handled automatically and an object will be garbage collected when nothing is referring it.

            All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

            M Offline
            M Offline
            MarkB777
            wrote on last edited by
            #5

            Hi Navaneeth, Do developers consider it good practise to always initialise an instance to null when they are declared? For example, if I always declared variables, and set them to null as below: Class class1 = null; would it be bad practice and/or any slower than: Class class1;

            Mark Brock Click here to view my blog

            N A 2 Replies Last reply
            0
            • M MarkB777

              Hi Navaneeth, Do developers consider it good practise to always initialise an instance to null when they are declared? For example, if I always declared variables, and set them to null as below: Class class1 = null; would it be bad practice and/or any slower than: Class class1;

              Mark Brock Click here to view my blog

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              Hello Mark,

              MarkBrock wrote:

              Do developers consider it good practise to always initialise an instance to null when they are declared?

              I am not sure, but I never used to assign NULL when it is declaring.

              object obj;
              object obj1 = null;
              Console.WriteLine(obj == null);
              Console.WriteLine(obj1 == null);

              This won't compile. Compiler will give error saying obj is not assigned. So easily I can find out uninitialized variables. But if you assign NULL to it and forget to initialize, compiler won't give any error, but you will end with a NullReferenceException at the runtime. I think performance wise there won't be any difference.

              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

              1 Reply Last reply
              0
              • M MarkB777

                Hi Navaneeth, Do developers consider it good practise to always initialise an instance to null when they are declared? For example, if I always declared variables, and set them to null as below: Class class1 = null; would it be bad practice and/or any slower than: Class class1;

                Mark Brock Click here to view my blog

                A Offline
                A Offline
                Alan Balkany
                wrote on last edited by
                #7

                It's not good practice to initialize a variable to null in .NET because it's already done automatically by the CLR. It just wastes an instruction because it's redundant and unnecessary. See Effective C# by Bill Wagner.

                F 1 Reply Last reply
                0
                • A Alan Balkany

                  It's not good practice to initialize a variable to null in .NET because it's already done automatically by the CLR. It just wastes an instruction because it's redundant and unnecessary. See Effective C# by Bill Wagner.

                  F Offline
                  F Offline
                  fateme_developer
                  wrote on last edited by
                  #8

                  so they're the same in memory but not in cpu usage you mean?

                  A 1 Reply Last reply
                  0
                  • F fateme_developer

                    so they're the same in memory but not in cpu usage you mean?

                    A Offline
                    A Offline
                    Alan Balkany
                    wrote on last edited by
                    #9

                    You could say that.

                    F 2 Replies Last reply
                    0
                    • A Alan Balkany

                      You could say that.

                      F Offline
                      F Offline
                      fateme_developer
                      wrote on last edited by
                      #10

                      thanks

                      1 Reply Last reply
                      0
                      • A Alan Balkany

                        You could say that.

                        F Offline
                        F Offline
                        fateme_developer
                        wrote on last edited by
                        #11

                        Alan!I wanted to mark ur anwer as reply or sth like that,and I clicked report this message link :rolleyes: is there any way to undo it?

                        A 1 Reply Last reply
                        0
                        • F fateme_developer

                          Alan!I wanted to mark ur anwer as reply or sth like that,and I clicked report this message link :rolleyes: is there any way to undo it?

                          A Offline
                          A Offline
                          Alan Balkany
                          wrote on last edited by
                          #12

                          Ah, so THAT'S why the Code Project agents showed up at my place with a warrent!

                          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