什么是C ++ const size_t在C#中的等价? [英] What is the equivalent for C++ const size_t in C#?

查看:848
本文介绍了什么是C ++ const size_t在C#中的等价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图翻译一些Ogre代码到它的C#版本,我遇到了一个问题:

  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
};

基本上,const size_t不存在于C#中,const int不能用于声明数组的大小。



我想知道如何声明一个常量值数组。

解决方案

size_t是一个typedef(类似于#define宏),基本上是另一个类型的别名。它的定义取决于SDK,但通常是 unsigned int

无论如何,在这种情况下,它并不重要,因为它们常量,所以你知道nVertices是8和vbufCount是48.你可以这样写在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
};


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
    };

这篇关于什么是C ++ const size_t在C#中的等价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆