问题描述
我试图让我周围的一些我的前辈代码谁,有益,采用无功申报一切头。
I am trying to get my head around some of my predecessors code who, helpfully, has used 'var' to declare everything.
我有一个使用声明,是如下:
I have a using statement which is below:
using (var postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
当我把这里断点,postStream显示了在汽车窗口系统.Net.ConnectStream。而不是变种我想用'ConnectStream,但编译器不喜欢这样。
When I put a breakpoint here, postStream shows up in the Autos window as System.Net.ConnectStream. Instead of 'var' I want to use 'ConnectStream' but the compiler doesn't like this.
我在想什么,为什么我不能写我喜欢的代码这样的:
What am I missing, why can't I write my code like this:
using (ConnectStream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
我知道这是微不足道,但我总是被告知不要使用无功除非你有特殊原因,这样做(例如使用LINQ处理时)。难道我错了吗?
I know this is trivial but I was always taught not to use 'var' unless you have a specific reason to do so (such as when dealing with LINQ). Am I wrong?
感谢
推荐答案
ConnectStream
是一个内部类,你可以没有明确使用它。但是没关系,因为你不需要知道它的实际类型是 ConnectStream
:所有你需要知道的是,这是一个流
,实际执行并不重要。
ConnectStream
is an internal class, you can't use it explicitly. But it doesn't matter, because you don't need to know that its actual type is ConnectStream
: all you need to know is that it's a Stream
(the return type declared by GetRequestStream
), the actual implementation doesn't really matter.
如果你希望明确指定类型,只写这样的:
If you want to specify the type explicitly, just write it like this:
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
(但它有确切的含义与使用相同的 VAR
)
这篇关于如何使用System.Net.ConnectStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!