更多关于using语句的信息 [英] more about the using statement

查看:50
本文介绍了更多关于using语句的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


我只是想知道读取器变量在下面的代码片段中声明为

TextReader是什么意思..

是不是因为在读者变量上使用了多态性。


使用(TextReader reader = new StreamReader(fullPathname))

{

字符串行;

while((line = reader.ReadLine())!= null)

{

source.Text + = line +" \\\
";

}

}


// Tony

解决方案

2008年1月16日星期三10:33:24 -0800,Tony Johansson

< jo * ****************@telia.comwrote:


您好!


我只是想知道将读取器变量声明为以下片段中的文本读取器是什么意思?

是否因为在读取器变量上使用了多态性。



在你给出的例子中,确实没有任何区别。引用

只存储在一个变量中,而且该变量可能就像
一样很容易成为StreamReader。


但是由于StreamReader和StringReader只是简单地实现了抽象的

TextReader类而没有添加任何新东西,所以将它们用作

TextReaders是很好的。同样的事情也适用于任何其他类可能来自TextReader的
。这样代码更通用,而且
不依赖于TextReader的特定实现。当然,

通常这是将局部变量传递给采用TextReader的某些

方法的问题,当然无论是否或

不是变量被明确声明为TextReader而不是

StreamReader。


显然在某些时候你需要明确哪个class是

创建的,但这只需要在一个特定的地方完成,其余的

代码可以通过使用TextReader更加可重用。


在某种程度上,将局部变量声明为TextReader只会使得

更清楚,using语句中的代码不是$

依赖读者是StreamReader。在这里,它可能是一个代码可读性问题。


Pete


< blockquote> StreamReader是一个类型。 TextReader,这就是代码工作的原因。如果

你问我水果,我给你传了一个苹果,没关系,因为一个苹果是b $ b的类型。水果这在工作中不是多态,而是简单的

继承概念。


话虽如此,因为StreamReader是一个更具体的类型 ;你可能不想保留这行代码。

你想要将读者声明为StreamReader,这样您可以在使用块中充分利用StreamReader的成员的优势。就像现在一样,

你只能在

使用块中使用TextReader的更通用的成员,即使你确实有一个StreamReader记忆。


-Scott

" Tony Johansson" < jo ***************** @ telia.comwrote in message

新闻:U5 ************* **** @ newsb.telia.net ...


你好!


我只是想知道有什么意义将读取器变量声明为

下面的代码片段中的TextReader ..

是否因为在读者变量上使用了多态性。

使用(TextReader reader = new StreamReader(fullPathname))

{

string line;

while((line = reader) .ReadLine())!= null)

{

source.Text + = line +" \ n" ;;

} < br $>
}


// Tony






在那个句子中使用另一个含义,它基本上做了两件事,首先是

它为变量阅读器创建了一个范围,第二个更重要的是它

电话在范围的末尾处理。

在这种结构中你不必关闭阅读器。因为Dispose

方法会为你做,即使在范围内抛出异常。


当然变量被使用了必须实施IDisposable


-

Ignacio Machin
http://www.laceupsolutions.com

Mobile&仓库解决方案。

" Tony Johansson" < jo ***************** @ telia.comwrote in message

新闻:U5 ************* **** @ newsb.telia.net ...


你好!


我只是想知道有什么意义将读取器变量声明为

下面的代码片段中的TextReader ..

是否因为在读者变量上使用了多态性。

使用(TextReader reader = new StreamReader(fullPathname))

{

string line;

while((line = reader) .ReadLine())!= null)

{

source.Text + = line +" \ n" ;;

} < br $>
}


// Tony



Hello!

I just wonder what is the point of having the reader variable declared as
TextReader in the snippet below..
Is it because of using the polymorfism on the reader variable perhaps.

using (TextReader reader = new StreamReader(fullPathname))
{
string line;
while ((line = reader.ReadLine()) != null)
{
source.Text += line + "\n";
}
}

//Tony

解决方案

On Wed, 16 Jan 2008 10:33:24 -0800, Tony Johansson
<jo*****************@telia.comwrote:

Hello!

I just wonder what is the point of having the reader variable declared as
TextReader in the snippet below..
Is it because of using the polymorfism on the reader variable perhaps.

In the example you give, there''s really not any difference. The reference
only ever is stored in one variable, and that variable could just as
easily have been a StreamReader.

But since StreamReader and StringReader simply implement the abstract
TextReader class without adding anything new, it''s nice to use them as
TextReaders. The same thing would apply to any other class someone might
derive from TextReader. That way the code is more general-purpose and
doesn''t depend on a specific implementation of TextReader. Of course,
usually this would be a matter of passing the local variable to some
method that takes a TextReader, and of course that can be done whether or
not the variable is declared explicitly as just a TextReader versus a
StreamReader.

Obviously at some point you need to be explicit about which class is
created, but this only needs to be done in one specific place and the rest
of the code can be more reusable by using TextReader instead.

To some extent, declaring the local variable as a TextReader just makes it
that much more clear that the code within the using statement isn''t
dependent at all on the reader being a StreamReader. Here, it''s likely an
issue of code readability as anything else.

Pete


A StreamReader is a "type" of TextReader and that''s why the code works. If
you ask me for fruit and I pass you an apple, it is ok, because an apple is
a "type" of fruit. This isn''t quite polymorphism at work here, but simple
inheritance concepts.

Having said that, since a StreamReader is a more specific "type" than a
TextReader, you probably wouldn''t want to keep this line of code as it is.
You''d want to declare reader as a StreamReader so that you can take full
advantage of a StreamReader''s members in the using block. As it is now,
you''d only be able to use the more generic members of a TextReader in the
using block even though you really do have a StreamReader in memory.

-Scott
"Tony Johansson" <jo*****************@telia.comwrote in message
news:U5*****************@newsb.telia.net...

Hello!

I just wonder what is the point of having the reader variable declared as
TextReader in the snippet below..
Is it because of using the polymorfism on the reader variable perhaps.

using (TextReader reader = new StreamReader(fullPathname))
{
string line;
while ((line = reader.ReadLine()) != null)
{
source.Text += line + "\n";
}
}

//Tony



Hi,

In that sentence using as another meaning, it basically do two things, first
it create a scope for the variable reader and second and more importantly it
call Dispose at the end of the scope.
In this construction you do not have to close the Reader. as the Dispose
method will do it for you, even if an exception is throw inside the scope.

Of course the variable being "used" must implement IDisposable

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Tony Johansson" <jo*****************@telia.comwrote in message
news:U5*****************@newsb.telia.net...

Hello!

I just wonder what is the point of having the reader variable declared as
TextReader in the snippet below..
Is it because of using the polymorfism on the reader variable perhaps.

using (TextReader reader = new StreamReader(fullPathname))
{
string line;
while ((line = reader.ReadLine()) != null)
{
source.Text += line + "\n";
}
}

//Tony



这篇关于更多关于using语句的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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