473,241 Members | 1,607 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,241 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 2218
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
12
by: Jack Daly | last post by:
I've inherited some code which uses an undocumented feature of a third-party vendor's library. Essentially, this vendor has kept the details of an interface struct secret, but we can pass a pointer...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
1
by: prakash.mirji | last post by:
Hello, I am trying to compile C++ code which uses rogue wave 9.0 classes on RHEL 4.0. I use gnu g++ compiler to compile the code. Compiler is not able to match the right type of parameters to...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
4
by: king kikapu | last post by:
Hi, i am trying, to no avail yet, to take a C#'s overloaded functions skeleton and rewrite it in Python by using closures. I read somewhere on the net (http://dirtsimple.org/2004/12/python-is-...
3
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
9
by: Lawrence Krubner | last post by:
Possibly a dumb question but is type hinting allowed with overloaded methods? Can one class have these two methods? public function doSelect(Criteria $c) { // some code here } public...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.