C#中类的大小 [英] Size of a class in c#

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

问题描述

大家好,


请让我知道C#中类的大小.

例如
a)班级信息的大小是多少?

hi guys,


please let me know what is the size of the class in c#.

for e.g

a) what is the size of class info ?

class info
{
    protected string name;
    protected string age;
    protected string emailid;
   
    public void display()
    { 
Anything u want to display....
}
}


b)这个空类的大小是多少?


b)what is the size of this empty class ?

class studentinfo
{ } 


请说明每种数据类型的大小功能简介.

提前谢谢.

Aditya.


please explain the size of each datatype & function in brief.

Thanks in advance.

Aditya.

推荐答案

在这里您可能有一个概念上的问题-该类的大小并没有真正暴露给您,在这种情况下,是开发人员. />
用于存储它的底层机制是编译器详细信息-编译器也在优化-这意味着如果可以提高性能,则可以自由更改某些对象的大小.

通常,如果数据成员是对象类型,它将在类的大小上增加32/64位(以处理其地址).

如果数据成员是值类型,则它将值类型的大小添加到类中.

再一次-不要依赖此数据-编译器可以自由选择如何存储该类,只要它符合标准即可.

类的大小是C ++的一个概念,即使那样也是片​​状的-如果有问题的编译器以特定的方式实现,那么C ++中的sizeof()也可以测量虚拟函数表指针的大小.

如果您确实需要在内部进一步了解该对象的大小,可以让我们知道为什么吗?这可能使事情变得更清楚:)
You may have a conceptual problem here - the size of that class is not really supposed to be exposed to you, the developer in this case.

The underlying mechanisms used to store it are compiler details - also the compiler is optimising - which means that it is perfectly at liberty to change the sizes of certain objects if it provides a performance upgrade.

In general if an data member is an object type it will add 32/64 bits to the size of the class (to handle the address to it).

If the data member is a value type, it will add the size of the value type to the class.

Again - don''t rely on this data - the compiler is at liberty to choose how to store the class as long as it adheres to the standard.

Sizes of classes is a C++ concept, and even then is flaky - sizeof() in C++ can also measure the size of the virtual function table pointer IF the compiler in question implements one in a specific way.

If you really need to know more about the size of this object internally, can you let us know why? This may make things clearer :)


我认为最好的答案是DaveKerr提供的.我只会说说封送处理.

如果要将类编组为 native 代码,则可以计算类的大小.您可以使用Marshal.SizeOf方法来获取可编组"类型的大小.最终应使用某些特定属性声明此类型和该类型的数据成员:MarshalAsStructLayout,...

如果需要了解有关封送处理的信息,则有很多教程.
I think the best answer was given by DaveKerr. I will just say a word about marshaling.

It is possible to compute the size of a class if you want to marshal it to native code. You can use the Marshal.SizeOf method to get the size of a "marshalable" type. This type and data members of that type should eventualy be declared with some specific attributes: MarshalAs, StructLayout, ...

There are lots of tutorials about marshaling if you need to learn more about that.


.NET运行时并未公开计算引用类型大小的机制.通常,您无需理会.您可以对类中每个成员元素的大小求和.请注意,不能将sizeof运算符用于引用类型,因为它会返回指针的大小,而不是对象使用的实际内存量.
The .NET runtime does not expose the mechanism of calculating the size of a reference type. Normally you wouldn''t care of it. You can play with summing up the sizeof each member element of the class. Note that you cannot use sizeof operator for reference type as it will return the size of the pointer but not the actual amount of memory used by the object.


这篇关于C#中类的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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