By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,132 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,132 IT Pros & Developers. It's quick & easy.

void why is it needed as a return type?

P: n/a
Just curious, we no longer require void on a method that takes no
parameters;

void methodWithNoParameters(void) has been replaced by void
methodWithNoParameters().

Is there a good reason we still have to specify void for a method that
returns no result? For example;

private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().

Nov 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Hi,

It is the C# lenguage definition, and also making things explicit improves
the code understanding.

Best regards
Salva
"ge*****************@yahoo.co.uk" wrote:
Just curious, we no longer require void on a method that takes no
parameters;

void methodWithNoParameters(void) has been replaced by void
methodWithNoParameters().

Is there a good reason we still have to specify void for a method that
returns no result? For example;

private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().

Nov 17 '05 #2

P: n/a
How would you differentiate the following

class Foo
{
// this is a default constructor
public Foo()
{
}
// this is a method called Foo that returns nothing
public Foo()
{
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Just curious, we no longer require void on a method that takes no
parameters;

void methodWithNoParameters(void) has been replaced by void
methodWithNoParameters().

Is there a good reason we still have to specify void for a method that
returns no result? For example;

private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().

Nov 17 '05 #3

P: n/a
Salvador,
Just because it's the language definition doesn't mean it's the best
way of doing things. Also you are explicitly not having a return type
on the method prototype and so you're explicitly saying there's no
return type.

Richard,
Good point. In way of doing things you'd not allow methods named the
same as the class they are being called on - it'll throw a compiler
error. Can you thing of a good reason why a method would have to be
called the same as the class?

Either way it's no big deal...

Nov 17 '05 #4

P: n/a
Hi,

I haven't say that the lenguage definition is the best way to do things,
it is just a definition, you are free to choose your lenguage, there is no
performance implication on this, actually if you generate your own lenguage
based on ASM your return type is a memory address. What C# is doing is
reseting the address with 0000000 on the heap, specially assigned for void
type, when you assign void this address is protected so you can not use it.
At the end, no performance implication, is only a matter of how the lenguage
understand the things. You can also ask why the lenguage use the "for"
statement instead of (int i = 0; i< x; i++) without a for? It is just a
matter of definitions , that's all.

Regards
Salvador


"ge*****************@yahoo.co.uk" wrote:
Salvador,
Just because it's the language definition doesn't mean it's the best
way of doing things. Also you are explicitly not having a return type
on the method prototype and so you're explicitly saying there's no
return type.

Richard,
Good point. In way of doing things you'd not allow methods named the
same as the class they are being called on - it'll throw a compiler
error. Can you thing of a good reason why a method would have to be
called the same as the class?

Either way it's no big deal...

Nov 17 '05 #5

P: n/a
I guess the issue is that to some degree not allowing a method the same name as the type of a fairly arbitrary restriction and when combined with C# tending to favor explicit syntax rather than implied behavior would be counter-intuitive to the rest of the language.

Another issue is if I have

class Bar
{
public Foo()
{
}
public Bar()
{
}
}

if I then rename the type as Foo then suddenly Foo becomes a constructor and i may miss that and get very strange behavior when I create instances

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Richard,
Good point. In way of doing things you'd not allow methods named the
same as the class they are being called on - it'll throw a compiler
error. Can you thing of a good reason why a method would have to be
called the same as the class?


Nov 17 '05 #6

P: n/a
Well, I have another opinion. I program in Delphi as well and in that
language you have 2 different types of functions. You have what are called
functions which would return something and you have things called procedures
which do not. However, to declare these things you have to type out
function or procedure in front of the function name so the compiler knows
what you plan to do.

So taking that into account I think that is the answer. The reason that its
necessary to specify a return type is so that the compiler can be sure of
your intentions. Parameters are not that important because you either have
them or you don't, but trying to return something when you said you were not
going to, or perhaps not returning something when you said you were could
cause problems in other places. By forcing you to tell the compiler up
front what sort of contract you will have with a particular function you
allow it to always know that you do what you said you were going to do.

Otherwise it would take it longer to do its job because it would have to
determine your intentions only after analysing the entire code section...
Also when looking at calling things from external modules it could just be a
runtime error because there may be cases where that determination can't be
made at design time due to functions being contained in COM objects or other
various places...

just my 2 cents,

glenn
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:ul**************@tk2msftngp13.phx.gbl...
How would you differentiate the following

class Foo
{
// this is a default constructor
public Foo()
{
}
// this is a method called Foo that returns nothing
public Foo()
{
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Just curious, we no longer require void on a method that takes no
parameters;

void methodWithNoParameters(void) has been replaced by void
methodWithNoParameters().

Is there a good reason we still have to specify void for a method that
returns no result? For example;

private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().

Nov 17 '05 #7

P: n/a
To think... all of this discussion because of 4 simple characters (v-o-i-d)?

I'm at a loss for words... lol

~d

glenn wrote:
Well, I have another opinion. I program in Delphi as well and in that
language you have 2 different types of functions. You have what are called
functions which would return something and you have things called procedures
which do not. However, to declare these things you have to type out
function or procedure in front of the function name so the compiler knows
what you plan to do.

So taking that into account I think that is the answer. The reason that its
necessary to specify a return type is so that the compiler can be sure of
your intentions. Parameters are not that important because you either have
them or you don't, but trying to return something when you said you were not
going to, or perhaps not returning something when you said you were could
cause problems in other places. By forcing you to tell the compiler up
front what sort of contract you will have with a particular function you
allow it to always know that you do what you said you were going to do.

Otherwise it would take it longer to do its job because it would have to
determine your intentions only after analysing the entire code section...
Also when looking at calling things from external modules it could just be a
runtime error because there may be cases where that determination can't be
made at design time due to functions being contained in COM objects or other
various places...

just my 2 cents,

glenn
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:ul**************@tk2msftngp13.phx.gbl...
How would you differentiate the following

class Foo
{
// this is a default constructor
public Foo()
{
}
// this is a method called Foo that returns nothing
public Foo()
{
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Just curious, we no longer require void on a method that takes no
parameters;

void methodWithNoParameters(void) has been replaced by void
methodWithNoParameters().

Is there a good reason we still have to specify void for a method that
returns no result? For example;

private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().


Nov 17 '05 #8

P: n/a
<ge*****************@yahoo.co.uk> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().


Well, first of all, "private" is already assumed so
private methodWithNoReturn() {...}
could be written as:
methodWithNoReturn() {....}

But, in C/C++ that means "returning a integer". When you have identical
syntax, you should have them to the same thing.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 17 '05 #9

P: n/a
You think thats bad?

Have a look at how much has been written about 7 characters (D-i-s-p-o-s-e) ;-)

Regards

Richard Blewett - DebelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

To think... all of this discussion because of 4 simple characters (v-o-i-d)?

I'm at a loss for words... lol
Nov 17 '05 #10

P: n/a
Typing four characters is not the issue. It's the "why" that I'm
interested in? It think Richard's comment about refactoring and
constructors probably is the best reason for explicit return types is
I've heard so far.

Nov 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.