3层Windows应用程序的数据层类中的变量初始化. [英] Variables initialization in Data Layer Class of 3-tier windows application.

查看:85
本文介绍了3层Windows应用程序的数据层类中的变量初始化.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下内容中,我们应该在3层体系结构的DataLayer类中初始化DataSet和其他变量.

1)在数据层类中''构造函数?或
2)在使用它们的方法中?

它们在构造函数中的初始化是否会导致提前分配内存?

由于构造函数是在我们向该类声明任何对象后立即调用的,因此分配给这些变量的内存将保持阻塞状态,直到我们使用这些变量并释放它们的内存为止.

或Visual Studio 2005自己管理.

Out of the followings where should we initialize our DataSets and other Variables in DataLayer class of 3-tier architecture.

1) In Data Layer class'' Contructor? or
2) In methods where they are used?

Does their initialization in constructor results to allocation of memory in advance?

Since constructor is invoked as soon as we declare any object to that class, will memory allocated for these variable shall remained blocked untill we use these variable and release their memory.

or Visual Studio 2005 manage it itself.

推荐答案

1)"...我们应该在哪里初始化我们的数据集和其他变量"……?
当涉及到"where"时,关于本地(堆栈变量)的问题就不存在了,因为在使用它们之前,应该始终在本地对其进行初始化.所以问题应该是关于类成员的,无论是静态的还是非静态的.没有严格的配方;这完全取决于代码的设计.

2)它们在构​​造函数中的初始化是否会导致预先分配内存?"
没有预先"或没有预先".构造所有引用类型的结果总是总是立即进行堆内存分配.创建堆栈帧时,局部变量还会消耗堆栈内存(对于引用类型,堆栈内存保留在堆栈帧上,并且还会分配堆内存).通常,为堆栈帧保留内存通常不称为分配".

3)因为一旦我们向该类声明任何对象,构造函数便会被调用……"
对于"C#"不是正确的.引用类型的声明不创建也不反对.预先声明声明引用类型的变量而无需初始化的代码使它变得null;没有调用构造函数. (这很容易使用调试器进行检查.)

4)分配给这些变量的内存是否会保持阻塞状态??"
这部分问题并没有真正的意义.这不是阻塞的内存"这样的概念. (如果拒绝阻止"应表示仍分配",则将在下面讨论此主题.)



释放内存":
内存是由垃圾回收器(GC)释放的,而不是由Visual Studio释放的,而是由.NET Framework释放的(通常,我们讨论的内容与Visual Studio无关;您可以编译并关闭它;所有发生的事情都在运行时发生)您的应用程序在.NET平台上运行).
GC自动运行(尽管您可以控制某些操作);它使用以下标准检测要释放的所有对象:是否丢失了对该对象的所有引用,因此是销毁对象. GC甚至可以检测相互或循环引用的对象的孤立孤岛"的情况(可以在调试器中轻松检查).最终,GC调用此类对象的析构函数并回收内存.对象变为丢失"对象后不会立即发生:GC根据其自身的优化策略决定"何时进行操作.从这个意义上说,破坏的时刻是未知的.任何代码都不应依赖于销毁对象的任何特定顺序.这是在良好的代码中很少使用析构函数的原因之一;在大多数情况下,不需要析构函数.

—SA
1) "...where should we initialize our DataSets and other Variables"…?
When it comes to "where", there is not such question about local (stack variables) as they always should be initialized locally before use. So the question should be about class members, static or not. There is no strict recipe; it totally depends of the design of the code.

2) "Does their initialization in constructor results to allocation of memory in advance?"
There is no "in advance" or not "in advance". Heap memory allocation always takes place immediately as a result of construction of all reference types. There is also stack memory consumed by local variables when a stack frame is created (for reference types, stack memory is reserved on a stack frame and heap memory is also allocated). Reservation of the memory for a stack frame usually is not called "allocation" though.

3) "Since constructor is invoked as soon as we declare any object to that class…"
Not true for "C#". A declaration of a reference type does not create and object; the pre-elaboration of the code declaring a variable of reference type without initialization makes it null; no constructor is called. (This is easy to check up using debugger.)

4) "will memory allocated for these variable… remained blocked…?"
This part of question does not really make sense. This is not such concept as "blocked memory". (If "remane blocked" should mean "remain allocated", this topic is covered below.)



"Release memory":
Memory is released by Garbage Collector (GC), and not by Visual Studio but by .NET Framework (in general, what we discuss has nothing to do with Visual Studio; you compile and close it; all what happens happens in run-time of your application runs on the .NET platform).
GC works automatically (you can control some actions though); it detects all objects to be released using the following criteria: is the all references to the object is lost, it is a subject of destruction. GC can even detect the situation of isolated "island" of objects mutually or cyclically referencing each other (which can be easily checked up under debugger). Eventually, GC calls the destructors of such objects and reclaims memory. It does not happen immediately after the object becomes "lost": GC "decides" when to do it according to its own optimized strategy. In this sense, the moment of destruction is unknown; no code should depend on any certain order of destruction of the objects. This is one of the reasons by which using destructors in a good code is relatively rare; in most cases destructors are not needed.

—SA


感谢这些信息.


这篇关于3层Windows应用程序的数据层类中的变量初始化.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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