3

I study how FileStream is implemented in C# and in the Read(..) method I can see:

n = ReadCore(_buffer, 0, _bufferSize);
...
Buffer.InternalBlockCopy(_buffer, _readPos, array, offset, n);
...

Where Buffer.InternalBlockCopy points to definition (bellow) in buffer.cs. The method BlockCopy is defined as static extern. Where is this method defined? It is part of .NET? It is managed or native?

[System.Runtime.InteropServices.ComVisible(true)]
public static class Buffer
{
    // Copies from one primitive array to another primitive array without
    // respecting types.  This calls memmove internally.  The count and 
    // offset parameters here are in bytes.  If you want to use traditional
    // array element indices and counts, use Array.Copy.
    [System.Security.SecuritySafeCritical]  // auto-generated
    [ResourceExposure(ResourceScope.None)]
    [MethodImplAttribute(MethodImplOptions.InternalCall)]
    public static extern void BlockCopy(Array src, int srcOffset,
        Array dst, int dstOffset, int count);
..
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • 1
    Possibly related: [What's the point of MethodImplOptions.InternalCall?](https://stackoverflow.com/questions/11161120/whats-the-point-of-methodimploptions-internalcall#11161727) – Nisarg Shah Aug 10 '18 at 06:54
  • 1
    If you look around the coreclr repo you can usually find these methods (of course this is the core clr implementation not the full .net framework). This one is [here](https://github.com/dotnet/coreclr/blob/2583ce936776a0eac31df904e41d5119840c203b/src/vm/comutilnative.cpp). Look for Buffer::BlockCopy. – Mike Zboray Aug 10 '18 at 07:01
  • @mikez Thanks, that is what I was looking for. Now I see that in Core it is few checks and memmove. – Tomas Kubes Aug 10 '18 at 07:24

1 Answers1

2

Well, the extern keyword means that the method is defined outside C# code, and the

[MethodImplAttribute(MethodImplOptions.InternalCall)]

means that it calls a method defined inside Common Language Runtime.

After MethodImplOptions docs:

The call is internal, that is, it calls a method that is implemented within the common language runtime.

V0ldek
  • 9,623
  • 1
  • 26
  • 57