普通类对象可以在静态类中调用 [英] normal class object can be called in static class

查看:97
本文介绍了普通类对象可以在静态类中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



有一个静态类,它有一个名为add()的方法和一个普通类。它还有一个名为sum()的方法。我将在普通类的静态类中创建100个对象...?

将占用多少内存1或100?



谢谢你

Hi friends,

There is one static class, it has one method called add() and one normal class. It has also one method called sum(). I will create 100 objects in the static class of the normal class...?
How much memory will occupy 1 or 100?

Thanks You

推荐答案

如果静态类的(方法)创建了100个'标准'类的对象,那么显然,使用的内存将是 100 乘以一个实例(数据成员)所需的一个。
If the (the method) of the static class creates 100 object of the 'standard' class then, obviously, the memory used will be 100 times the one required for (data members of) one instance.


您是否真的尝试过代码?设置一个断点,然后单步执行代码(在Visual Studio中使用F11):每一步都看一下变量的值。



什么你的代码告诉我的是你对C#非常困惑,或者,如果你在课堂上,这是家庭作业的一部分,那么你的老师(和他们的课程)很困惑。



在您的代码中,'test Class的每个实例都是在静态类''Add方法中创建的:它只存在于该方法的范围内一旦.NET的垃圾收集器出现就被处理掉。



因此,在这里,你将永远不会分配内存超过两个实例每次调用静态类的'测试类''添加方法。



您可以探索和教育自己的问题:



1.范围:对象的持久性;访问修饰符



2.. NET的内存分配和垃圾处理
Have you actually tried your code ? Set a breakpoint(s), and single-step through the code (using F11 in Visual Studio): with each step look at the values of the variables.

What your code says to me is that you are either very confused about C#, or, if you are in a class, and this is part of homework, then your teacher (and their curriculum) is very confused.

In your code each instance of the 'test Class is created in the static class' 'Add method: it exists only in the scope of that method, and it will be disposed as soon as .NET's garbage collector comes around.

So, it is never the case, here, that you will allocate memory for more than the two instances of the 'test Class with each call to the static class' 'Add method.

Issues for you to explore and educate yourself about:

1. Scope: the persistence of objects; access modifiers

2. .NET's memory allocation and garbage disposal


这篇关于普通类对象可以在静态类中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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