gpt4 book ai didi

.net - New之后什么都没有,有可能吗?

转载 作者:行者123 更新时间:2023-12-02 00:13:10 25 4
gpt4 key购买 nike

NothingNew 之后,这可能吗?

Dim myObj As MyClass = Nothing
myObj = New MyClass(params)
If myObj Is Nothig Then
' is it possible?
End If

构造函数返回空(无)对象是否存在理论上的可能性?

比如说,在构造函数中设置 Me = Nothing?或者如果在构造函数中抛出异常,那么在捕获对象时会发生什么?或者在构造函数的最后一行中,我通过引用“Me”传递给一个方法,而这个方法将该引用设置为 Nothing?

最佳答案

不,New-operator用于创建新的对象实例。即使此对象的所有字段都保持为 Nothing,实例本身也不是 Nothing

Visual Basic Language Specification:

11.10 New Expressions The New operator is used to create new instances of types. ....

11.10.1 Object-Creation Expressions An object-creation expression is used to create a new instance of a class type or a structure type. The type of an object creation expression must be a class type, a structure type, or a type parameter with a New constraint and cannot be a MustInherit class. Given an object creation expression of the form New T(A), where T is a class type or structure type and A is an optional argument list, overload resolution determines the correct constructor of T to call. A type parameter with a New constraint is considered to have a single, parameterless constructor. If no constructor is callable, a compile-time error occurs; otherwise the expression results in the creation of a new instance of T using the chosen constructor. If there are no arguments, the parentheses may be omitted. Where an instance is allocated depends on whether the instance is a class type or a value type. New instances of class types are created on the system heap, while new instances of value types are created directly on the stack. An object-creation expression can optionally specify a list of member initializers after the constructor arguments. These member initializers are prefixed with the keyword With, and the initializer list is interpreted as if it was in the context of a With statement.

关于.net - New之后什么都没有,有可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14463146/

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