如何找到类对象和静态类的大小? [英] How to find the size of class object and static class ?

查看:61
本文介绍了如何找到类对象和静态类的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。我想知道是否有可能找到类对象的大小。



2.如果类对象不包含任何数据(空值对象)将显示多大的尺寸。



3.我们可以找到静态类的大小。



我有什么试过:



考虑以下课程:



1. I want to know that is it possible to find the the size of class object.

2. If the class object don't hold any data(null value object) what size will be shown.

3. Can we find the size of a static class .

What I have tried:

Consider the following Classes:

public class Student
{
public int ID {get;set;}
public string Name {get;set;}
public string StuClass{get;set;}
}

public static class Teacher
{
public int EmpID{get;set;}
public string Name{get;set;}
public sting MonitoringClass{get;set;}
}





以下结果如下:





what will be the result of the following :

Student std1=new Student{1,"Test","Class2"};
Student std2=new Student();
int size1=sizeof(std1);//object has some data
int size2=sizeof(std2);//null object
int size3=Teacher{1,"TeacherTest","Class2"};//static class 





当我尝试执行它时,我收到以下错误:



1.错误CS0246:类型或命名空间名称`std1'无法执行被发现。你错过了一个程序集参考吗?



1.'TridToExcel.Test.Teacher'没有预定义的大小,因此sizeof只能在不安全的环境中使用(考虑使用System.Runtime.InteropServices.Marshal.SizeOf)D:\ SampleProjects \ GridToExcel \ GridToExcel \ Test.aspx.cs 32 25 GridToExcel



什么我做错了吗?



When i am trying to execute it ,i am getting following error:

1. error CS0246: The type or namespace name `std1' could not be found. Are you missing an assembly reference?

1. 'GridToExcel.Test.Teacher' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf) D:\SampleProjects\GridToExcel\GridToExcel\Test.aspx.cs 32 25 GridToExcel

what i am doing wrong??

推荐答案

请参阅 sizeof(C#参考) [ ^ ]。


好像,您可以找到问题的答案,但是您需要先解决编译错误。



按照说明操作:

编译器错误CS0246 [ ^ ]

编译器错误CS0233 [ ^ ]
Seems, you're able to find out the answer to your question, but you need to resolve compiler errors first.

Follow the instruction:
Compiler Error CS0246[^]
Compiler Error CS0233[^]


谢谢你的帮助。我发现sizeof用于查找非托管类型(int,float等)的大小。所以我做了一些搜索,我发现了这个。



Marshal.SizeOf方法(类型)( System.Runtime.InteropServices)



以下示例演示了调用SizeOf方法。此代码示例是为Marshal类提供的更大示例的一部分。



thank you for your help .I found out that sizeof is used to find the size of unmanaged type (int,float etc).so I did some search and I found out this.

Marshal.SizeOf Method (Type) (System.Runtime.InteropServices)

The following example demonstrates calling the SizeOf method. This code example is part of a larger example provided for the Marshal class.

// Demonstrate the use of the SizeOf method of the Marshal class.
Console.WriteLine("Number of bytes needed by a Point object: {0}", 
    Marshal.SizeOf(typeof(Point)));
Point p = new Point();
Console.WriteLine("Number of bytes needed by a Point object: {0}",
    Marshal.SizeOf(p));


这篇关于如何找到类对象和静态类的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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