C# C# Streams and Data Processing Streaming Data on the Net WebClient

Daniel Hildreth
Daniel Hildreth
16,170 Points

Call read to end WebClient Challenge task 4

Hey I don't know what I'm doing with this code. I thought when you typed it out as return treehouse.ReadToEnd(); it called in the method and assigned it at the same time. That's what I took out of the video at least. Can someone help me with this?

Program.cs
using System;
using System.IO;
using System.Net;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetTreehouseHome());
        }

        public static string GetTreehouseHome()
        {
            string treehouse = "";
            using(var webClient = new WebClient())
            {
                byte[] treehouseBytes = webClient.DownloadData("https://www.teamtreehouse.com");
                using (var stream = new MemoryStream(treehouseBytes))
                using (var reader = new StreamReader(stream))
                { 
                    return treehouse.ReadToEnd();
                }
            } 

        }
    }
}

2 Answers

Steven Parker
Steven Parker
182,438 Points

:point_right: The variable "treehouse" is a string. It has no ReadToEnd method.

The final task instructions are "Call the ReadToEnd method on the on the reader object, and assign the result of the method to the provided treehouse variable." Following those instructions will produce this line:

                    treehouse = reader.ReadToEnd();

But it also looks like you removed a line that the challenge started with, you'll need to put it back:

            return treehouse;

`` using System; using System.IO; using System.Net;

namespace Treehouse.CodeChallenges { class Program { static void Main(string[] args) { Console.WriteLine(GetTreehouseHome()); }

    public static string GetTreehouseHome()
    {
        string treehouse = "";
        using(var webClient = new WebClient())
        {
            byte[] treehouseBytes = webClient.DownloadData("https://www.teamtreehouse.com");
            using (var stream = new MemoryStream(treehouseBytes))
            using (var reader = new StreamReader(stream))
            { 
                treehouse = reader.ReadToEnd();
            }
        } 
     return treehouse;
    }
}

} ``