Jump to content
  • Advertisement
Sign in to follow this  
boolean010

[.net] c# Arrays

This topic is 3697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the difference between this: int[] arr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; and that: int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
C# provides quite a bit of shortcuts for getting tasks done, removing redundant and otherwise verbose containers. Events and delegates are another good example, where you can specify the method directly instead of all the other mumbo jumbo.

Share this post


Link to post
Share on other sites
The only difference is that you can only use this syntax:

int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

On declarations.

For example, given the following method:

void DoSomething( int[] array ) { ... }

You could invoke it like:
DoSomething( new int[] {1} );

But you couldn't use:
DoSomething( {1} );

Share this post


Link to post
Share on other sites
Just for the fun of it - in C# 3.0 you can also do this:
DoSomething(new [] {1,2,3,4,5})
(The array type is inferred to be int[] here)

Regards,
Andre

Share this post


Link to post
Share on other sites
Quote:
Original post by Daerax
Does C# have array literals yet?


Assuming you mean the declaration of an array using the syntax
{ 1, 2, 3}
versus specifying the array type using
int[] { 1, 2, 3 }
or
new [] { 1, 2, 3 }
then no, C# doesn't support that.

With the advent of the var keyword the infrastructure would seem to be in place to support them.

Share this post


Link to post
Share on other sites
Something like

void Function(int [] intarray)
{

}

Call Function({0,1,2,3}).
EDIT: Yep that. So are you saying I can go Function( var {0,1,2})?

Its something of a small annoyance Im always bumping my head agaisnt whenever I use C#.

Share this post


Link to post
Share on other sites
Quote:
Original post by Daerax
Something like

void Function(int [] intarray)
{

}

Call Function({0,1,2,3}).
EDIT: Yep that. So are you saying I can go Function( var {0,1,2})?

Its something of a small annoyance Im always bumping my head agaisnt whenever I use C#.


var cannot be used for that. It can only be used for declaring local variables (in C# 3.0).

You have to call your function with Function( new int[] {0,1,2} ); or you can make use of params:

void Function(params int[] array) { ... }

Function(1, 2, 3);

but this comes with a few limitations (must be placed last in argument list, only one such argument per method).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!