Results 1 to 7 of 7

Thread: [3.0/LINQ] Please clarify

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    116

    [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

  2. #2
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    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.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: [3.0/LINQ] Please clarify

    Quote 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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [3.0/LINQ] Please clarify

    Quote Originally Posted by syntaxeater
    The "= null" however, is redundant.
    Not so, or at least not necessarily so. If you do this:
    CSharp Code:
    1. private void Form1_Load(object sender, EventArgs e)
    2. {
    3.     List<object> list;
    4.  
    5.     this.DoSomething(ref list);
    6. }
    7.  
    8. private void DoSomething(ref List<object> list)
    9. {
    10.     // ...
    11. }
    you'll get a compilation error "Use of unassigned local variable" but if you do this:
    CSharp Code:
    1. private void Form1_Load(object sender, EventArgs e)
    2. {
    3.     List<object> list = null;
    4.  
    5.     this.DoSomething(ref list);
    6. }
    7.  
    8. private void DoSomething(ref List<object> list)
    9. {
    10.     // ...
    11. }
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [3.0/LINQ] Please clarify

    Quote 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:
    1. int x = null;
    2. 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:
    1. public struct MyStruct
    2. {
    3.     string str;
    4.     long lng;
    5. }
    will ALWAYS be 96 bits in size because a string variable is 32-bit and a long is 64-bit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width