Results 1 to 12 of 12

Thread: [RESOLVED] why use and not use new in making array

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Resolved [RESOLVED] why use and not use new in making array

    I am reading on arrays and have a question about them. The line below does not use the new keyword. Does that make it any different of an array object than the example below it that uses new to create the object reference?

    int[] nums = { 99, 10, 100, 18, 78, 23,
    63, 9, 87, 49 };


    int[] nums = new int[] { 99, 10, 100, 18, 78, 23,
    63, 9, 87, 49 };

    From what I read, new is for making references to the object as memory is allocated for it. We don't use new when creating variables and assigning them a value but we do need new for referencing of objects to a variable.
    I am confused why array objects can be done either way. Whats so special about them or maybe I should ask what further understanding do I need about this concept.
    Thanks in advance.

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

    Re: why use and not use new in making array

    What you're talking about is a special case that was added for convenience. If you initialise the elements of an array on the same line as you declare the array variable then you can omit the "new" key word. This is standard syntax:
    Code:
    int[] arr = new int[] {1, 2, 3};
    This is legal syntax that is allowed for convenience:
    Code:
    int[] arr = {1, 2, 3};
    This is done because the line already contains a description of the object type. If you're initialising the array on a line other than the one on which it's declared then the line will not include a specification of the type so that syntax is not allowed, i.e. you cannot do this:
    Code:
    int[] arr;
    
    arr = {1, 2, 3};
    but must rather do this:
    Code:
    int[] arr;
    
    arr = new int[] {1, 2, 3};
    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

  3. #3

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: why use and not use new in making array

    Special cases. I'm glad you let me know about that. Thanks!
    Sometimes ya just gotta know the rules without knowing all the details right?

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

    Re: [RESOLVED] why use and not use new in making array

    You find out things like this from the MSDN library. Where do you think I found out that very fact? By reading the documentation on arrays in the MSDN library, back when I was probably no more experienced than you are now.
    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

  5. #5

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: [RESOLVED] why use and not use new in making array

    Could you give me a link to the msdn C# library. I see asp.net but I don't see c#.
    Is this about where?
    http://msdn2.microsoft.com/en-us/library/default.aspx

    EDIT: Think this is it. This is where you go right?
    http://msdn2.microsoft.com/en-us/lib...32(VS.71).aspx
    Last edited by gjon; Apr 17th, 2007 at 12:06 AM.

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

    Re: [RESOLVED] why use and not use new in making array

    You should have installed the MSDN library locally when you installed VS. I strongly recommend using the local version in preference to the online version for the core information. The online version includes more than the local version but 99% of what you'll need will be found locally. The local version has the advantage of an index and filtering.
    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

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] why use and not use new in making array

    In addition, you can usually quickly find MSDN topics using Google:
    http://www.google.com.au/search?q=c%...ient=firefox-a

  8. #8

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: [RESOLVED] why use and not use new in making array

    I have the sdk installed and am using the sdk command prompt. I was hoping that I could use that link rather than install the library. My computer doesn't have the resources for VS and the library locally.

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

    Re: [RESOLVED] why use and not use new in making array

    In that case you should just search MSDN online for terms like arrays "c#" to find C#-specific topics.
    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

  10. #10

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: [RESOLVED] why use and not use new in making array

    I think this is the best online page I have found. You mean the search in the top right of this page right?
    http://msdn2.microsoft.com/en-us/lib...08(VS.80).aspx

    Just asking cause I'd hate to miss out on something even better.
    Thanks

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

    Re: [RESOLVED] why use and not use new in making array

    That's the root of the class library reference and it lists all the namespaces available in the Framework. That's a good place to start to see what sorts of groups of functionality are available, but if you were looking for something specific you wouldn't start there. If you wanted to know about arrays you search for something like array class, which would take you straight to the documentation for the Array class, which all .NET arrays are based on. Alternatively, if you wanted to find C#-specific information on arrays then you'd search for something like arrays "c#". If you know what class you're using then you can see what it can do by going straight to its member listing. For instance, if you're working with strings you might search for string members to see what properties and methods the String class has.
    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

  12. #12

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: [RESOLVED] why use and not use new in making array

    I hear ya now. After trying to find things through it, search is the best solution.
    Thanks for the pointers.

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