1

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);
}

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);
}

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?

8
  • 1
    var is your friend :) and also you don't have to put breakpoint there to know the type. Just hover over var and it will tell the type.
    – Beku
    Jan 19, 2012 at 16:53
  • @Beku, it will tell the declared type, not the actual type of the instance; ConnectStream is not a public class, so in this case it will only show as Stream (the declared returned type of GetRequestStream) Jan 19, 2012 at 16:56
  • @ThomasLevesque But stream is enough to remove the var, which was causing trouble to the reader of the code.
    – Beku
    Jan 19, 2012 at 16:58
  • Thankyou both, you learn something new every day!
    – JMK
    Jan 19, 2012 at 17:06
  • 1
    In Visual Studio you can see what type "var" is inferred to be by mousing over it. As long as you do that it's the same as typing it out, unless you intentionally assign to a supertype reference.
    – Hans
    Apr 22, 2014 at 19:45

3 Answers 3

10

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);
}

(but it has exactly the same meaning as using var)

6
  • 6
    +1 "because you don't need to know that it's actual type is" - "all you need to know is that it's a stream." Jan 20, 2012 at 13:13
  • 1
    @BobbyCannon I wish wish this were true. Unfortunately the System.Net.ConnectStream violates the Liskov Substitution Principle, so you can't treat it like a standard stream. Aug 26, 2013 at 20:23
  • @JimmyHoffa, how does it violate the LSP? Aug 26, 2013 at 20:56
  • @ThomasLevesque As with a variety of the Stream implementations, it throws NotImplementedException when you try to access the Length property, and one or two other properties. The whole family of the inheritance hierarchy for Stream are often guilty of these types of Liskov violations. Annoying. Aug 26, 2013 at 21:10
  • 3
    @JimmyHoffa, there is a CanSeek property that must be true to access the Length property, otherwise it throws NotSupportedException. This exception is documented in the Stream.Length documentation. ConnectStream does respect this contract, so I don't think it violates the LSP. Aug 27, 2013 at 0:51
2

Theres a great snippet from the var keyword on the InfoQ site. This talks about when and when not to use var. Its not quite as clear cut as don't' use it unless your using linq, its more you use it when you don't need to draw attention to the data type and use typed objects when you need to draw attention to the data type.

Its one of the personal preference things... but normally the best preference is however your boss/code lead/architect likes their code 'grammar' to look to make it uniform.

0

I reused one answer from here: How do I get the filesize from the Microsoft.SharePoint.Client.File object?

It' reply from 'Freejete' and his method 'ReadToEnd' worked like a charm for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.