473,699 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1396
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*******@gmai l.com> escribió en el mensaje
news:11******** *************@g 49g2000cwa.goog legroups.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 GenericDictiona ryCollection<T>
: System.Collecti ons.Generic.Dic tionary<String, T>
{

/// <summary>
/// Overridable default Item method
/// </summary>
/// <param name="key">key of Item</param>
/// <returns>Valu e 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.co m> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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***@DIESPAMM ERSDIEtakempis. 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.co m>
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<obj ect>();
| MemoryStream m = doSomething<Mem oryStream>();

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 GetCustomAttrib ute (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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
| Kevin Spencer <ke***@DIESPAMM ERSDIEtakempis. 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.co m>
| 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<obj ect>();
| MemoryStream m = doSomething<Mem oryStream>();
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/c96debb32de857b 1/cf4e46ca8f99b79 8#cf4e46ca8f99b 79
8

Personally I find in the case of GetCustomAttrib ute (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.co m>
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
3255
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 includes. My question is, if you must declare each method (as opposed to simply 'using' those methods like an #included file), then why not just declare the methods on your own as if you created them yourself? What advantage does using an interface...
4
1657
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 function a(x as string, y as string) as DataReader and
42
3430
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: ---------------------------------------------------- TypeError: addFile() takes exactly 1 argument (2 given) ----------------------------------------------------
10
3368
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. Compiler: gcc 4.1, Linux Thanks,
8
5737
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 private methods in the same class as method arguments. For example, if Public method Foo() calls private method Bar(), and if Bar() uses member variable m_MyVariable, I declare the private method with the signature: private void Bar(SomeType...
5
2209
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 NEW keyword, anyway. Aren't they located on the stack, like values, and isn't NEW used to create space on the heap? Dom
4
1785
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 MyLovelyDelegate(int parm1, string parm2); 1. Is it true that, in the output assembly, a new class will be created that inherits from System.MulticastDelegate - and the name of that class is
6
1400
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, it's essentially a pointer to a piece of code somewhere else in memory. It therefore (to me anyway) makes sense to define delegates with their signatures and be able to use those signatures at different points in
4
1734
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 scenes in the foreach? Also, I could not find where the interface signatures were for the
0
8687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9174
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
9035
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...
1
8914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
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
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
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
2347
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.