1

I was looking at definition for void type in C#. It's an empty structure that represents a no value.

But, what is exactly the difference between this structure and others. I mean If you define a method with return type of void and then you return typeOf(void) you will get an error.

It seems that C# have different behavior with this type.

There is a little documentation of what void really is. I mean what's going on under the hood. how can a value type structure refer to nothing ?

UPDATE:

I think that some of you guys didn't get the point of this question and this is why you marked it as duplicate. My question is. Void is an structure. OK ? .. Structures are value types. In the lowest possible stage what happens ? what void represents in memory? What is the difference between this structure and others while if you press F12 on void you see only an empty structure ? Where c# makes this difference ?

Pouyan
  • 2,849
  • 8
  • 33
  • 39
  • http://msdn.microsoft.com/en-us/library/yah0tteb(v=vs.110).aspx – Darren Jul 01 '13 at 12:10
  • check this link.. http://stackoverflow.com/questions/1043034/what-does-void-mean-in-c-c-and-c – user1102001 Jul 01 '13 at 12:11
  • 2
    @DarinDimitrov: `void**` is very different to `void`. – Jon Skeet Jul 01 '13 at 12:13
  • I Still believe that it's not a duplicate question. My question is about the philosophy and theory. Of course that I know what void means. none of questions above are my answers. please refrain making such misundestandings – Pouyan Jul 02 '13 at 04:54

5 Answers5

12

The point is that a method with a void return type does not return anything. It doesn't return a value of type void; it has no return value at all.

You can't declare a method of that type; you can't declare a parameter to be void... it represents the absence of any value at all. The type System.Void really only exists to facilitate reflection, where things like MethodInfo.ReturnType need to indicate that a method doesn't return anything.

There are many ways in which it would be useful if void were a real type, just as Unit is in F#. For example, we could then just have Task<T> instead of Task<T> and Task... effectively Task is Task<void>, but that's not valid...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I could probably try this when I get back to my computer.. but do `void` pointers work in unsafe C#? – Simon Whitehead Jul 01 '13 at 12:19
  • @SimonWhitehead: I think void pointers end up meaning something a bit different. I try to avoid pointers myself :) (But yes, they work.) – Jon Skeet Jul 01 '13 at 12:23
3

It's actually fairly well documented. It's a fundamental concept in type theory, related to the unit type.

In fact you can use typeof(void), although there's not very much you can do with it. The reason why

public void foo() { return typeof(void); } 

won't work is because the result of typeof(void) isn't an instance of the void type, it's an instance of the Type type. The proper way to write this, is simply

public void foo() { return; } 
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
3

Some languages has 2 different forms of subroutines. Procedures and Functions. The difference is that procdures are a sequence of operations. Functions are operations with a return statement.

In C# everything is a "function", so the void keyword just show, that it is not actually a function, because it has no return statement.

Crusader633
  • 595
  • 1
  • 4
  • 17
1

When used as the return type for a method, void specifies that the method doesn't return a value.

http://msdn.microsoft.com/en-us/library/yah0tteb(v=vs.110).aspx

Darren
  • 68,902
  • 24
  • 138
  • 144
1

The meaning of the word Void means none. where a Value is void it means it has no Value. in a method or function it means it doesen't have a return Type therefore your function doesent have to return anything to run

public static void TestRefType()
{
SampleRefType rt = new SampleRefType();
rt.value = 44;
ModifyObject(rt);
System.Console.WriteLine(rt.value);
}
Jono
  • 163
  • 1
  • 5