Question on Method Signatures

  • Thread starter Thread starter Robert McCall
  • Start date Start date
R

Robert McCall

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
 
Robert said:
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
 
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
 
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
 
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.
 
Kevin Spencer said:
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,
| 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/micr...bb32de857b1/cf4e46ca8f99b798#cf4e46ca8f99b798

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


| > 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 - <[email protected]>
| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too
 
Jay B. Harlow said:
| 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/microsoft.public.dotnet.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 :)
 
Back
Top