471,594 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Overloaded Functions with same header ?

Hello,

Is it possible in C# to have 2 overloaded functions with

- same names
- same parameters
- different return type values

If no, is it possible in another language ?
Regards,
Cybertof.
Nov 15 '05 #1
3 2129
cybertof <cy****************@gmx.net> wrote:
Is it possible in C# to have 2 overloaded functions with

- same names
- same parameters
- different return type values
No.
If no, is it possible in another language ?


Not if it wants to remain CLS-compliant. The spec is quite interesting
on this point. Quoting from section 10.2 of partition I:

<quote>
Note: The CTS, while it describes inheritance, object layout, name
hiding, and overriding of virtual methods, does not discuss overloading
at all. While this is surprising, it arises from the fact that
overloading is entirely handled by compilers that target the CTS and
not the type system itself. In the metadata, all references to types
and type members are fully resolved and include the precise signature
that is intended. This choice was made since every programming language
has its own set of rules for coercing types and the VES does not
provide a means for expressing those rules.

Following the rules of the CTS, it is possible for duplicate names to
be defined in the same scope as long as they differ in either kind
(field, method, etc.) or signature. The CLS imposes a stronger
restriction for overloading methods. Within a single scope, a given
name may refer to any number of methods provided they differ in any of
the following:

o Number of parameters
o Type of each argument

Notice that the signature includes more information but CLS-compliant
languages need not produce or consume classes that differ only by that
additional information (see Partition II for the complete list of
information carried in a signature):

o Calling convention
o Custom modifiers
o Return type
o Whether a parameter is passed by value or by reference (i.e. as a
managed pointer or by-ref)

There is one exception to this rule. For the special names op_Implicit
and op_Explicit described in clause 10.3.3 methods may be provided that
differ only by their return type. These are marked specially and may be
ignored by compilers that don't support operator overloading.

Properties shall not be overloaded by type (that is, by the return type
of their getter method), but they may be overloaded with different
number or types of indices (that is, by the number and types of the
parameters of its getter method). The overloading rules for properties
are identical to the method overloading rules.

CLS Rule 37: Only properties and methods may be overloaded.

CLS Rule 38: Properties, instance methods, and virtual methods may be
overloaded based only on the number and types of their parameters,
except the conversion operators named op_Implicit and op_Explicit which
may also be overloaded based on their return type.

Note:
CLS (consumer): May assume that only properties and methods are
overloaded, and need not support overloading based on return type
unless providing special syntax for operator overloading. If return
type overloading isnt supported, then the op_Implicit and op_Explicit
may be ignored since the functionality shall be provided in some other
way by a CLS-compliant framework.

CLS (extender): Should not permit the authoring of overloads other than
those specified here. It is not necessary to support operator
overloading at all, hence it is possible to entirely avoid support for
overloading on return type.

CLS (framework): Shall not publicly expose overloading except as
specified here. Frameworks authors should bear in mind that many
programming languages, including Object- Oriented languages, do not
support overloading and will expose overloaded methods or properties
through mangled names. Most languages support neither operator
overloading nor overloading based on return type, so op_Implicit and
op_Explicit shall always be augmented with some alternative way to gain
the same functionality.

</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi cybertof,

No it is not possible.
Maybe some languages provide such a overloading, I don't know any of them,
though.
This is not a technical issue. Compilers knows everything for the method you
want to override (at compile time), so it is not a problem to offer overring
on return value's type only. But this is considered as error prone and is
excluded as an option by design.

For example take a look at C++.
The C++ compilers decorate internaly the names of all methods with coded
information about the class the method is decalred in (if any), all of their
parameters' type and the type of the returning value. However, what
decoration should be used is not standardized and is left to the compiler's
vendors to choose their own. This is one of the main reason for c++ library
and obj file incompatibility.

I gave you that example just to back up my assertion here that this has
never been a technical issue. It is rather a language design issue and I
believe there is no language among the most popular ones that offers this
functionality.
Of course I might be wrong.

---
B\rgds
100

"cybertof" <cy****************@gmx.net> wrote in message
news:MP***********************@msnews.microsoft.co m...
Hello,

Is it possible in C# to have 2 overloaded functions with

- same names
- same parameters
- different return type values

If no, is it possible in another language ?
Regards,
Cybertof.

Nov 15 '05 #3
Thanks for your answers Jon & Stoitcho.

Regards,
Cybertof.
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

44 posts views Thread by bahadir.balban | last post: by
1 post views Thread by prakash.mirji | last post: by
2 posts views Thread by desktop | last post: by
4 posts views Thread by king kikapu | last post: by
9 posts views Thread by Lawrence Krubner | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.