I am a complete beginner in C#(I know better C)
looking at some examples I see that .Close() is used in the end for a binaryreader(never used binaryreader in C)
but if I try to use it then it cause errors if I use the same function twice.
It seems better simply not to close binaryreader and I wonder what is the advantage of closing it
Here is a simple example for a code that works only after I commented r.close out of the program
You can see that I never close r in the code and I wonder if this type of code
can cause problems.
Expand|Select|Wrap|Line Numbers
- using System;
- using System.IO;
- // Program start class
- class InteractiveWelcome
- {
- private const string FILE_NAME = "outputfile.bin";
- public int find_number(int place, FileStream fs)
- {
- int num;
- BinaryReader r = new BinaryReader(fs);
- fs.Seek(place * sizeof(Int32), SeekOrigin.Begin);
- num = r.ReadInt32();
- // r.Close();
- return num;
- }
- public static void Main(String[] args)
- {
- InteractiveWelcome fd=new InteractiveWelcome();
- FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
- Console.WriteLine(fd.find_number(0,fs));
- Console.WriteLine(fd.find_number(1,fs));
- fs.Close();
- }
- }