How to iterate through the built-in types in C#?

前端 未结 3 1554
忘掉有多难
忘掉有多难 2021-02-06 01:41

I want to iterate through the built-in types (bool, char, sbyte, byte, short, ushort, etc) in c#.

How to do that?

foreach(var x in GetBuiltInTypes())
{
/         


        
3条回答
  •  轮回少年
    2021-02-06 02:19

    There's no built-in way to do that; you can try:

    foreach (var type in new Type[] { typeof(byte), typeof(sbyte), ... })
    {
        //...
    }
    

    Of course, if you're going to be doing this a lot, factor out the array and put it inside a static readonly variable.

提交回复
热议问题