void 子程序返回什么? [英] What does a void subroutine return?
问题描述
我刚刚对复数视力进行了评估,并得到了以下问题.
I was just doing an assessment on pluralsight and was given the following question.
void 子程序返回什么?"
"What does a void subroutine return?"
我的印象是 void 子程序没有返回任何内容,但这不是所提供的答案之一(多选题).
I was under the impression that a void subroutine did not return anything, but this was not one of the answers provided (Multiple choice question).
.Net 是否在后台返回值类型或者这个问题不正确?
Does .Net return a value type in the background or is this question incorrect?
我得到的选择是:整数布尔值细绳日期时间
The choices I was given were: Integer Boolean String Datetime
推荐答案
System.Void
不是 nothing ,它是一个 struct
因此是一个值类型.
System.Void
is not nothing , it's a struct
hence a value type.
但是,作为 Sub
(VB.NET) 或返回"void
(C#) 的方法并没有真正返回值.所以你不能写这样的东西:
However, a method that is a Sub
(VB.NET) or "returns" void
(C#) does not really return a value. So you cannot write something like this:
System.Void nothing = Foo(); // Foo is a void-method
这不会编译(System.Void
不能在 C# 中使用 -- 使用 typeof(void) 来获取 void 类型对象").相关:为什么不是 System.Void?
This doesn't compile ("System.Void
cannot be used from C# -- use typeof(void) to get the void type object"). Related: Why not System.Void?
正如 Jeroen 和其他人提到的,实际上 void
方法确实不返回任何内容,因此正确答案是:它什么都不返回".
As Jeroen and others have mentioned, actually a void
method does really not return anything, so the correct answer was: "It returns nothing".
MSDN 提到它只在反射中有用:
MSDN mentioned that it's only useful in reflection:
"在 System.Reflection 命名空间中使用了 Void 结构,但是在典型应用中很少有用.Void 结构没有除了所有类型都继承自 Object 类的成员之外的成员."
"The Void structure is used in the System.Reflection namespace, but is rarely useful in a typical application. The Void structure has no members other than the ones all types inherit from the Object class."
如果您查看 void
关键字上的工具提示,您会看到它映射到 System.Void
.但同样,这并不意味着它是从方法返回的.它只是一个不存在的返回值的占位符.我猜 void
由于历史原因也存在,因为 C# 是基于 C 的.
If you look at the tooltip on the void
-keyword you see that it maps to System.Void
. But again, that doesn't mean that it is returned from the method. It's just a placeholder for a non-existing return value. I guess that void
also exists due to historical reasons since C# is based on C.
也值得一读:为什么 C 中的 void 意味着不无效?
这篇关于void 子程序返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!