0

I want to pass an array to a function with specific index in C#, like we do in C++, something like below:

void foo(int* B) {
// do something;
}

int main() {
int A[10];
foo(A + 3);
}

In this case suppose A starts with base address 1000, then B would be 1000+(sizeof(int)*3). The answer in this link uses Skip() function call but in this case B should be of type IEnumerable<int> and on calling Skip() with ToArray() it creates a copy with different base address, but I want to pass this same array to function foo. How can we do that in C#?

dragon
  • 21
  • 5
  • 1
    Why not simply pass the array and the offset? Yes, that is one extra parameter, but is that such a big deal? Why overcomplicate things? Maybe even the C++ code would be better off doing things that way, to be honest. – PaulMcKenzie Apr 08 '22 at 13:51
  • 1
    @PaulMcKenzie That's quite a big deal. That is the opposite of dependency injection. Now _every_ array argument is two. The C++ code would preferably be a `std::span`. – Passer By Apr 08 '22 at 13:54
  • 2
    I think you may be looking for “span”. It passes part of an array, but doesn’t make a copy. You may have only 1 element in your span, but I think it gives you what you want. – Mike Wodarczyk Apr 08 '22 at 13:59
  • ^^ [All About Span: Exploring a New .NET Mainstay](https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/january/csharp-all-about-span-exploring-a-new-net-mainstay) & [Span struct](https://learn.microsoft.com/en-us/dotnet/api/system.span-1?view=net-6.0) – Fildor Apr 08 '22 at 14:00
  • Note that you can use pointers in c# if you allow unsafe code, but `span` is probably a better option in the vast majority of cases. – JonasH Apr 11 '22 at 06:59

1 Answers1

2

This has been answered here: C# Array slice without copy

You can use ArraySegment or Span to achieve this

public static void Main()
{
    int[] arr1 = new int[] {2, 5, 8, 10, 12};
    int[] arr2 = new int[] {10, 20, 30, 40};

    DoSomethingWithSegmentReference(new ArraySegment<int>(arr1, 2, arr1.Length - 2));
    
    DoSomethingWithSpanReference(arr2.AsSpan().Slice(1, arr2.Length - 1));
}

private static void DoSomethingWithSegmentReference(ArraySegment<int> slice){
    for(int i = 0; i < slice.Count; i++)
        slice[i] = slice[i] * 2;
}

private static void DoSomethingWithSpanReference(Span<int> slice){
    for(int i = 0; i < slice.Length; i++)
        slice[i] = slice[i] * 2;
}
Douglas Ferreira
  • 434
  • 1
  • 7
  • 23
  • If you think this has been answered already, please mark it as duplicate instead. – Fildor Apr 08 '22 at 14:31
  • I think he asked the same thing in a totally different way to think. I think his question is biased to a lower level lang like C and C++, thinking in passing pointers, memory address offsets and stuff. But in higher level langs such as C#, Golang, Python, JS, is quite common have classes or methods for array slicing he just didn't know that – Douglas Ferreira Apr 08 '22 at 15:08
  • Thanks Douglas, I think among ArraySegment and Span, the Span more fits in my requirements... – dragon Apr 12 '22 at 06:54