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
Printable View
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
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.
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
Ahhh, makes sense. My mistake, was not aware.Quote:
Originally Posted by techgnome
Not so, or at least not necessarily so. If you do this:Quote:
Originally Posted by syntaxeater
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; 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.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) { // ... }
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:Quote:
Originally Posted by JenniferBabe
How could the second line be executed if the first line didn't create a 32-bit variable?CSharp Code:
int x = null; x = int.MaxValue;
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:will ALWAYS be 96 bits in size because a string variable is 32-bit and a long is 64-bit.CSharp Code:
public struct MyStruct { string str; long lng; }