简体   繁体   English

Console.WriteLine() 错误 CS1503 无法从“void”转换为“bool”

[英]Console.WriteLine() error CS1503 cannot convert from 'void' to 'bool'

I am executing this simple program in which I am trying to see the outcome of Console.WriteLine with function returning void given as an argument.我正在执行这个简单的程序,我试图在其中查看Console.WriteLine的结果,函数返回 void 作为参数。

using System;

class Program
{
    static void printMe()
    {
    }

    static void Main(string[] args)
    {
        Console.WriteLine(printMe());
    }
}

This is giving following error:这是给出以下错误:

Program.cs(11,27): error CS1503: Argument 1: cannot convert from 'void' to 'bool' [C:\Users\Nafeez Quraishi\source\repos\X\X.csproj]

The build failed. Fix the build errors and run again.

As per the WriteLine documentation at https://docs.microsoft.com/en-us/dotnet/api/system.console.writeline?view=netframework-4.8 this looks to be corresponding to following case:根据https://docs.microsoft.com/en-us/dotnet/api/system.console.writeline?view=netframework-4.8 上的 WriteLine 文档,这看起来对应于以下情况:

WriteLine(Object)写线(对象)

Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream.将指定对象的文本表示,后跟当前行终止符,写入标准输出流。

Question is:问题是:

In order to write text representation of the function object returning void, why is it trying to covert it in to bool(than perhaps string)?为了编写返回 void 的函数对象的文本表示,为什么要尝试将其转换为 bool(而不是字符串)?

the void is treated specially by C# language and .net runtime. void 由 C# 语言和 .net 运行时特别处理。 so a user can not create an instance of void by static way or dynamic way.因此用户无法通过静态方式或动态方式创建 void 实例。

Now come to your question about why it converts into bool and not in the string.现在来谈谈为什么它会转换为 bool 而不是字符串的问题。

if you run below code in unsafe assembly.如果您在不安全的程序集中运行以下代码。 you can see it has size of 1 byte.你可以看到它的大小为 1 个字节。 this lead to convert into bool and not string这导致转换为 bool 而不是字符串

        static unsafe void Main(string[] args)
    {
        //Console.WriteLine(sizeof(System.Void));
        var o = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(void));
        Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(o));
        Console.Read();
        //Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(System.Void));
    }

How do you print one void (obviously means 'void'-nothing, not even null)?你如何打印一个空(显然意味着“空” - 没有,甚至不为空)? You must return something, not 'void'你必须返回一些东西,而不是 'void'

If you want, you can do如果你愿意,你可以做

static bool!!!!! printMe() //Hi I'm a function returning a value, bool precisely.
{
    return false; //I must return something.
}

Edit: you want the signature of the function/method?编辑:你想要函数/方法的签名? You want Reflection.你想要反射。 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection

static void Main()
{
    MethodInfo[] mInfo= typeof(TheClass).GetMethods();
    //now you can enumerate the methods, and know everything about them, even run them
}

Welcome to new way to use C#, there are so many space, get in.欢迎使用 C# 的新方法,还有很多空间,请进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#无法从void转换为bool - CS1503 - C# Cannot convert from void to bool - CS1503 如何解决此错误“错误 CS1503:参数 1:无法从‘void’转换为‘bool’” - How to resolve this error "error CS1503: Argument 1: cannot convert from 'void' to 'bool'" CS1503:参数 1:无法从“方法组”转换为“Action,bool&gt;” - CS1503: Argument 1: cannot convert from 'method group' to 'Action,bool>' C# 错误消息:CS1503Argument 1:无法从“void”转换为“bool” - C# Error Message: CS1503Argument 1: cannot convert from 'void' to 'bool' CS1503 参数 1:无法从 'string' 转换为 'string[*,*]' - CS1503 Argument1: cannot convert from 'string' to 'string[*,*]' CS1503 无法将矩阵转换为 MatrixOrder - CS1503 Cannot convert Matrix to MatrixOrder 错误 CS1503 - 无法从 Microsoft.Extensions.Configuration.IConfigurationSection 转换为 System.Action&lt;&gt; - Error CS1503 - Cannot convert from Microsoft.Extensions.Configuration.IConfigurationSection to System.Action<> C# 错误 CS1503 参数 1:无法从“字符串”转换为“字符” - C# Error CS1503 Argument 1: cannot convert from 'string' to 'Character' 有限 State 机器的问题“错误 CS1503:参数 1:无法从‘UIManager’转换为‘GameStateAbstract’” - Problem with a Finite State Machine "error CS1503: Argument 1: cannot convert from 'UIManager' to 'GameStateAbstract'" 错误 CS1503:参数 1:无法从“UnityEngine.XR.XRNode”转换为“string” - error CS1503: Argument 1: cannot convert from 'UnityEngine.XR.XRNode' to 'string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM