In VB.NET, you can instantiate and immediately use an array like this:
Dim b as Boolean = {"string1", "string2"}.Contains("string1")
In c#, however, it appears you have to do this:
bool b = new string[] { "string1", "string2" }.Contains("string1");
Does c# have equivalent shorthand syntax that uses type inference to determine the type of array without it having to be explicitly declared?
Implicitly typed arrays do not have to include their type, provided it can be inferred:
bool b = new [] { "string1", "string2" }.Contains("string1");
It called Implicitly Typed Arrays
You can create an implicitly-typed array in which the type of the array instance is inferred from the elements specified in the array initializer. The rules for any implicitly-typed variable also apply to implicitly-typed arrays.
static void Main()
{
var a = new[] { 1, 10, 100, 1000 }; // int[]
var b = new[] { "hello", null, "world" }; // string[]
}
You can use it also for jagged array.
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