gpt4 book ai didi

c# - 公共(public)成员与 C# 中类的构造函数?

转载 作者:行者123 更新时间:2023-11-30 13:15:21 24 4
gpt4 key购买 nike

我有疑问。

1. namespace JIMS.ViewModel.Stock
2. {
3. internal class StockGroupViewModel : JIMS.ViewModel.BaseViewModel
4. {
5. JIMSEntities dbContext = new JIMSEntities();
6.
7. public StockGroupViewModel()
8. {
9. dbContext = new JIMSEntities();
10. }
11. }
12. }

我有这门课。我想知道先调用哪个。当我创建此类的实例时

StockGroupViewModel s = new StockGroupViewModel();

第 5 行或第 9 行。

最佳答案

第 5 行 - 它是一个字段初始化程序,在构造函数中的任何代码之前执行。

来自规范:

10.5.5.2 Instance field initialization

The instance field variable initializers of a class correspond to a sequence of assignments thatare executed immediately upon entry to any one of the instanceconstructors (§10.11.1) of that class. The variable initializers areexecuted in the textual order in which they appear in the classdeclaration. The class instance creation and initialization process isdescribed further in §10.11.

关于c# - 公共(public)成员与 C# 中类的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9912190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com