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
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 ...
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? 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.
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.
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[]
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With