473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why can't C# use System.Void type?

With generics in .NET 2, there are certain scenarios in which using
System.Void would be useful:

TReturnType MyFunction<TReturnType>()
{
return default(TReturnType);
}

Above is a simple function that would allow me to make a generic method that
would return any type I instanciate the method with. But what if I wanted to
return void? Well, I will just call MyFunction<System.Void>(). Unfortunately,
the compiler issues an error saying System.Void isn't accessible in C#.

What is the reasoning behind this? Why are we not allowed to use System.Void
in C#?
Nov 17 '05 #1
2 5340

"Judah" <Ju***@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
With generics in .NET 2, there are certain scenarios in which using
System.Void would be useful:

TReturnType MyFunction<TReturnType>()
{
return default(TReturnType);
}

Above is a simple function that would allow me to make a generic method
that
would return any type I instanciate the method with. But what if I wanted
to
return void? Well, I will just call MyFunction<System.Void>().
Unfortunately,
the compiler issues an error saying System.Void isn't accessible in C#.

What is the reasoning behind this? Why are we not allowed to use
System.Void
in C#?


Basically is that you can't actually return a void, have a parameter, field,
or variable of type void, or use a method that has a void return type
generically(how do you use a method that returns a value only sometimes?
Methods that return void push nothing on the stack, methods that return
something push something on the stack, the implementation of the two are
very different.) The sole legal use of void would be to specify that a
method does not return a value, and thus having a method that is returns
values only sometimes is quite different than a method that is guarenteed to
return a value or guarenteed not to.

Consider this code, which is functionally equivilent to yoru original code.
Does it actually make any sense when looked at like this?:

TReturnType MyFunction<TReturnType>()
{
TReturnType type = default(TReturnType);
return type;
}

Nov 17 '05 #2
And what if you use the keyword "void"?
"Judah" <Ju***@discussions.microsoft.com> schrieb im Newsbeitrag
news:9E**********************************@microsof t.com...
With generics in .NET 2, there are certain scenarios in which using
System.Void would be useful:

TReturnType MyFunction<TReturnType>()
{
return default(TReturnType);
}

Above is a simple function that would allow me to make a generic method that would return any type I instanciate the method with. But what if I wanted to return void? Well, I will just call MyFunction<System.Void>(). Unfortunately, the compiler issues an error saying System.Void isn't accessible in C#.

What is the reasoning behind this? Why are we not allowed to use System.Void in C#?

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
4
by: Hai Nguyen | last post by:
I'm learning C sharp and do not like vb much. I'm creatiing a wepage using panel to test myself. I tried to use these code below, which is written in VB, and to transform them to c sharp but I got...
9
by: Oberon | last post by:
My HashTable (Global.Games) is a static collection of objects of type Game. A Game object has 8 fields (exposed as properties). The key to the HashTable is also one of these fields (GameID, of type...
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
6
by: Dariusz Tomon | last post by:
Hi How can I get url of page so taht I can pass it to string varaible. I have got several urls in my IIS under one folder. I want to have one default.aspx where code under it recognize which...
6
by: Pucca | last post by:
I have a program that originally compiles into a exe file. I changed the compile option to generate dll file. This program calls a com component. Can I use pinvoke in C# to call it? The...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
1
by: kret | last post by:
Hi, this is my first post so first of all I would like to say hello :) Now getting to my problem. In my job I have to create an ActiveX control in .NET 1.1 that can be lunched from IE....
9
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.