Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C+的等价物是什么+;常数大小(单位:C)?_C#_C++ - Fatal编程技术网

C# C+的等价物是什么+;常数大小(单位:C)?

C# C+的等价物是什么+;常数大小(单位:C)?,c#,c++,C#,C++,我试图将一些食人魔代码翻译成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

我试图将一些食人魔代码翻译成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不能用于声明数组的大小

我想知道如何用常量值声明数组

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

size\t中声明数组的方法是一个typedef(有点像#define宏),它基本上是另一种类型的别名。它的定义取决于SDK,但通常是无符号int

无论如何,在这种情况下,这并不重要,因为它们是常量,所以您知道nVertices是8,vbufCount是48。你可以这样用C写:

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

这不是因为
const int
,而是因为数组大小不是C#中数组类型的一部分。您可以将代码更改为:

float[] vertices = {
        -100.0f,100.0f,-100.0f,     //0 position
        -sqrt13,sqrt13,-sqrt13,     //0 normal
        //... 
       -sqrt13,-sqrt13,sqrt13,      //7 normal
};
还有其他几种方法可以做同样的事情,包括:

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

唯一的区别是,如果初始化器中的项数与指定的数字不匹配,则将得到编译时错误。

< P> C++,SiZeHT是至少16位的无符号整数类型,遵循CPU的本机整数类型。换句话说,sizeof(size_t)不是固定的,即使大多数人将其用作“unsigned int”。在C#中没有这样的事情

C#(例如,使用数组和列表时)中的大小通常为'int'类型,它是一个32位整数

在您的情况下,我可能会将数组设置为只读,并使用“顶点.长度”,例如:

    private readonly float[] vertices = new float[]
    {
        1f,2f,3f,4f,5.2f // etc
    };
或者在这种情况下,我可能会将其定义为2D数组,并使用顶点。GetLength:

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

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

所有这些答案实际上并没有回答什么类型的衣服等同于尺码的问题。NET中大小的正确等效类型为UIntPtr。它在32位平台上是32位的,在64位平台上是64位的,并且没有签名。这是唯一真正等价的类型。

你不需要显式地写数组的大小,至少不是C++中的。数组已经固定了,所以没有必要把它的大小建立在常量上。这是一种方法。您也可以用不同的方式编写它,包括
float[]array={1.2F,2.3F,3.4F,4.5F}顺便说一句,这不会编译,因为
100.0
double
并且它不能隐式转换为
float
。您可以使用
f
后缀:
100.0f
来修复此问题。
    private readonly float[,] vertices = new float[5,5];

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