I'm doing a tutorial on how to save a text file with StreamWriter. I understand how to do it but I started reading a tutorial on the StreamWriter Class to get a better understanding of it. In an example, it places the word using StreamWriter but in the other tutorial, it does not.  Why is this? Do I need to use the using?  how to save to text tutorials way  private void button3_Click(object sender, EventArgs e)         {             StreamWriter File = new StreamWriter("data.txt", true);              File.Write("");             File.Close();         }   ==  StreamWriter Class In C# With Programming Example  ==  namespace StreamWriter_Class {     class Program     {         static void Main(string[] args)         {             string file = @"D:\csharpfile.txt";         ---&gt;    using (StreamWriter writer = new StreamWriter(file))
4191
User  answered the wonderful  question by mentioning the using keyword. Can you elaborate on that? What are the uses of using?
57492
I have a helper class pulling a string from an XML file. That string is a file path (so it has backslashes in it). I need to use that string as it is... How can I use it like I would with the literal command?  Instead of this:  string filePath = @"C:\somepath\file.txt";   I want to do this:  string filePath = @helper.getFilePath(); //getFilePath returns a string   This isn't how I am actually using it; it is just to make what I mean a little clearer. Is there some sort of .ToLiteral() or something?
32831
If you have a java.io.InputStream object, how should you process that object and produce a String?    Suppose I have an InputStream that contains text data, and I want to convert it to a String, so for example I can write that to a log file.  What is the easiest way to take the InputStream and convert it to a String?  public String convertStreamToString(InputStream is) {     // ??? }
81621
If I write the following class:  public class Example {        int j;       int k;        public Example(int j, int k) {            j = j;            k = k;       }        public static void main(String[] args) {            Example exm = new Example(1,2);            System.out.println(exm.j);            System.out.println(exm.k);       }  }   The program compiles, but when I run the program, the main method will print out two 0s. I know that in order to say that I want to initialize the instance variables in the constructor I have to write:  this.j = j; this.k = k;   But if I don't write it, then which variable is evaluated (or considered) in the constructor (on the left and on the write hand side of the expressions)? Is is the argument or the instance variable? Does it make a difference?  Are there other cases where the use of this is obligatory?
64302
I frequently find myself writing code like this:  List&lt;int&gt; list = new List&lt;int&gt; { 1, 3, 5 }; foreach (int i in list) {     Console.Write("{0}\t", i.ToString()); } Console.WriteLine();   Better would be something like this:  List&lt;int&gt; list = new List&lt;int&gt; { 1, 3, 5 }; Console.WriteLine("{0}\t", list);   I suspect there's some clever way of doing this, but I don't see it. Does anybody have a better solution than the first block?
2458
I want to create a text file which will add each command I am going to run on the terminal as a new line entry in the text file. i.e., I need to keep a record of what are the things I'm doing in the terminal. Further if I can keep a record of outputs I'm getting in the terminal, along with the inputs in the terminal, as a text file, it will be much better.   Each time I forget a command I have to search again in the Internet for the command.  Can I can make a file like above?
2935
I am fascinated by the way the CLR and GC works (I'm working on expanding my knowledge on this by reading CLR via C#, Jon Skeet's books/posts, and more).  Anyway, what is the difference between saying:  MyClass myclass = new MyClass(); myclass = null;   Or, by making MyClass implement IDisposable and a destructor and calling Dispose()?  Also, if I have a code block with a using statement (eg below), if I step through the code and exit the using block, is the object disposed of then or when a garbage collection occurs? What would happen if I call Dispose() in the using block anyay?  using (MyDisposableObj mydispobj = new MyDisposableObj()) {  }   Stream classes (eg BinaryWriter) have a Finalize method? Why would I want to use that?
120245
What's a better way to start a thread, _beginthread, _beginthreadx or CreateThread?  I'm trying to determine what are the advantages/disadvantages of _beginthread, _beginthreadex and CreateThread.  All of these functions return a thread handle to a newly created thread, I already know that CreateThread provides a little extra information when an error occurs (it can be checked by calling GetLastError)... but what are some things I should consider when I'm using these functions?  I'm working with a windows application, so cross-platform compatibility is already out of the question.  I have gone through the msdn documentation and I just can't understand, for example, why anybody would decide to use _beginthread instead of CreateThread or vice versa.  Cheers!  Update: OK, thanks for all the info, I've also read in a couple of places that I can't call WaitForSingleObject() if I used _beginthread(), but if I call _endthread() in the thread shouldn't that work? What's the deal there?
89906
Sometime I have to use the text files created in Ubuntu, using editors, in Windows editors. So basically when ever I create a text file in Ubuntu I append the extension .txt, and it does make the file open in Windows very easily.  But the actual problem is the text files created in Ubuntu are so difficult to understand (read) when opened in Windows' Notepad. No matter how many lines have been used, all the lines appear in the same one line.  Is there a easy way to save the text files in Ubuntu's basic editors so that it can be seen exactly the same in Windows' notepad?
51479
Having read the threads  and  I am wondering for classes such as SqlConnection or one of the several classes inheriting from the Stream class does it matter if I close Dispose rather than Close?
1624
The very common beginner mistake is when you try to use a class property "statically" without making an instance of that class. It leaves you with the mentioned error message:     You can either make the non static method static or make an instance of that class to use its properties.   Why? I am not asking for solutions. I would be grateful to know what is the reason behind it. The very core reason!  private java.util.List&lt;String&gt; someMethod(){     /* Some Code */     return someList;             }  public static void main(String[] strArgs){                // The following statement causes the error. You know why..     java.util.List&lt;String&gt; someList = someMethod();          }
59157
I have a StreamReader object that I initialized with a stream, now I want to save this stream to disk (the stream may be a .gif or .jpg or .pdf).  Existing Code:  StreamReader sr = new StreamReader(myOtherObject.InputStream);    I need to save this to disk (I have the filename). In the future I may want to store this to SQL Server.   I have the encoding type also, which I will need if I store it to SQL Server, correct?
102481
Hi I want to prepend text to a file.  For example I want to add tasks to the beginning of a todo.txt file.  I am aware of echo 'task goes here' &gt;&gt; todo.txt but that adds the line to the end of the file (not what I want).
91985
I have a huge file, where I have to insert certain characters at a specific location. What is the easiest way to do that in C# without rewriting the whole file again.
34160
C# Textreader and TextWriter are the another way to read and write file respectively...      I added a text box and button I called tbUpdate and btUpdate respectively...      In the right hand column, we’ll use three TextBlocks; the first and third as fixed labels (with the values Likeability: and % respectively)...      WorkerSupportsProgress and WorkerSupportsCancellation which hold true/false values that let the backgroundworker report progress or cancel an async progress respectively...   While learning many the many different aspects of development, I have not come across a book or series where the word respectively is not used in explaining an example. Honestly I guess it's implied that we know what it means. To me I always kind of skim over it because I can usually figure out what they're saying through the surrounding context. Can anyone explain this?
122495
I cannot figure out how to make a C# Windows Form application write to a textbox from a thread.  For example in the Program.cs we have the standard main() that draws the form:  static void Main() {     Application.EnableVisualStyles();     Application.SetCompatibleTextRenderingDefault(false);     Application.Run(new Form1()); }   Then we have in the Form1.cs:  public Form1() {     InitializeComponent();      new Thread(SampleFunction).Start(); }  public static void SampleFunction() {     while(true)         WindowsFormsApplication1.Form1.ActiveForm.Text += "hi. "; }   Am I going about this completely wrong?  UPDATE  Here is the working code sample provided from bendewey:  public partial class Form1 : Form {     public Form1()     {         InitializeComponent();         new Thread(SampleFunction).Start();     }      public void AppendTextBox(string value)     {         if (InvokeRequired)         {             this.Invoke(new Action&lt;string&gt;(AppendTextBox), new object[] {value});             return;         }         textBox1.Text += value;     }      void SampleFunction()     {         // Gets executed on a seperate thread and          // doesn't block the UI while sleeping         for(int i = 0; i&lt;5; i++)         {             AppendTextBox("hi.  ");             Thread.Sleep(1000);         }     } }
14429
What's the difference between double quotes and single quote in C#?  I coded a program to count how many words are in a file  using System; using System.IO; namespace Consoleapp05 {     class Program     {         public static void Main(string[] args)         {             StreamReader sr = new StreamReader(@"C:\words.txt");             string text = sr.ReadToEnd();             int howmany = 0;             int howmany2 = 0;              for(int i = 0; i &lt; text.Length; i++)             {                 if(text[i] == " ")                 {                 howmany++;                 }             }             howmany2 = howmany + 1;             Console.WriteLine("It is {0} words in the file", howmany2);             Console.ReadKey(true);         }     } }   This gives me an error because of the double quotes. My teacher told me to use single quote instead, but he didn't tell me why. So what's the difference between double quotes and single quote in C#?