方法重载和引用 [英] method overloading and references

查看:49
本文介绍了方法重载和引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我刚刚开始使用C#而我正在试验方法

重载。

似乎它不是可以使用返回类型覆盖方法,只有

参数。这是设计的,无论如何围绕那个或什么?即我想

喜欢使用相同的方法返回DataReader或DataAdapter

,具体取决于调用它时的左值。


另一个问题。如果我的方法返回一个值,是否可以通过引用返回

或者总是这样,总是复制对象?


无论如何,这一切都沸腾了事实上,我的班级将拥有

应该为我检索数据库结果的方法,我希望他们能够

为我提供DataAdapters(与组件轻松集成)

和DataReaders(如果我想在循环中使用结果而不是

组件)。我希望这样做尽可能简单和简单没有

有任何重复的代码,3倍的方法或类似的东西和

事实上它必须是最佳表现 - wize。

解决方案

Daniel Gustafsson< da **** @ phosworks.se>写道:

你好,嗯,我刚开始使用C#而我正在试验方法
重载。
好像不可能使用返回类型覆盖方法,只有
参数。这是设计的,无论如何围绕那个或什么?


这是设计 - 你怎么能在

客户端区分它们?例如:


MyMethod();


可以返回一个值,但我没有*使用它 - 这个版本

会打电话吗?

即我想
喜欢使用相同的方法返回DataReader或DataAdapter
,这取决于调用它时的左值。


说实话,这对我来说是一个相当混乱的界面。

另一个问题。如果我的方法返回一个值,它可以通过引用返回
它还是总是,总是复制对象?


它不会通过引用返回 - 它返回* a *引用。你需要明白参考类型之间的区别

和价值类型是什么。


如果你想做一个对象的副本,你应该在方法中做那个

,并返回对副本的引用。

无论如何,这一切归结为我的课程这一事实将有一些方法可以为我检索数据库结果,我希望它们能够为我提供DataAdapter(以便与组件轻松集成)和DataReaders(在case我想在循环中使用结果而不是
组件。我希望尽可能无缝和简单地执行此操作,而不会有任何重复的代码,3倍的方法或类似的东西和
它必须是最佳性能wize的事实。



嗯,你可以提供另一个参数来说明返回什么类型的引用

,并将其声明为返回object,将其保留给施放它的

来电者。尽管如此,这并不像有一个强大的

类型的返回值。我建议要么有不同的API

类,要么*可能*(我也不喜欢这个)具有

两个接口,其中一个描述了返回一个DataReader和一个描述返回DataAdapter的
。然后使用显式接口

实现来区分两者,并让

客户端将其初始引用转换为两个接口之一

取决于他们想做什么。


-

Jon Skeet - < sk *** @ pobox.com>
< a rel =nofollowhref =http://www.pobox.com/~skeet/target =_ blank> http://www.pobox.com/~skeet/

如果回复该团体,请不要给我发邮件


Daniel Gustafsson< da **** @ phosworks.se>写道:

你好,嗯,我刚开始使用C#而我正在试验方法
重载。
好像不可能使用返回类型覆盖方法,只有
参数。这是设计的,无论如何围绕那个或什么?


这是设计 - 你怎么能在

客户端区分它们?例如:


MyMethod();


可以返回一个值,但我没有*使用它 - 这个版本

会打电话吗?

即我想
喜欢使用相同的方法返回DataReader或DataAdapter
,这取决于调用它时的左值。


说实话,这对我来说是一个相当混乱的界面。

另一个问题。如果我的方法返回一个值,它可以通过引用返回
它还是总是,总是复制对象?


它不会通过引用返回 - 它返回* a *引用。你需要明白参考类型之间的区别

和价值类型是什么。


如果你想做一个对象的副本,你应该在方法中做那个

,并返回对副本的引用。

无论如何,这一切归结为我的课程这一事实将有一些方法可以为我检索数据库结果,我希望它们能够为我提供DataAdapter(以便与组件轻松集成)和DataReaders(在case我想在循环中使用结果而不是
组件。我希望尽可能无缝和简单地执行此操作,而不会有任何重复的代码,3倍的方法或类似的东西和
它必须是最佳性能wize的事实。



嗯,你可以提供另一个参数来说明返回什么类型的引用

,并将其声明为返回object,将其保留给施放它的

来电者。尽管如此,这并不像有一个强大的

类型的返回值。我建议要么有不同的API

类,要么*可能*(我也不喜欢这个)具有

两个接口,其中一个描述了返回一个DataReader和一个描述返回DataAdapter的
。然后使用显式接口

实现来区分两者,并让

客户端将其初始引用转换为两个接口之一

取决于他们想做什么。


-

Jon Skeet - < sk *** @ pobox.com>
< a rel =nofollowhref =http://www.pobox.com/~skeet/target =_ blank> http://www.pobox.com/~skeet/

如果回复小组,请不要给我发邮件


>似乎不可能使用返回类型覆盖方法,

只有

覆盖方法必须具有相同的签名,这意味着甚至返回类型

你打算超载吗?

即我希望使用相同的方法返回DataReader或
DataAdapter,具体取决于调用它时的左值。
左值?左值?

公共类A

{

公共DataReader YourMethod()

{

...

}

公共DataAdatpter YourMethod()

{

...

}

}

DataReader dr = YourMethod(); //这样???

左边值是可选的,所以我们如何区分2种方法?


还有另一个问题。如果我的方法返回一个值,是否可以通过引用返回它或者总是这样,总是复制对象?



如果返回类型是引用类型它是隐含的引用,而不是副本


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.

解决方案

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


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


> 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


这篇关于方法重载和引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆