Link to home
Start Free TrialLog in
Avatar of Kokas79
Kokas79

asked on

Creating a text file

Hello

Have a look please at the code below:

class Test
{
    public static void Main()
    {
        using (StreamWriter sw = new StreamWriter("TestFile.txt"))
        {
            // Add some text to the file.
            sw.Write("This is the ");
            sw.WriteLine("header for the file.");
            sw.WriteLine("-------------------");
            // Arbitrary objects can also be written to the file.
            sw.Write("The date is: ");
            sw.WriteLine(DateTime.Now);
        }
    }
}

1.what is that using keyword for? what does it do? i thought using is just for namespaces.
2.where is this file created? in C:\ ?
3.what does he mean in the "Arbitrary objects..." comment?
ASKER CERTIFIED SOLUTION
Avatar of melodiesoflife
melodiesoflife

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kokas79
Kokas79

ASKER

1. cool....can u use "using" with any kind of object...do something and then have it disposed? like create an array...view its contents and then throw it away?

is he doing that just because it's good practise?
well if u dont use "using" then this code is the same as

StreamWriter sw = new StreamWriter("TestFile.txt");
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);

only that this time the StreamWriter object remains on the heap?

2. when u say assembly i'm thinking of all the files in my Visual Studio project together as a unit...am i right?
1. Normally, I use "using ( ..)" with object, which require expensive resource like: File or Database connection.
   > only that this time the StreamWriter object remains on the heap?
   You right.

2. Assembly in this case is the file, which you compile from this code.
Avatar of Kokas79

ASKER

How can i understand if an object performs a heavy operation like StreamWriter? Is there a list of objects that i should be familiar with? I know i'm asking more things, so i'll give u more points.

So an assembly can be a file only...but when using Visual Studio.NET it means the whole project?

Also, is it possible to ask u directly a question through this website?
Avatar of Kokas79

ASKER

or maybe u dont want that to happen!

:)
Hi Kokas79
There isn't a explicit list of that object. Normally any operation on File, Network, Database connection ... and any thing that you think it take a lot of resource. It depend on you.

>So an assembly can be a file only...but when using Visual Studio.NET it means the whole project?
No, the name: "assembly" only point out that: it is a file, which you can get when compiling your project.

>Also, is it possible to ask u directly a question through this website?
Yes, if you have any question, please ask, I will try with my best. And if I don't know, there are many other developers know.