Hi Team,
I am using sizeof() in c#
I need to find the memory size required by the array which is of the type strut.
Please see the snippet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace sizeOfDemo
{
struct Vertex
{
public float r;
public float g;
public float b;
public float x;
public float y;
public float z;
public Vertex(float r, float g, float b,float x, float y, float z)
{
this.r = r; this.g = g; this.b = b;
this.x = x; this.y = y; this.z = z;
}
}
public class MainClass
{
static void Main(string[] args)
{
Console.WriteLine("sizeof operations");
Vertex[] v = new Vertex[]{
new Vertex( 1.0f, 1.0f, 1.0f, -1.0f, 2.5f, -1.0f ),
new Vertex( 1.0f, 1.0f, 1.0f, -1.0f, 2.5f, 1.0f ),
new Vertex( 1.0f, 1.0f, 1.0f, 1.0f, 2.5f, 1.0f ),
new Vertex( 1.0f, 1.0f, 1.0f, 1.0f, 2.5f, -1.0f )
};
unsafe
{
Console.WriteLine("The size of Vertex is {0}\n", sizeof(Vertex));
Console.WriteLine("The size of Vertex is {0}\n", sizeof(v));///ERROR here.
Console.Read();
}
}
}
}
I want the size required for the Vertex Array.Please help me.
Using sizeof() in c# with arrays
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 26, 2009, 12:37 AM EST
This sizeof method is used to get the size of value types. You cannot use it for arrays.
But you can get the size of 'Vertex' type and multiply it by the length of the array.
Console.WriteLine("The size of v is {0}.", (sizeof(Vertex) * v.Length).ToString());
Please mark Do you like this answer checkbox, if this helps.