I only see four different possibilities (there may be more, though):
1- You can use anonymous type syntax:
var siblings = new[]{
new { name= "Mike", age= 28} ,
new { name= "Mary", age= 25},
new { name= "John", age= 31}
};
where some typing is required (even if auto completion can kick in).
2- You use explicit type syntax, which means you can call a constructor and
specify public properties (or fields) values. I use the predefined Rectangle
type, for illustration:
Rectangle x = new Rectangle(1, 2, 3, 4) { Width = 10 };
( I don't claim that makes sense, but just use it to show it is possible).
Sounds unrelated, at first, but that is because that second case has some
short cut, mainly, if you use the default constructor:
Rectangle x = new Rectangle() { X = 1, Y = 2, Height = 3, Width = 4 };
You can then drop the ( ) :
Rectangle x = new Rectangle { X = 1, Y = 2, Height = 3, Width = 4 };
which can also be:
var x = new Rectangle { X = 1, Y = 2, Height = 3, Width = 4 };
and if we drop Rectangle, we got the anonymous type, as in the first case,
which explain a little bit about what happen in the first case, as it is
associated to call first a constructor (the default one, by ... default )
and then to assign public fields, or properties.
Note also that
var siblings = { { "Mike", 28 }, { "Mary", 25 }, { "John", 31 } };
does NOT work, but can have something else which is close:
3- Use tuples
var others = new[] {
Tuple.Create( "Mike", 28 ),
Tuple.Create( "Mary", 25 ),
Tuple.Create( "John", 31 ) };
if( others[1].Item1== "Mike") { }
which can be seen like an anonymous type where the 'names' are supplied for
you as being item1, item2,.... You can further on use LINQ to convert that
array ( using OfType, or ToList, or anything you wish for).
4- Use default array construction.
While
var siblings = { { "Mike", 28 }, { "Mary", 25 }, { "John", 31 } };
does not work,
object[,] siblings = { { "Mike", 28 }, { "Mary", 25 }, { "John", 31 } };
does, but that seems to be related to a short cut syntax for an array
constructor on value types, and unfortunately, a jagged array,
object[][] sibligns;
does not offer short cuts that the array does, and you need the solution you
found, or use one of the other short cuts presented.
Vanderghast, Access MVP
Abby Brown said:
Jason Newell said:
object[,] siblings = { { "Mike", 28 }, { "Mary", 25 }, { "John", 31 } };
Jason Newell
www.jasonnewell.net
How do I create an array like the following which contains string and
integer?
string[,] siblings = { {"Mike", 28}, {"Mary", 25}, {"John", 31} };
Is it possible to do:
object[][] siblings = { { "Mike", 28 }, { "Mary", 25 }, { "John",
31 } };
without verbosity of doing something like:
object[][] siblings = { new object[]{ "Mike", 28 }, new object[]{
"Mary", 25 }, new object[]{ "John", 31 } };
?