[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

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?
0
Kokas79
Asked:
Kokas79
  • 3
  • 3
1 Solution
 
melodiesoflifeCommented:
Hi Kokas79

1. The command:

       using (StreamWriter sw = new StreamWriter("TestFile.txt"))
        {
            /// ....
        }

mean that: after close "}" sw will automatic call ws.Dispose() immediately.

2. TestFile.txt will create on the folder, which this assembly is executed.
Example: if you compile this code to test.exe and execute it in "C:\Temp", TestFile.txt will be create in "C:\Temp"

3. May be he mean that: Not only string can be written to file, but also an object can be written to file.
An object can be written to file if the object has ToString() method. In this case: DateTime.Now have a public method: ToString().
0
 
Kokas79Author Commented:
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?
0
 
melodiesoflifeCommented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Kokas79Author Commented:
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?
0
 
Kokas79Author Commented:
or maybe u dont want that to happen!

:)
0
 
melodiesoflifeCommented:
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.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now