Asp example: FileUpload.FileBytes on MSDN, confusing

I was only going to use the FileUpload.FileBytes property, but after looking at the corresponding example in the MSDN library, I got confused in this part:

int fileLen; // Get the length of the file. fileLen = FileUpload1.PostedFile.ContentLength; // Create a byte array to hold the contents of the file. byte[] input = new byte[fileLen - 1]; input = FileUpload1.FileBytes; 

(from here , I skipped a few unimportant lines)

new in this code is looking for me as food for the garbage collector without use. (Perhaps a big meal if the file is large.) Why not just write:

 byte[] input = FileUpload1.FileBytes; 

I'm too new to .NET and C # to be brave enough to declare this to be just an unnecessary or bad written example. Does it have any goals (perhaps a performance advantage or so)? (Also I don't understand why they subtract 1 from fileLen .)

+4
source share
1 answer

MSDN code samples vary in quality, and this is a good example of a bad coding example.

As you say, it’s easier to just write:

 byte[] input = FileUpload1.FileBytes; 

And there is an error with the length of the byte array - there is no reason to subtract 1 from the published length.

+2
source

Source: https://habr.com/ru/post/1302791/


All Articles