9

Possible Duplicate:
C# memcpy equivalent

What is the equivalent of the memcpy function in c#?

Community
  • 1
  • 1
Siddiqui
  • 7,662
  • 17
  • 81
  • 129
  • 2
    Could you please provide a bit more context. What are you trying to do? – Brian Rasmussen Jun 08 '10 at 10:27
  • 4
    Any details what you try to do. Because the approach will vary based on that. – Incognito Jun 08 '10 at 10:31
  • Your question lacks way to much information to be answered. Since you're talking about memcpy I assume you want to copy using Pointers? But then there are multiple type of pointers in c#. So could you please elaborate? – Phyx Jun 08 '10 at 10:42
  • Actually I have two applications, first in c# and 2nd in c++, both the applications are communicating with each other, for the encoding, I use memcpy function, which I have to decode in c# application. – Siddiqui Jun 08 '10 at 10:43

6 Answers6

5

Buffer.BlockCopy is close, but limited to arrays. I agree it depends what you're trying to do.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
5

As already said it depends what you are trying to do... So you can consider one of these... check the links:

There may be other possible options based on your needs...

Incognito
  • 16,567
  • 9
  • 52
  • 74
3

For copying (byte) arrays, you can use the Array.Copy() method, but that's probably not what you want:

byte[] array1 = new byte[10] { 1,2,3,4,5,6,7,8,9,10 };
byte[] array2 = new byte[10];

Array.Copy(array1,array2,10);
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
  • Or, alternatively, `Buffer.BlockCopy`. http://msdn.microsoft.com/en-us/library/system.buffer.blockcopy.aspx – LukeH Jun 08 '10 at 10:38
2

There is none. C# protects the actual memory behind several layers of abstraction. For some purposes, the IClonable interface may be of some help though.

Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
1

Not an exact equivalent, but does Array::Copy do what you need to do?

In silico
  • 51,091
  • 10
  • 150
  • 143
1

You can use unsafe and pointers. But your particular case would say a bit more about what direction you should head.

Mikael Svenson
  • 39,181
  • 7
  • 73
  • 79