|
-
Dec 22nd, 2008, 12:17 PM
#1
Thread Starter
Lively Member
[3.0/LINQ] Please clarify
hi friends
can any body tell me what is the purpose of below syntax in c#
list<classname>classobject=null;
if any body have an idea please share with me.if possible plz give me an example.
Thanks in advance
Regards
Kishore
-
Dec 22nd, 2008, 12:31 PM
#2
Re: [3.0/LINQ] Please clarify
That isn't linq - just a declaration.
You end up with a variable named classobject capable of pointing to an instance of a Generic.List containing classname objects. The "= null" however, is redundant.
-
Dec 22nd, 2008, 01:26 PM
#3
Re: [3.0/LINQ] Please clarify
If you are familiar with VB.NET, it's equivilent to this:
Dim classobject as List(Of classname) = Nothing
syntax - No one said it was LINQ. the LINQ in the subject is the default for when you select FW3 from the C# forum.... I've complained about that a number oftimes (as LINQ is more than jsut C#).... but because of the hacks & changes, it ain't gonna change any time soon, if ever.
-tg
-
Dec 22nd, 2008, 01:54 PM
#4
Re: [3.0/LINQ] Please clarify
 Originally Posted by techgnome
syntax - No one said it was LINQ. the LINQ in the subject is the default for when you select FW3 from the C# forum.... I've complained about that a number oftimes (as LINQ is more than jsut C#).... but because of the hacks & changes, it ain't gonna change any time soon, if ever.
Ahhh, makes sense. My mistake, was not aware.
-
Dec 22nd, 2008, 06:43 PM
#5
Re: [3.0/LINQ] Please clarify
 Originally Posted by syntaxeater
The "= null" however, is redundant.
Not so, or at least not necessarily so. If you do this:
CSharp Code:
private void Form1_Load(object sender, EventArgs e) { List<object> list; this.DoSomething(ref list); } private void DoSomething(ref List<object> list) { // ... }
you'll get a compilation error "Use of unassigned local variable" but if you do this:
CSharp Code:
private void Form1_Load(object sender, EventArgs e) { List<object> list = null; this.DoSomething(ref list); } private void DoSomething(ref List<object> list) { // ... }
you won't. By assigning null explicitly you are telling the compiler that you specifically want the variable to be null and you'll take responsibility for making sure that a NullReferenceException doesn't occur. By refusing to compile without the assignment the compiler ensures that you haven't just forgotten to assign a value.
-
Dec 28th, 2008, 06:14 PM
#6
Hyperactive Member
Re: [3.0/LINQ] Please clarify
This is not an answer to the question but rather another question under the same topic. If i have the following:
int x=4;
Then I create a variable called x, assign 4 bytes of memory since its int and allocate 4 to this portion of memory. However if i have:
int x=null;
As far as i know NULL means nothing and its either a data type of a keyword in most languages. So is 4 bytes of memory allocated in this case also?
Jennifer.
-
Dec 28th, 2008, 07:24 PM
#7
Re: [3.0/LINQ] Please clarify
 Originally Posted by JenniferBabe
This is not an answer to the question but rather another question under the same topic. If i have the following:
int x=4;
Then I create a variable called x, assign 4 bytes of memory since its int and allocate 4 to this portion of memory. However if i have:
int x=null;
As far as i know NULL means nothing and its either a data type of a keyword in most languages. So is 4 bytes of memory allocated in this case also?
Jennifer.
It must be because the variable is type int and int is ALWAYS 32-bits wide. You are reading that as one statement because in C# it is but when the C# code is compiled to MSIL the creation of the variable is completely separate to the assignment. Consider this:
CSharp Code:
int x = null;
x = int.MaxValue;
How could the second line be executed if the first line didn't create a 32-bit variable?
The size of EVERY variable is determined by its TYPE, not its VALUE. The value is COMPLETELY irrelevant to the amount of space a variable occupies. ALL reference type variables occupy 32 bits on a 32-bit system because that's the amount of space a memory address requires. The size of a value type variable depends on the cumulative size of its members. Variables of this type:
CSharp Code:
public struct MyStruct
{
string str;
long lng;
}
will ALWAYS be 96 bits in size because a string variable is 32-bit and a long is 64-bit.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|