why should we assigne an object to null?
-
what's the diffrence between these two declarations: ObjectType objectName; ObjectType objectName=null;
-
what's the diffrence between these two declarations: ObjectType objectName; ObjectType objectName=null;
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.
-
what's the diffrence between these two declarations: ObjectType objectName; ObjectType objectName=null;
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
-
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.
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
-
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
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
-
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
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
-
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
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.
-
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.
so they're the same in memory but not in cpu usage you mean?
-
so they're the same in memory but not in cpu usage you mean?
You could say that.
-
You could say that.
thanks
-
You could say that.
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?
-
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?
Ah, so THAT'S why the Code Project agents showed up at my place with a warrent!