• Saves Valuable Time
  • Trusted Accuracy since 2004
  • 15-Day Money-Back Guarantee

C++ Equivalent to C# Arrays

Since it is becoming increasingly common to use std::vector instead of arrays in C++, we give examples of both conversions below. (C# to C++ Converter converts to std::vector by default).

Unsized Array

C# C++
int[] myArray = null; //as a vector:
std::vector<int> myArray;

//as an array:
int *myArray = nullptr;

Sized Array

C# C++
int[] myArray = new int[2]; //as a vector:
std::vector<int> myArray(2);

//as an array:
int *myArray = new int[2];

Access Array Element

C# C++
x = myArray[0]; x = myArray[0];   //no change

Jagged Array

C# C++
int[][] myArray = new int[2][]; //as a vector:
std::vector<std::vector<int>> myArray(2);

//as an array:
int **myArray = new int*[2];

Rectangular Array

C# C++
int[,] myArray = new int[2, 3]; //as a vector:
std::vector<std::vector<int>> myArray = RectangularVectors::RectangularIntVector(2, 3);

// Copyright © 2004 - 2020 Tangible Software Solutions, Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
// This class includes methods to convert multidimensional arrays to C++ vectors.
class RectangularVectors
    static std::vector<std::vector<int>> RectangularIntVector(int size1, int size2)
        std::vector<std::vector<int>> newVector(size1);
        for (int vector1 = 0; vector1 < size1; vector1++)
            newVector[vector1] = std::vector<int>(size2);

        return newVector;

//as an array:
int **myArray = new int*[2][3];

Use C# to C++ Converter to convert from C# to C++.     

Additional resource: C++ and C# Equivalents

Copyright © 2004 – 2021 Tangible Software Solutions, Inc.