C # Generics - an array?

How to repeat the declaration of this function of a C ++ template in C #?

    template <class type>
void ReadArray(type * array, unsigned short count)
{
    int s = sizeof(type) * count;
    if(index + s > size)
        throw(std::exception("Error 102"));
    memcpy(array, stream + index, s);
    index += s;
}   

When called, it adds bytes / word / (type) to the given array, reading the stream (stream) at a specific position (index).

I tried to repeat an ad like this, but I get an error

    public static T void ReadArray(<T> Array inputarray) // error
    {
        ...
    }

Thanks!

Another conservative question: how to add bytes to this array (memcpy ()), should I use a pointer?

+3
source share
2 answers

You use it as follows:

public static void ReadArray<T>(T[] inputArray) {
   ...
}

You can use the method Array.Copyto copy data between arrays.

Edit:
" " , . , , , . , BitConverter . " ", , , .

+14
public static void ReadArray<T>(T[] inputarray)
    {
        ...
    }

,

List<T> list = new List<T>();
list.AddRange(inputarray);
list.AddRange(anotherArray);
+2

Source: https://habr.com/ru/post/1706707/


All Articles