473,804 Members | 3,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<TRet urnType>()
{
return default(TReturn Type);
}

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<Syst em.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 5361

"Judah" <Ju***@discussi ons.microsoft.c om> wrote in message
news:9E******** *************** ***********@mic rosoft.com...
With generics in .NET 2, there are certain scenarios in which using
System.Void would be useful:

TReturnType MyFunction<TRet urnType>()
{
return default(TReturn Type);
}

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<Syst em.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<TRet urnType>()
{
TReturnType type = default(TReturn Type);
return type;
}

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

TReturnType MyFunction<TRet urnType>()
{
return default(TReturn Type);
}

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<Syst em.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
2218
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 reflection. The code I supplied is based on the SqlDataAdapter with reflection. The error occurs when trying to create the delegate that will be passed in to EventInfo.AddEventHandler. I get the following error: An unhandled exception of type...
4
2516
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 hard time to understand vb syntax. I don't know if anyone in here can point out which is the equivalent object used in c sharp. Translate these two lines to C sharp: Sub Next_Click(Sender As Object, e As EventArgs) Select Case...
9
4041
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 int). When I try to create a SortedList from the HashTable with the following: SortedList sortedGames = new SortedList(Global.Games); I get an error message:
5
1915
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
29656
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 url is used by user and then display appropriate data to that url (one url is about fashion, other about law and so on). By means of recognition of url I can display appropriate banners for example.
6
2213
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 following is the main dll file. #include "stdafx.h" #define MAX_ADSPATH_CHARS 2048 //*************************************************************************** // local function prototypes
3
4037
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 selects? thanks...
1
8303
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. This part is done but next requirement is that after closing ActiveX I have to redirect parent page to location X or Y depending on how the
9
1936
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 like this : (very simple..........) 010001010110001101010101010101010101010101 #include<stdio.h>
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.