473,396 Members | 2,106 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,396 software developers and data experts.

Question on Method Signatures

Greetings All,

Does anyone have any idea why a method signature does not
include the return type?

Example:

int someFct( int a, int b );

is the same signature as

double someFct( int a, int b );

and, as such, cannot co-exist in a class.

Is there some kind of object-oriented base ~ sub-class
relationship that is helped by not including the return
value in the signature?

Peace & Blessings,
Robert McCall

Nov 17 '05 #1
7 1385
Robert McCall wrote:
Does anyone have any idea why a method signature does not
include the return type?


Because overloading by return type would be really nasty - it would
mean that there would be no such concept as the type of an expression;
it would depend on what was being expected.

I don't *think* that occurs anywhere in C# 1.1. It might to some extent
with C# 2.0 in terms of generics (it does with Java generics), but it's
generally a bad idea, IMO.

For one thing, what would you expect to be called if someone were just
to call the function without *using* the return value?

Jon

Nov 17 '05 #2
Because the caller can ignore the function result and the compiler would not
know which function to call. At least using VB.NET you can have:

Call MyFunction(a,b);

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Robert McCall" <mi*******@gmail.com> escribió en el mensaje
news:11*********************@g49g2000cwa.googlegro ups.com...
Greetings All,

Does anyone have any idea why a method signature does not
include the return type?

Example:

int someFct( int a, int b );

is the same signature as

double someFct( int a, int b );

and, as such, cannot co-exist in a class.

Is there some kind of object-oriented base ~ sub-class
relationship that is helped by not including the return
value in the signature?

Peace & Blessings,
Robert McCall

Nov 17 '05 #3
Thanks Jon (and Carlos below),

Ah, I see now, yet I have occasionally found the desire to
create such functions to prevent my manually having to
call the necessary function (or make the cast) on the
return value.

Of course, one of my general gripes with polymorphic
languages is that each name should be different in some way
to specifiy the *exact* function that is being performed. In
the case of constructors they are not all just "new" functions,
they are all specific variants of a new function whose parameters
purposefully influence how the class is instantiated.

Of course, with a more specific (i.e. non text-based) IDE like
the Intentional Programming environment, selecting the exact
function is required if there is any ambiguity and the link isn't
to the name, but to the actual function definition.

Anyway, thanks for answering my question.

Peace & Blessings,
Robert McCall

Nov 17 '05 #4
Using the new Generics in C#, you can declare a Generic method, such as:

public T doSomething<T>(T returnVal)
{
return returnVal;
}

You can also create a method that returns a Generic Type declared as a Type
parameter in the class in which it resides:

/// <summary>
/// Base class for various Dictionaries with string key
/// </summary>
/// <typeparam name="T">Type of Values</typeparam>
public class GenericDictionaryCollection<T>
: System.Collections.Generic.Dictionary<String, T>
{

/// <summary>
/// Overridable default Item method
/// </summary>
/// <param name="key">key of Item</param>
/// <returns>Value of Item</returns>
new virtual public T this[string key]
{
get { return base[key]; }
set
{
base[key] = value;
}
}

//...
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Robert McCall wrote:
Does anyone have any idea why a method signature does not
include the return type?


Because overloading by return type would be really nasty - it would
mean that there would be no such concept as the type of an expression;
it would depend on what was being expected.

I don't *think* that occurs anywhere in C# 1.1. It might to some extent
with C# 2.0 in terms of generics (it does with Java generics), but it's
generally a bad idea, IMO.

For one thing, what would you expect to be called if someone were just
to call the function without *using* the return value?

Jon

Nov 17 '05 #5
Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
Using the new Generics in C#, you can declare a Generic method, such as:

public T doSomething<T>(T returnVal)
{
return returnVal;
}


But could you do:

public T doSomething<T where T:new()>()
{
return new T(); // Whatever the syntax is - can't remember offhand
}

Then:

object o = doSomething();
MemoryStream m = doSomething();

You can in Java with scary amounts of type inference. Not sure I
entirely approve, to be honest...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Jon,
| public T doSomething<T where T:new()>()
Yes you can define a generic function where the parameter is only used for
the return type.

However you need to supply the type parameter when you call the method,
something like:

| object o = doSomething<object>();
| MemoryStream m = doSomething<MemoryStream>();

However! it "violates" an FxCop rule as its "ambiguous".

Here is a thread that discusses it:

http://groups.google.com/group/micro...4e46ca8f99b798

Personally I find in the case of GetCustomAttribute (as the thread shows) it
makes sense as the type parameter is encapsulating the downcast, plus the
type parameter is used to "do work".

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
| Kevin Spencer <ke***@DIESPAMMERSDIEtakempis.com> wrote:
| > Using the new Generics in C#, you can declare a Generic method, such as:
| >
| > public T doSomething<T>(T returnVal)
| > {
| > return returnVal;
| > }
|
| But could you do:
|
| public T doSomething<T where T:new()>()
| {
| return new T(); // Whatever the syntax is - can't remember offhand
| }
|
| Then:
|
| object o = doSomething();
| MemoryStream m = doSomething();
|
| You can in Java with scary amounts of type inference. Not sure I
| entirely approve, to be honest...
|
| --
| Jon Skeet - <sk***@pobox.com>
| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too
Nov 17 '05 #7
Jay B. Harlow [MVP - Outlook] <Ja************@tsbradley.net> wrote:
| public T doSomething<T where T:new()>() Yes you can define a generic function where the parameter is only used for
the return type.

However you need to supply the type parameter when you call the method,
something like:

| object o = doSomething<object>();
| MemoryStream m = doSomething<MemoryStream>();
Oh good. It's always a little bit freaky when it "just works" in Java.
However! it "violates" an FxCop rule as its "ambiguous".

Here is a thread that discusses it:

http://groups.google.com/group/micro...languages.csha
rp/browse_frm/thread/c96debb32de857b1/cf4e46ca8f99b798#cf4e46ca8f99b79
8

Personally I find in the case of GetCustomAttribute (as the thread shows) it
makes sense as the type parameter is encapsulating the downcast, plus the
type parameter is used to "do work".


Right. Okay, I think all that makes sense - thanks very much :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

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

Similar topics

13
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface...
4
by: James Radke | last post by:
Hello, Can someone explain to me why the return value is not part of what makes up the signature of a function? I would love to be able to overload a function such as: public overloads...
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
8
by: David Veeneman | last post by:
Should a member variable be passed to a private method in the same class as a method argument, or should the method simply call the member variable? For years, I have passed member variables to...
5
by: Dom | last post by:
is there a difference between the following: Rectangle r = new Rectangle (10,20,30,40); Rectangle r = Rectangle.FromLTRB(10,20,30,40); Also, I guess I'm not sure why Structs are created with a...
4
by: Bob Cramer | last post by:
I don't have a copy of Reflector handy :-( so I'd appreciate if someone could confirm (or clarify) the following. In consideration of the following delegate declaration... delegate string...
6
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is,...
4
by: jmDesktop | last post by:
In the code below from MSDN How do the PeopleEnum methods ever get called? foreach (Person p in peopleList) Console.WriteLine(p.firstName + " " + p.lastName); What is going on behind the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...

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.