Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Using new[]

I have a question about using new[].

Imagine this:

Object.SomeProperty = new[] {"string1", "string2"};

Where SomeProperty expects an array of strings.

I know this code snippet will work. But i want to know what it does under the hood. Does new[] makes an instance of the class object and in SomeProperty it converts it automatically to a string object?

Thanks

like image 943
Martijn Avatar asked Nov 28 '08 08:11

Martijn


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

Okay, there's still a little bit of confusion here.

The inference that's going on has nothing to with the type of Object.SomeProperty, but everything to do with the types of the expressions in the array initializer. In other words, you could do:

object o = new[] { "string1", "string2" };

and o would still be a reference to a string array.

Basically, the compiler looks at an expression like this:

new[] { A, B, C, D, ... }

(where A, B, C, D etc are expressions) and tries to work out the correct array type to use. It only considers the types of A, B, C and D (etc) as the array element type. Taking this set of candidate types, it tries to find one which all the others can be implicitly converted to. If there's not exactly one such type then the compiler will complain.

So for example:

new[] { new Form(), new MemoryStream() }

will not compile - neither MemoryStream nor Form is convertible to the other. However:

new[] { GetSomeIDisposable(), new MemoryStream() }

will be treated as an IDisposable[] because there's an implicit conversion from MemoryStream to IDisposable. Likewise:

new[] { 0, 1, 3.5 } // double[]
new[] { 1, 3, 100L } // long[]
like image 53
Jon Skeet Avatar answered Oct 22 '22 04:10

Jon Skeet


This is just syntactical sugar. The compiler will infer the type actually necessary here and create code that is equivalent to the explicit construct:

Object.SomeProperty = new string[] {"string1", "string2"};

There's no such thing as new[] that gets executed at runtime.

like image 28
Konrad Rudolph Avatar answered Oct 22 '22 03:10

Konrad Rudolph


It is roughly translated to:

string[] $temp = new string[2];
$temp[0] = "string1";
$temp[1] = "string2";
Object.SomeProperty = $temp;

Interestingly, var x = new[] { "string1", "string2" }; works as well, it can infer x to be a string[], but var x = { "string1", "string2" }; fails.

like image 4
Simon Buchan Avatar answered Oct 22 '22 02:10

Simon Buchan