这是一个锯齿状的数组声明吗?

时间:2017-11-10 07:49:04

标签: c# .net arrays

你能解释一下element数组声明背后的语法吗?这是一个锯齿状的阵列吗?在这种情况下,ValueType是什么?

  
 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 个答案:

答案 0 :(得分:5)

让我们从内到外:elements数组的项目

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

匿名类型实例(包含Value的两个属性int和{{1}类型的Type });有关详细信息,请参阅https://msdn.microsoft.com/en-us/library/bb397696(v=vs.90).aspx

Type数组

elements

是此类匿名类型实例的简单 1d 数组

答案 1 :(得分:2)

  

这是一个锯齿状阵列吗?

没有。它是一个匿名对象的一维数组。

  

在这种情况下,SELECT adw."adwords" as "Type", adw."campaignID_adwords" as "campaignID", adw."impressions_adwords"as "impression", adw."CTR_adwords" as "CTR" FROM "adwords" adw UNION select skl."sklik" as "Type", skl."Campaign_id_sklik" as "campaignID", skl."impressions_sklik" as "impression", (case when skl."impressions_sklik" = 0 then 0 when skl."clicks_sklik" = 0 then 0 when skl."clicks_sklik" = '' then 0 when skl."impressions_sklik" = '' then 0 ELSE (iff(skl."clicks_sklik" = 0 or skl."clicks_sklik" IS NULL or skl."clicks_sklik" = '', 0, skl."clicks_sklik") / iff(skl."impressions_sklik" = 0 or skl."impressions_sklik" IS NULL or skl."impressions_sklik" = '', 0, skl."impressions_sklik")) END) as "CTR" FROM "sklik" skl UNION select fad."facebook_ads" as "Type", fad."cam_campaign_id_facebook_ads" as "campaignID", fad."cam_impressions_facebook_ads" as "impression", fad."cam_ctr_facebook_ads" as "CTR" FROM "facebook_ads" fad Value是什么?

匿名类的属性。