Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a jagged array declaration?

Tags:

arrays

c#

.net

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}");
     }
 }
like image 421
misfit Avatar asked May 11 '26 19:05

misfit


1 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

like image 162
Dmitry Bychenko Avatar answered May 13 '26 08:05

Dmitry Bychenko