VB.Net中的类成员的这两种初始化方法之间有什么区别? [英] Whats the difference between these two initialization methods for a class member in VB.Net?

查看:250
本文介绍了VB.Net中的类成员的这两种初始化方法之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个obj初始化方法之间有什么区别?我已经看过这两种方法,但是知道是否有适当的时间使用一种方法.我发现这篇帖子涵盖了C#,但不确定是否VB.Net也是如此.

Whats the difference between these two initialization methods for obj? I've seen both of these, but know know if there is an appropriate time to use one vs the other. I found this post which covers C#, but not sure if the same applies to VB.Net.

Public Class Class1
   Sub New()
   End Sub

   Dim obj As New Object
End Class

vs

Public Class Class1
   Sub New()
      obj=New Object
   End Sub

   Dim obj As Object
End Class

如果这是重复的话,我会提前道歉.

My apologies ahead of time if this a duplicate.

推荐答案

在这种情况下,没有区别.主要区别在于您的构造函数是否执行其他操作-

In this case, there is no difference. The main difference would be if your constructor does other operations -

在这种情况下,内联初始化(Dim obj As New Object)将在构造函数内的任何代码之前发生.将初始化放入构造函数中,可以选择初始化的顺序.

In that case, the inline initialization (Dim obj As New Object) will occur prior to any code inside the constructor. Putting the initialization in the constructor lets you choose the order of initialization.

这篇关于VB.Net中的类成员的这两种初始化方法之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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