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#?

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. Commented Apr 8, 2022 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. Commented Apr 8, 2022 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. Commented Apr 8, 2022 at 13:59
  • ^^ All About Span: Exploring a New .NET Mainstay & Span<T> struct Commented Apr 8, 2022 at 14:00
  • Note that you can use pointers in c# if you allow unsafe code, but span<T> is probably a better option in the vast majority of cases. Commented Apr 11, 2022 at 6:59

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

If you think this has been answered already, please mark it as duplicate instead.
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
Thanks Douglas, I think among ArraySegment and Span, the Span more fits in my requirements...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.