-2

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.

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://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=netframework-4.8 this looks to be corresponding to following case:

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)?

Nafeez Quraishi
  • 5,380
  • 2
  • 27
  • 34
  • 2
    I’m not sure what you’re trying to do exactly. If you are looking to write the name of the function, this would do that: Console.WriteLine(nameof(printMe));. You can’t just dump the c# definition of the method if that’s what you meant. If you wanted to do that, you’d need to do something far more complex. If you want to dump the output of the method then the method can’t return void as void is not even null. – rrreee Jan 11 '20 at 21:00
  • basically, it doesn't matter what it's trying to convert it to. because void is void. it's the absense of any value or reference - it's not even NULL. – Franz Gleichmann Jan 11 '20 at 21:04
  • @rrreee, bool in the error did not make sense to me, so i was trying to understand that. If you are having an answer, even if "complex", please share that. – Nafeez Quraishi Jan 12 '20 at 09:24
  • What I was alluding to is that you can take a string which is valid C# code, compile it at runtime and execute what you compiled. See https://stackoverflow.com/q/826398 for ways to do that. I’ve done it and it’s quite tough, especially with dependencies, and generally not worth the trouble as you won’t discover compile errors until runtime rather than build time. It was handy for me when I used it to unit test a code generator. Anyway if you did that, then you have the C# code in a string and you could write it to the console, but I can’t recommend it, unless there is no other alternative. – rrreee Jan 12 '20 at 15:25

2 Answers2

2

the void is treated specially by C# language and .net runtime. so a user can not create an instance of void by static way or dynamic way.

Now come to your question about why it converts into bool and not in the string.

if you run below code in unsafe assembly. you can see it has size of 1 byte. this lead to convert into bool and not string

        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));
    }
divyang4481
  • 1,584
  • 16
  • 32
-1

How do you print one void (obviously means 'void'-nothing, not even null)? You must return something, not '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://learn.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.

DrkDeveloper
  • 939
  • 7
  • 17