-1

I know it is a foolish question to ask but after reading the very first answer of Mr. Matteo Italia If void() does not return a value, why do we use it?, I have performed this small experiment in C# 4.0

class Program
    {
        static void Main(string[] args)
        {
            var x = (int)5; // worked as expected 
            var x1 = (void)10; // Error 'void' cannot be used in this context

        }        
    }

Why?

Community
  • 1
  • 1
priyanka.sarkar
  • 25,766
  • 43
  • 127
  • 173
  • 1
    Notice that my answer referred to C and C++, C# is a different language with its own rules. – Matteo Italia Dec 22 '14 at 06:47
  • void generally used mainly with 'methods' to tell the caller that dont expect any return value from calling method. – Avijit Dec 22 '14 at 06:48
  • The question you linked to refers to a C or C++ construct and has no relation to C#. It would be pointless to try to discuss why it doesn't work in C#. – Jeff Mercado Dec 22 '14 at 06:56
  • After reading the answers of various people , I am now in a dilemma...is void not a datatype in C#? then what it is? – priyanka.sarkar Dec 22 '14 at 06:58
  • I think the answer is explained nicely [Why is 'void' not allowed as a generic type in C#][1] by Eric Lippert. [1]: http://programmers.stackexchange.com/questions/131036/why-is-void-not-allowed-as-a-generic-type-in-c – priyanka.sarkar Dec 22 '14 at 07:02

1 Answers1

1

Void is not a data type and hence we cannot cast anything to void type. I believe you are probably coming from a C world where we could have a void* which is very different.

danish
  • 5,550
  • 2
  • 25
  • 28
  • Technically it is a type in C#, http://msdn.microsoft.com/en-us/library/system.void.aspx, but I think most people think of it as a keyword. – Ola Ekdahl Dec 22 '14 at 06:54
  • I just saw this SO question http://stackoverflow.com/questions/16644914/what-is-the-practical-use-of-void-datatype-and-void-pointer after reading ur answer that void is not a datatype.But it is pertaining to C – priyanka.sarkar Dec 22 '14 at 06:56
  • http://programmers.stackexchange.com/questions/131036/why-is-void-not-allowed-as-a-generic-type-in-c – priyanka.sarkar Dec 22 '14 at 07:03