Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone,
What a difference between
int []x=new int[]{1,2,3};
and
int[]x={1,2,3};

Thnx for any help
Posted
Updated 14-Nov-10 13:31pm
v2

In .Net different code syntax produces identical code.

Consider the two bits of code below:
C#
private string myString;

public string MyString
{
  get
  {
    return myString;
  }

  set
  {
    myString = value;
  }
}

and
C#
public string MyString {get; set;}


They both do exactly the same thing

The second block is basically just a shorthand way to create the first.

For your second Array example the C# compiler 'knows' that you want to create a new int[] and does it for you behind the scenes.

It is just a more recent way to do it to make the coders job easier.
 
Share this answer
 
As far as I know, there is no difference.

They are just two different ways to declare and instantiate Arrays. I suspect that behind the scenes the IL code produced would be the same.
 
Share this answer
 
Comments
Mostafa Elsadany 14-Nov-10 19:48pm    
thnx for help
and first of all sorry for my English
but i think there is difference
because use new
there is target from use (new)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900