views:

70

answers:

2

i can not Convert the T TYPE array in to Int array My Code is below .

T [] arra = new 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;
                }
            }
        }
+1  A: 
    Array a = new Array();
    a.Cast<long>().ToArray();
SaeedAlg
if this is an answer mark it as answer to improve your accept rate :D
SaeedAlg
+1  A: 

I am not at all clear (from the code) what you are trying to do, but to give a classic example of Array.ConvertAll (for comparison):

string[] data = { "123", "456", "789" };
int[] ints = Array.ConvertAll(data, int.Parse);

Here, an int[] of length 3 is allocated, then (for each string) int.Parse is used to transform from a string to an int; the output should be the int[] with values 123,456,789.

A more complex example (using lambda syntax):

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);
});

Here the lambda body (which is our converter) reverses each string (by reversing the characters); the result should be the string[] with values "cba","fed","ihg".

Marc Gravell