简体   繁体   中英

What is the equivalent for C++ const size_t in C#?

I'm trying to translate some Ogre code to it's C# version and I ran into a problem :

    const size_t nVertices = 8;
    const size_t vbufCount = 3*2*nVertices;

    float vertices[vbufCount] = {
            -100.0,100.0,-100.0,        //0 position
            -sqrt13,sqrt13,-sqrt13,     //0 normal
            //... 
           -sqrt13,-sqrt13,sqrt13,     //7 normal
    };

Basically, const size_t doesn't exist in C#, and const int can't be used to declare array's size.

I was wondering how to declare arrays with a constant value?

size_t is a typedef (kind of like a #define macro) which is basically an alias for another type. Its definition depends on the SDK, but it's usually unsigned int .

Anyway, in this case it doesn't really matter because they're constants, so you know that nVertices is 8 and vbufCount is 48. You can just write it like this in C#:

const int nVertices = 8;
const int vbufCount = 3 * 2 * nVertices;

float[] vertices = new float[vbufCount] {
    -100.0,100.0,-100.0,        //0 position
    -sqrt13,sqrt13,-sqrt13,     //0 normal
    //... 
    -sqrt13,-sqrt13,sqrt13,     //7 normal
    };

Basically, const size_t doesn't exist in C#, and const int can't be used to declare array's size.

That's not because of const int , but because array size is not part of the array type in C#. You can change your code into this:

float[] vertices = {
        -100.0f,100.0f,-100.0f,     //0 position
        -sqrt13,sqrt13,-sqrt13,     //0 normal
        //... 
       -sqrt13,-sqrt13,sqrt13,      //7 normal
};

There are also several other ways to do the same thing, including:

const int nVertices = 8;
const int vbufCount = 3*2*nVertices;

float[] vertices = new float[vbufCount] {
        -100.0f,100.0f,-100.0f,     //0 position
        -sqrt13,sqrt13,-sqrt13,     //0 normal
        //... 
       -sqrt13,-sqrt13,sqrt13,      //7 normal
};

The only difference is that if the number of items in the initializer doesn't match the number you specified, you will get a compile-time error.

float[] array = new float[] { 1.2F, 2.3F, 3.4F, 4.5F };

这个如何在C#中声明arrays

In C++, size_t is an unsigned integer type of at least 16-bits that follows the native integer type of the CPU. In other words, sizeof(size_t) is not fixed, even though most people use it as 'unsigned int'. In C# there is no such thing.

Sizes in C# (f.ex. when using arrays and list's) are normally type 'int', which is a 32-bit integer.

In your case I would probably make the array readonly and use 'vertices.Length', eg:

    private readonly float[] vertices = new float[]
    {
        1f,2f,3f,4f,5.2f // etc
    };

or in this case I'd probably define it as a 2D array and use vertices.GetLength:

    private readonly float[,] vertices = new float[5,5];

    // fill in code:
    vertices[0, 0] = 0; 
    // etc

All these answers don't actually answer the question of what type is equivalent to size_t. The correct type equivalent for size_t in .NET is UIntPtr. It's 32-bit on 32-bit platforms, 64-bit on 64-bit platforms, and unsigned. It's the only type that's truly equivalent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM