C# pass int[] (array) with and without ref


Bulgari boy

I'm a little confused about this because I've read about an int[] array, even though int is a primitive type, since it's an array, it's a reference type variable.

What is the difference between such methods:

public static void ChangeSomething(ref int[] array)
{
     array[0] = 100;
}

and

public static void ChangeSomething(int[] array)
{
     array[0] = 100;
}

After modifying the array, I can see the new value 100 at index 0 for both calls.

What's going on under the covers that make a difference? Does the VS IDE allow both just because the "ref" keyword clarifies the intent?

light armored vehicle

The difference is that you can assign primitive variables directly in the method. If you change the method to this:

public static void ChangeSomething(ref int[] array)
{
     array = new int[2];
}

and call it like this:

var myArray = new int[10];
ChangeSomething(ref myArray);

Console.WriteLine(array.Length);

You will see that myArraythe length is only 2 after the call. Without refthe keyword, you can only change the contents of the array, because the reference to the array is copied into the method.

Related


C# pass int[] (array) with and without ref

Bulgari boy I'm a little confused about this because I've read about an int[] array, even though int is a primitive type, since it's an array, it's a reference type variable. What is the difference between such methods: public static void ChangeSomething(ref i

Python pass ref int as parameter

Mr. Sha The tlb library in my python project passes win32com.client. I've easily used a number of built-in functions, but one of the main functions takes a list of arguments for two of them marked as ref int. When I try to pass a python integer to the function

C: Pass 2D int array

its charlieb I'm having trouble passing a 2d array of integers to an array of functions in C. My task is to scan integer rows from stdin into a 2d array and then pass that array to another function for processing. Here is my code. void displayAll(int p[][], ch

C: Pass 2D int array

its charlieb I'm having trouble passing a 2d array of integers to an array of functions in C. My task is to scan integer rows from stdin into a 2d array and then pass that array to another function for processing. Here is my code. void displayAll(int p[][], ch

Shuffling an int array in C with - without a while loop

Eyal Gruper I want to shuffle an array of integers, sorted, of size n, with values 1-n. I just want to avoid the while loop to make sure rand() doesn't give me the same index. The code looks like this: void shuffleArr(int* arr, size_t n) { int newIndx = 0;

Shuffling an int array in C with - without a while loop

Eyal Gruper I want to shuffle an array of integers, sorted, of size n, with values 1-n. I just want to avoid the while loop to make sure rand() doesn't give me the same index. The code looks like this: void shuffleArr(int* arr, size_t n) { int newIndx = 0;

Shuffling an int array in C with - without a while loop

Eyal Gruper I want to shuffle an array of integers, sorted, of size n, with values 1-n. I just want to avoid the while loop to make sure rand() doesn't give me the same index. The code looks like this: void shuffleArr(int* arr, size_t n) { int newIndx = 0;

Shuffling an int array in C with - without a while loop

Eyal Gruper I want to shuffle an array of integers, sorted, of size n, with values 1-n. I just want to avoid the while loop to make sure rand() doesn't give me the same index. The code looks like this: void shuffleArr(int* arr, size_t n) { int newIndx = 0;

C++ Int to Array without library

username good morning, how can i convert a to int a = 325;an array int b[2]without lib ? I mean this b[0] = 3;, b[1] = 2;andb[2] = 5; goodbye misty Do something like: int x = a; int i = 0; while (x > 0) { b[i++] = x % 10; x /= 10; }

Shuffling an int array in C with - without a while loop

Eyal Gruper I want to shuffle an array of integers, sorted, of size n, with values 1-n. I just want to avoid the while loop to make sure rand() doesn't give me the same index. The code looks like this: void shuffleArr(int* arr, size_t n) { int newIndx = 0;

Shuffling an int array in C with - without a while loop

Eyal Gruper I want to shuffle an array of integers, sorted, of size n, with values 1-n. I just want to avoid the while loop to make sure rand() doesn't give me the same index. The code looks like this: void shuffleArr(int* arr, size_t n) { int newIndx = 0;

Shuffling an int array in C with - without a while loop

Eyal Gruper I want to shuffle an array of integers, sorted, of size n, with values 1-n. I just want to avoid the while loop to make sure rand() doesn't give me the same index. The code looks like this: void shuffleArr(int* arr, size_t n) { int newIndx = 0;

C++ Int to Array without library

username good morning, how can i convert a to int a = 325;an array int b[2]without lib ? I mean this b[0] = 3;, b[1] = 2;andb[2] = 5; goodbye misty Do something like: int x = a; int i = 0; while (x > 0) { b[i++] = x % 10; x /= 10; }

Pass numpy array element (int) to c++ int using SWIG

otherness I want to pass integer elements from a numpy array in python to a c function which captures it as a c++ integer using SWIG. What am I missing here? add_vector.i %module add_vector %{ #define SWIG_FILE_WITH_INIT #define NPY_NO_DEPRECATED_API N

Pass numpy array element (int) to c++ int using SWIG

otherness I want to pass integer elements from a numpy array in python to a c function which captures it as a c++ integer using SWIG. What am I missing here? add_vector.i %module add_vector %{ #define SWIG_FILE_WITH_INIT #define NPY_NO_DEPRECATED_API N

Pass numpy array element (int) to c++ int using SWIG

otherness I want to pass integer elements from a numpy array in python to a c function which captures it as a c++ integer using SWIG. What am I missing here? add_vector.i %module add_vector %{ #define SWIG_FILE_WITH_INIT #define NPY_NO_DEPRECATED_API N

Pass numpy array element (int) to c++ int using SWIG

otherness I want to pass integer elements from a numpy array in python to a c function which captures it as a c++ integer using SWIG. What am I missing here? add_vector.i %module add_vector %{ #define SWIG_FILE_WITH_INIT #define NPY_NO_DEPRECATED_API N

Pass numpy array element (int) to c++ int using SWIG

otherness I want to pass integer elements from a numpy array in python to a c function which captures it as a c++ integer using SWIG. What am I missing here? add_vector.i %module add_vector %{ #define SWIG_FILE_WITH_INIT #define NPY_NO_DEPRECATED_API N

C#: Pass int array to C++ dll

Prasad I have a C++ dll for card printing (ID card). My implementation is done using C#.Net. I use the following code to call the c++ dll. [DllImport(@"J230i.dll",CallingConvention = CallingConvention.Cdecl,SetLastError=true)] public static extern int N_PrintJ

C#: Pass int array to C++ dll

Prasad I have a C++ dll for card printing (ID card). My implementation is done using C#.Net. I use the following code to call the c++ dll. [DllImport(@"J230i.dll",CallingConvention = CallingConvention.Cdecl,SetLastError=true)] public static extern int N_PrintJ

Pass text file into int or array using C#

crazy I'm new to C# programming for the past week, so I'm new to it, but I'm having trouble connecting the two parts of the program together. Where I'm at is reading information from a text file and then needing to pass that information to an int so that the m

Pass mutable Int64 array from Swift to C

Anton Malyshev I have a simple function in C that populates an array: void process(long long * array, int arraySizeInBytes) Try calling it from Swift, passing an array of 5 elements (40 bytes): var a: [Int64]() a.append(-1) a.append(-1) a.append(-1) a.append(

Pass text file into int or array using C#

crazy I'm new to C# programming for the past week, so I'm new to it, but I'm having trouble connecting the two parts of the program together. Where I'm at is reading information from a text file and then needing to pass that information to an int so that the m

Can't pass C# int array to VBA Excel macro

Manuel Hoffmann I'm trying to call a macro for an Excel sheet (2003) from my C# application using C# Interop. Before using the Integer parameter it worked fine. But now I need to pass an array of integers and keep getting a type mismatch exception. COMExceptio

Pass mutable Int64 array from Swift to C

Anton Malyshev I have a simple function in C that populates an array: void process(long long * array, int arraySizeInBytes) Try calling it from Swift, passing an array of 5 elements (40 bytes): var a: [Int64]() a.append(-1) a.append(-1) a.append(-1) a.append(

Pass mutable Int64 array from Swift to C

Anton Malyshev I have a simple function in C that populates an array: void process(long long * array, int arraySizeInBytes) Try calling it from Swift, passing an array of 5 elements (40 bytes): var a: [Int64]() a.append(-1) a.append(-1) a.append(-1) a.append(

Can't pass C# int array to VBA Excel macro

Manuel Hoffmann I'm trying to call a macro for an Excel sheet (2003) from my C# application using C# Interop. Before using the Integer parameter it worked fine. But now I need to pass an array of integers and keep getting a type mismatch exception. COMExceptio