473,385 Members | 1,834 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,385 software developers and data experts.

method overloading and references

Hi there, hmm, I've just started with C# and I'm experimenting with method
overloading.
It seems like it's not possible to override method using return types, only
parameters. Is that by design, anyway around that or something? I.e. I'd
like to have the same method return either a DataReader or a DataAdapter
depending on the lvalue when calling it.

And another question. If I have my method returning a value, can it return
it by reference or does it always, always copy the object?

Anyhow, it all boils down to the fact that my class will have methods that
are supposed to retrieve database results for me, and I want them to be able
to provide me both with DataAdapters (for easy integration with components)
and DataReaders (in the case I want to use the result in a loop instead of a
component). I want to do this as seemless and simple as possible without
having any duplication of code, 3x as many methods or stuff like that AND
the fact that it has to be optimal performance-wize.
Nov 15 '05 #1
18 4070
Daniel Gustafsson <da****@phosworks.se> wrote:
Hi there, hmm, I've just started with C# and I'm experimenting with method
overloading.
It seems like it's not possible to override method using return types, only
parameters. Is that by design, anyway around that or something?
It's by design - how would you be able to distinguish them at the
client side? For instance:

MyMethod();

could return a value, but I don't *have* to use it - which version
would that call?
I.e. I'd
like to have the same method return either a DataReader or a DataAdapter
depending on the lvalue when calling it.
That strikes me as a fairly confusing interface, to be honest.
And another question. If I have my method returning a value, can it return
it by reference or does it always, always copy the object?
It doesn't "return it by reference" - it returns *a* reference. You
need to understand exactly what the difference between reference types
and value types are.

If you want to make a copy of the object, you should probably do that
in the method, and return a reference to the copy.
Anyhow, it all boils down to the fact that my class will have methods that
are supposed to retrieve database results for me, and I want them to be able
to provide me both with DataAdapters (for easy integration with components)
and DataReaders (in the case I want to use the result in a loop instead of a
component). I want to do this as seemless and simple as possible without
having any duplication of code, 3x as many methods or stuff like that AND
the fact that it has to be optimal performance-wize.


Well, you could provide another parameter to say what type of reference
to return, and declare it as returning "object", leaving it to the
caller to cast it. That doesn't seem as nice a way as having a strongly
typed return value though. I'd suggest either having different API
classes, or *possibly* (and I don't like this particularly either) have
two interfaces, one of which describes returning a DataReader and one
of which describes returning a DataAdapter. Then use explicit interface
implementation to tell the difference between the two, and let the
client cast their initial reference to one of the two interfaces
depending on what they want to do.

--
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
Daniel Gustafsson <da****@phosworks.se> wrote:
Hi there, hmm, I've just started with C# and I'm experimenting with method
overloading.
It seems like it's not possible to override method using return types, only
parameters. Is that by design, anyway around that or something?
It's by design - how would you be able to distinguish them at the
client side? For instance:

MyMethod();

could return a value, but I don't *have* to use it - which version
would that call?
I.e. I'd
like to have the same method return either a DataReader or a DataAdapter
depending on the lvalue when calling it.
That strikes me as a fairly confusing interface, to be honest.
And another question. If I have my method returning a value, can it return
it by reference or does it always, always copy the object?
It doesn't "return it by reference" - it returns *a* reference. You
need to understand exactly what the difference between reference types
and value types are.

If you want to make a copy of the object, you should probably do that
in the method, and return a reference to the copy.
Anyhow, it all boils down to the fact that my class will have methods that
are supposed to retrieve database results for me, and I want them to be able
to provide me both with DataAdapters (for easy integration with components)
and DataReaders (in the case I want to use the result in a loop instead of a
component). I want to do this as seemless and simple as possible without
having any duplication of code, 3x as many methods or stuff like that AND
the fact that it has to be optimal performance-wize.


Well, you could provide another parameter to say what type of reference
to return, and declare it as returning "object", leaving it to the
caller to cast it. That doesn't seem as nice a way as having a strongly
typed return value though. I'd suggest either having different API
classes, or *possibly* (and I don't like this particularly either) have
two interfaces, one of which describes returning a DataReader and one
of which describes returning a DataAdapter. Then use explicit interface
implementation to tell the difference between the two, and let the
client cast their initial reference to one of the two interfaces
depending on what they want to do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
> It seems like it's not possible to override method using return types,
only
Override methods must have same signature, that means even return type
Did you intend overload?
I.e. I'd like to have the same method return either a DataReader or a DataAdapter depending on the lvalue when calling it. lvalue?? Left Value?
public Class A
{
public DataReader YourMethod()
{
...
}
public DataAdatpter YourMethod()
{
...
}
}
DataReader dr = YourMethod( ); // This way???
Left Value is optional so how can we distinguish 2 methods?

And another question. If I have my method returning a value, can it return
it by reference or does it always, always copy the object?

if return type is a reference type it is implicit a reference, not a copy
Nov 15 '05 #4
> It seems like it's not possible to override method using return types,
only
Override methods must have same signature, that means even return type
Did you intend overload?
I.e. I'd like to have the same method return either a DataReader or a DataAdapter depending on the lvalue when calling it. lvalue?? Left Value?
public Class A
{
public DataReader YourMethod()
{
...
}
public DataAdatpter YourMethod()
{
...
}
}
DataReader dr = YourMethod( ); // This way???
Left Value is optional so how can we distinguish 2 methods?

And another question. If I have my method returning a value, can it return
it by reference or does it always, always copy the object?

if return type is a reference type it is implicit a reference, not a copy
Nov 15 '05 #5

"Christian" <pl***********************@novasoftware.it> wrote in message
news:O3*************@TK2MSFTNGP10.phx.gbl...
DataReader dr = YourMethod( ); // This way???
Left Value is optional so how can we distinguish 2 methods?

Yeah, I figured that was the case why it didn't work.

And another question. If I have my method returning a value, can it return it by reference or does it always, always copy the object?

if return type is a reference type it is implicit a reference, not a copy


So I don't have to worry about returning DataAdapters/DataReaders will give
me a performance hit compared to having an out-parameter?
Nov 15 '05 #6

"Christian" <pl***********************@novasoftware.it> wrote in message
news:O3*************@TK2MSFTNGP10.phx.gbl...
DataReader dr = YourMethod( ); // This way???
Left Value is optional so how can we distinguish 2 methods?

Yeah, I figured that was the case why it didn't work.

And another question. If I have my method returning a value, can it return it by reference or does it always, always copy the object?

if return type is a reference type it is implicit a reference, not a copy


So I don't have to worry about returning DataAdapters/DataReaders will give
me a performance hit compared to having an out-parameter?
Nov 15 '05 #7

So I don't have to worry about returning DataAdapters/DataReaders will give me a performance hit compared to having an out-parameter?

No, returning a reference type shoul mean return 4 bytes indicating where
the object is located in the HEAP
Nov 15 '05 #8

So I don't have to worry about returning DataAdapters/DataReaders will give me a performance hit compared to having an out-parameter?

No, returning a reference type shoul mean return 4 bytes indicating where
the object is located in the HEAP
Nov 15 '05 #9

"Christian" <pl***********************@novasoftware.it> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...

So I don't have to worry about returning DataAdapters/DataReaders will

give
me a performance hit compared to having an out-parameter?

No, returning a reference type shoul mean return 4 bytes indicating where
the object is located in the HEAP


Ok, but afaik structures are value types, there is no way to return a
reference rather than getting it copied? the "ref" keyword only works for
parameters afaik. in c++ I can easily , and transparently, have my
functions return a reference to a value type.
Nov 15 '05 #10

"Christian" <pl***********************@novasoftware.it> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...

So I don't have to worry about returning DataAdapters/DataReaders will

give
me a performance hit compared to having an out-parameter?

No, returning a reference type shoul mean return 4 bytes indicating where
the object is located in the HEAP


Ok, but afaik structures are value types, there is no way to return a
reference rather than getting it copied? the "ref" keyword only works for
parameters afaik. in c++ I can easily , and transparently, have my
functions return a reference to a value type.
Nov 15 '05 #11
As struct are value types if you want a method return a reference to it you
should box it in an object,
but which are the reason for doing such an odd thing?
"Daniel Gustafsson" <da****@phosworks.se> ha scritto nel messaggio
news:e8**************@tk2msftngp13.phx.gbl...

"Christian" <pl***********************@novasoftware.it> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...

So I don't have to worry about returning DataAdapters/DataReaders will

give
me a performance hit compared to having an out-parameter?

No, returning a reference type shoul mean return 4 bytes indicating where the object is located in the HEAP


Ok, but afaik structures are value types, there is no way to return a
reference rather than getting it copied? the "ref" keyword only works for
parameters afaik. in c++ I can easily , and transparently, have my
functions return a reference to a value type.

Nov 15 '05 #12
As struct are value types if you want a method return a reference to it you
should box it in an object,
but which are the reason for doing such an odd thing?
"Daniel Gustafsson" <da****@phosworks.se> ha scritto nel messaggio
news:e8**************@tk2msftngp13.phx.gbl...

"Christian" <pl***********************@novasoftware.it> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...

So I don't have to worry about returning DataAdapters/DataReaders will

give
me a performance hit compared to having an out-parameter?

No, returning a reference type shoul mean return 4 bytes indicating where the object is located in the HEAP


Ok, but afaik structures are value types, there is no way to return a
reference rather than getting it copied? the "ref" keyword only works for
parameters afaik. in c++ I can easily , and transparently, have my
functions return a reference to a value type.

Nov 15 '05 #13

"Christian" <pl***********************@novasoftware.it> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
As struct are value types if you want a method return a reference to it you should box it in an object,
but which are the reason for doing such an odd thing?


Well, I didn't mean DataAdapters earlier on but I meant DataSets (for
components) and I've explained why I wanted to be able to fetch both types
as simplistic as possible.

And the returning structs -question was just out of curiousity since I did
some experimentation and it didn't seem like I could return references the
way I can in c++. I'm trying to figure all this out in a really short
timespan.
Nov 15 '05 #14

"Christian" <pl***********************@novasoftware.it> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
As struct are value types if you want a method return a reference to it you should box it in an object,
but which are the reason for doing such an odd thing?


Well, I didn't mean DataAdapters earlier on but I meant DataSets (for
components) and I've explained why I wanted to be able to fetch both types
as simplistic as possible.

And the returning structs -question was just out of curiousity since I did
some experimentation and it didn't seem like I could return references the
way I can in c++. I'm trying to figure all this out in a really short
timespan.
Nov 15 '05 #15
Actually I believe the framework does allow method overloading based on
return type, it's just that none of the current .NET languages support that.

I'm willing to be told I'm wrong though....
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Daniel Gustafsson <da****@phosworks.se> wrote:
Hi there, hmm, I've just started with C# and I'm experimenting with method overloading.
It seems like it's not possible to override method using return types, only parameters. Is that by design, anyway around that or something?


It's by design - how would you be able to distinguish them at the
client side? For instance:

MyMethod();

could return a value, but I don't *have* to use it - which version
would that call?
I.e. I'd
like to have the same method return either a DataReader or a DataAdapter
depending on the lvalue when calling it.


That strikes me as a fairly confusing interface, to be honest.
And another question. If I have my method returning a value, can it return it by reference or does it always, always copy the object?


It doesn't "return it by reference" - it returns *a* reference. You
need to understand exactly what the difference between reference types
and value types are.

If you want to make a copy of the object, you should probably do that
in the method, and return a reference to the copy.
Anyhow, it all boils down to the fact that my class will have methods that are supposed to retrieve database results for me, and I want them to be able to provide me both with DataAdapters (for easy integration with components) and DataReaders (in the case I want to use the result in a loop instead of a component). I want to do this as seemless and simple as possible without
having any duplication of code, 3x as many methods or stuff like that AND the fact that it has to be optimal performance-wize.


Well, you could provide another parameter to say what type of reference
to return, and declare it as returning "object", leaving it to the
caller to cast it. That doesn't seem as nice a way as having a strongly
typed return value though. I'd suggest either having different API
classes, or *possibly* (and I don't like this particularly either) have
two interfaces, one of which describes returning a DataReader and one
of which describes returning a DataAdapter. Then use explicit interface
implementation to tell the difference between the two, and let the
client cast their initial reference to one of the two interfaces
depending on what they want to do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #16
Actually I believe the framework does allow method overloading based on
return type, it's just that none of the current .NET languages support that.

I'm willing to be told I'm wrong though....
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Daniel Gustafsson <da****@phosworks.se> wrote:
Hi there, hmm, I've just started with C# and I'm experimenting with method overloading.
It seems like it's not possible to override method using return types, only parameters. Is that by design, anyway around that or something?


It's by design - how would you be able to distinguish them at the
client side? For instance:

MyMethod();

could return a value, but I don't *have* to use it - which version
would that call?
I.e. I'd
like to have the same method return either a DataReader or a DataAdapter
depending on the lvalue when calling it.


That strikes me as a fairly confusing interface, to be honest.
And another question. If I have my method returning a value, can it return it by reference or does it always, always copy the object?


It doesn't "return it by reference" - it returns *a* reference. You
need to understand exactly what the difference between reference types
and value types are.

If you want to make a copy of the object, you should probably do that
in the method, and return a reference to the copy.
Anyhow, it all boils down to the fact that my class will have methods that are supposed to retrieve database results for me, and I want them to be able to provide me both with DataAdapters (for easy integration with components) and DataReaders (in the case I want to use the result in a loop instead of a component). I want to do this as seemless and simple as possible without
having any duplication of code, 3x as many methods or stuff like that AND the fact that it has to be optimal performance-wize.


Well, you could provide another parameter to say what type of reference
to return, and declare it as returning "object", leaving it to the
caller to cast it. That doesn't seem as nice a way as having a strongly
typed return value though. I'd suggest either having different API
classes, or *possibly* (and I don't like this particularly either) have
two interfaces, one of which describes returning a DataReader and one
of which describes returning a DataAdapter. Then use explicit interface
implementation to tell the difference between the two, and let the
client cast their initial reference to one of the two interfaces
depending on what they want to do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #17
Stu Smith <st*****@remove.digita.com> wrote:
Actually I believe the framework does allow method overloading based on
return type, it's just that none of the current .NET languages support that.

I'm willing to be told I'm wrong though....


Nope, you're right, which is a bit of a surprise to me.

I've just managed to edit some IL which demonstrates it working.

Personally I hope it never *is* supported by any .NET languages though
- it's really nasty, IMO :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #18
Stu Smith <st*****@remove.digita.com> wrote:
Actually I believe the framework does allow method overloading based on
return type, it's just that none of the current .NET languages support that.

I'm willing to be told I'm wrong though....


Nope, you're right, which is a bit of a surprise to me.

I've just managed to edit some IL which demonstrates it working.

Personally I hope it never *is* supported by any .NET languages though
- it's really nasty, IMO :(

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

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

Similar topics

13
by: matthias_k | last post by:
Hi, I've never thought about this before, but since you are able to overload a function only by changing the const-ness of the formal parameters, some annoying side effects will arise. For...
0
by: Daniel Gustafsson | last post by:
Hi there, hmm, I've just started with C# and I'm experimenting with method overloading. It seems like it's not possible to override method using return types, only parameters. Is that by design,...
4
by: Lenn | last post by:
I have the following method: public void Execute(TCPCommand command, out string outputResponse, out string error) { //snipped code outputResponse = "some message"; error = ""; }
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
0
by: Mikkel Blanné | last post by:
I haven't been able to find any references to using this combination of technologies (remoting + generic methods + method overloading). I don't think the problem has to do with C#, but I couldn't...
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
8
by: g.vukoman | last post by:
Hi all, I asking me how to design my method. Should I use pointer only? Or references? Or Values? I don't know... Below is a small method to read the mem amount of a machine. Fell free to...
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
by: David Boddie | last post by:
On Mon May 26 17:37:04 CEST 2008, Alex Gusarov wrote: Right. I vaguely remember someone showing something like this at EuroPython a couple of years ago. I believe that this approach is actually...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.