[英]how to take checkpoint record and process remaining data from foreach c#
[英]c# how to take data_type as input in “foreach” clause
我希望代碼可以自動檢測數據類型並執行foreach循環。 例如,數組中的打印元素一個接一個。 但是我們事先不知道這個數組的類型。 我得到了一個使用any_array.GetType()。ToString()來獲取數據類型字符串的解決方案。 但這太愚蠢了。
還有其他更優雅的方法嗎?
using System;
public class basic_data_structure
{
public static void example01()
{
int[] int_array = new int[] { 1, 2, 3, 4, 5 }; // type = System.Int32[]
float[] float_array = new float[] { 1F, 2F, 3F, 4F, 5F }; // type = System.Single[]
string[] string_array = new string[] { "a", "b", "c", "d", "e" }; // type = System.String[]
print_any_array01(int_array);
print_any_array01(float_array);
print_any_array01(string_array);
}
public static void print_any_array01(Array any_array)
{
int count = 0;
string array_type = any_array.GetType().ToString();
if (array_type == "System.Int32[]")
{
foreach (int element in any_array)
{
count += 1;
Console.WriteLine("Element #{0}: {1}", count, element);
}
}
else if (array_type == "System.Single[]")
{
foreach (float element in any_array)
{
count += 1;
Console.WriteLine("Element #{0}: {1}", count, element);
}
}
else if (array_type == "System.String[]")
{
foreach (string element in any_array)
{
count += 1;
Console.WriteLine("Element #{0}: {1}", count, element);
}
}
else
{
Console.WriteLine("ERROR unknown data type array!!!");
}
}
}
您可以使用泛型:
public static void print_any_array01<T>(T[] any_array)
{
int count = 0;
foreach (T element in any_array)
{
count += 1;
Console.WriteLine("Element #{0}: {1}", count, element);
}
}
使用var關鍵字
public static void print_any_array01(Array any_array)
{
foreach (var element in any_array)
{
count += 1;
Console.WriteLine("Element #{0}: {1}", count, element);
}
}
如果您真的想知道傳遞的數組是什么類型,可以添加
if(any_array is int[])
Console.WriteLine("Integer array");
else if(any_array is float[])
Console.WriteLine("float array");
else if(any_array is string[])
Console.WriteLine("string array");
但是沒有太大的收獲。
在這種情況下,您可以只使用Object
。 我也很想問IEnumerable而不是數組。 除了數組之外,這還使您可以接受函數的List和其他序列:
public static void PrintSequence(IEnumerable any_array)
{
int count = 0;
foreach (Object element in any_array)
{
count += 1;
Console.WriteLine("Element #{0}: {1}", count, element);
}
}
在這種情況下, WriteLine()
函數最終將對傳入的任何內容調用.ToString()
。如果不存在現有的重載,則最終可能會看到類型的名稱,而不是值。 您可以使用泛型和代理對此進行改進,如下所示:
public static void PrintSequence<T>(IEnumerable<T> any_array)
{
PrintSequence(any_array, null);
}
public static void PrintSequence<T>(IEnumerable<T> any_array, Func<T, string> toString)
{
if (toString == null) toString = (x) => x.ToString();
int count = 0;
foreach (T element in any_array)
{
count += 1;
Console.WriteLine("Element #{0}: {1}", count, toString(element));
}
}
您可以使用與調用現有方法完全相同的方式進行調用,也可以使用有關如何根據所需對象類型生成字符串的說明進行調用。
請注意,這些都不顯示原始數組的類型。 但是,您的示例代碼也不是很在乎數組的類型。
如果您不想使用泛型,則需要將其轉換為正確的類型。 這遵循代碼的結構,但是只是強制轉換數組而不是直接訪問類型。
using System;
public class basic_data_structure
{
public static void Main()
{
int[] int_array = new int[] { 1, 2, 3, 4, 5 }; // type = System.Int32[]
float[] float_array = new float[] { 1F, 2F, 3F, 4F, 5F }; // type = System.Single[]
string[] string_array = new string[] { "a", "b", "c", "d", "e" }; // type = System.String[]
print_any_array01(int_array);
print_any_array01(float_array);
print_any_array01(string_array);
Console.ReadKey();
}
public static void print_any_array01(Array any_array)
{
int count = 0;
int[] int_array = any_array as int[];
float[] float_array = any_array as float[];
string[] string_array = any_array as string[];
if (int_array != null)
{
foreach (int element in int_array)
{
count += 1;
Console.WriteLine("Element #{0}: {1}", count, element);
}
}
else if (float_array != null)
{
foreach (float element in any_array)
{
count += 1;
Console.WriteLine("Element #{0}: {1}", count, element);
}
}
else if (string_array != null)
{
foreach (string element in any_array)
{
count += 1;
Console.WriteLine("Element #{0}: {1}", count, element);
}
}
else
{
Console.WriteLine("ERROR unknown data type array!!!");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.