开发者

Explaining codes

Hi can anyone explain these lines of codes, I need to understand how it works in order to proceed with what I am doing

if (e.Error == null){
    Stream responseStream = e.Result;
    StreamReader responseReader = new StreamRe开发者_如何转开发ader(responseStream);
    string response = responseReader.ReadToEnd();

    string[] split1 = Regex.Split(response, "},{");
    List<string> pri1 = new List<string>(split1);
    pri1.RemoveAt(0);
    string last = pri1[pri1.Count() - 1];
    pri1.Remove(last);
}


// Check if there was no error
    if (e.Error == null)
    {

// Streams are a way to read/write information from/to somewhere
// without having to manage buffer allocation and such
        Stream responseStream = e.Result;

// StreamReader is a class making it easier to read from a stream
        StreamReader responseReader = new StreamReader(responseStream);

// read everything that was written to a stream and convert it to a string using
// the character encoding that was specified for the stream/reader.
        string response = responseReader.ReadToEnd();

// create an array of the string by using "},{" as delimiter
// string.Split would be more efficient and more straightforward.
        string[] split1 = Regex.Split(response, "},{");

// create a list of the array. Lists makes it easier to work with arrays
// since you do not have to move elements manually or take care of allocations
        List<string> pri1 = new List<string>(split1);
        pri1.RemoveAt(0);

// get the last item in the array. It would be more efficient to use .Length instead
// of Count()
        string last = pri1[pri1.Count() - 1];

// remove the last item
        pri1.Remove(last);
     }

I would use a LinkedList instead of List if the only thing to do was to remove the first and last elements.


It's reading the response stream as a string, making the assumption that the string consists of sequences "{...}" separated by commas, e.g.:

{X},{Y},{Z}

then splits the string on "},{", giving an array of

{X

Y

Z}

then removes the first brace from the first element of the array ( {X => X ) and the end brace from the last element of the array ( Z} => Z).


From what I can see, it is reading from a stream that could have came from TCP. It reads the whole chunk of data, then separate the chunk using the delimiter },{.

So if you have something like abc},{dec , it will be placed into split1 array with 2 values, split1 [0]=abc , split1 [1]=dec.

After that, it basically remove the 1st and the last content


It is processing an error output. It received a stream from the e (I guess it is an exception), reads it. It looks something like : ""{DDD},{I failed},{Because},{There was no signal}{ENDCODE} It splits it into different string, and removes to fist and last entries (DDD, ENDCODE)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜