Is it a jagged array declaration?

Viewed 83

Could you please explain me the syntax behind element array declaration ? Is this a jagged array ? What are Value and Type in this case ?

 enum Color { Red = 1, Green, Blue }
 enum Theme { Dark = 1, Light, NotSure }

 public static void Main(string[] args)
 {
     var elements = new[]
     {
         new { Value = 1, Type = typeof(Color) },
         new { Value = 2, Type = typeof(Theme) },
         new { Value = 3, Type = typeof(Color) },
         new { Value = 1, Type = typeof(Theme) },
         new { Value = 2, Type = typeof(Color) },
     };

     foreach (var element in elements)
     {
         var enumValue = Enum.ToObject(element.Type, element.Value);
         Console.WriteLine($"{element.Type.Name}({element.Value}) = {enumValue}");
     }
 }
2 Answers

Let's go from inner to outer: elements array's items

   new { Value = 1, Type = typeof(Color) }

are anonymous type instances (with two properties: Value of type int and Type of type Type); see https://msdn.microsoft.com/en-us/library/bb397696(v=vs.90).aspx for details

elements array

   var elements = new[] {
     new { Value = 1, Type = typeof(Color) },
     new { Value = 2, Type = typeof(Theme) },
     ... 
   };

is a simple 1d array of such anonymous type instances

Is this a jagged array?

No. It is a one-dimensional array of anonymous objects.

What are Value and Type in this case?

Properties of the anonymous class.

Related