Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中实现Array.ConvertAll方法_C#_Visual Studio - Fatal编程技术网

C# 如何在c中实现Array.ConvertAll方法

C# 如何在c中实现Array.ConvertAll方法,c#,visual-studio,C#,Visual Studio,我无法将中的T类型数组转换为Int数组。我的代码如下 T[]arra=新的T[arr.Length] int [] converted = new int[arr.Length]; T element; for (int i = 0; i < arr.Length; i++) { for (int j = 0; j < arr.Length - 1-i; j++) {

我无法将中的T类型数组转换为Int数组。我的代码如下

T[]arra=新的T[arr.Length]

        int [] converted  = new int[arr.Length];
        T element;
        for (int i = 0; i < arr.Length; i++)
        {
            for (int j = 0; j < arr.Length - 1-i; j++)
            {
                Type s = (Type)System.Int64; 
                Type t = arr.GetType();
                Converter<T,int> a;
                if (t.Equals(s))
                {
                    Array.ConvertAll<T, int>(arr, Converter < T,int> converted);
                }

                if (arr[j] >  arr[j + 1])
                {               

                    element = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = element;
                }
            }
        }
从代码中我一点也不清楚您想做什么,但我想给出一个Array.ConvertAll的经典示例以供比较:

string[] data = { "123", "456", "789" };
int[] ints = Array.ConvertAll(data, int.Parse);
在这里,分配一个长度为3的int[],然后对每个字符串int.Parse用于将字符串转换为int;输出应为int[],值为123456789

使用lambda语法的更复杂示例:

string[] data = { "abc", "def", "ghi" };
string[] reversed = Array.ConvertAll(data, s => {
    char[] chars = s.ToCharArray();
    Array.Reverse(chars); // note; not fully i18n safe
    return new string(chars);
});

这里的lambda主体是我们的转换器,它通过反转字符来反转每个字符串;结果应该是字符串[],值为cba、fed、ihg。

有什么问题?你有例外吗?你有编译错误吗?@Dede我在那个无效的表达式项上有错误int@Pro_Zeck:因为那不是有效的代码。@Leppie你能解释一下吗@赞成者:代码是荒谬的。我不知道你想做什么。你的代码充满了错误。1类型s=TypeSystem.Int64;不会编译2个转换器a;无用变量;除了你抱怨的那个。
string[] data = { "abc", "def", "ghi" };
string[] reversed = Array.ConvertAll(data, s => {
    char[] chars = s.ToCharArray();
    Array.Reverse(chars); // note; not fully i18n safe
    return new string(chars);
});