|
-
Apr 16th, 2007, 10:20 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Apr 16th, 2007, 11:15 PM
#2
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};
-
Apr 16th, 2007, 11:33 PM
#3
Thread Starter
Hyperactive Member
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?
-
Apr 16th, 2007, 11:36 PM
#4
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.
-
Apr 17th, 2007, 12:00 AM
#5
Thread Starter
Hyperactive Member
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.
-
Apr 17th, 2007, 12:12 AM
#6
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.
-
Apr 17th, 2007, 12:15 AM
#7
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
-
Apr 17th, 2007, 12:23 AM
#8
Thread Starter
Hyperactive Member
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.
-
Apr 17th, 2007, 12:41 AM
#9
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.
-
Apr 17th, 2007, 06:15 PM
#10
Thread Starter
Hyperactive Member
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
-
Apr 17th, 2007, 07:27 PM
#11
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.
-
Apr 17th, 2007, 09:13 PM
#12
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|