C Sharp (C#) - File IOWhich of the following is the correct syntax to write text to a file using StreamWriter in C#?AStreamWriter.WriteLine("file.txt", "Hello");BStreamWriter sw = new StreamWriter.WriteLine("file.txt", "Hello");CStreamWriter sw = new StreamWriter("file.txt"); sw.Write("Hello"); sw.Close();Dusing(StreamWriter sw = new StreamWriter("file.txt")) { sw.WriteLine("Hello"); }Check Answer
Step-by-Step SolutionSolution:Step 1: Recognize correct StreamWriter usageUsing statement ensures proper disposal of StreamWriter after writing.Step 2: Validate syntax correctnessusing(StreamWriter sw = new StreamWriter("file.txt")) { sw.WriteLine("Hello"); } correctly creates StreamWriter and writes a line inside using block.Final Answer:using(StreamWriter sw = new StreamWriter("file.txt")) { sw.WriteLine("Hello"); } -> Option DQuick Check:StreamWriter write syntax = using block with WriteLine [OK]Quick Trick: Use 'using' to auto-close StreamWriter after writing [OK]Common Mistakes:MISTAKESCalling WriteLine as a static methodMissing using block or Close callIncorrect constructor or method chaining
Master "File IO" in C Sharp (C#)9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More C Sharp (C#) Quizzes Classes and Objects - Constructor overloading - Quiz 6medium Classes and Objects - Methods that operate on state - Quiz 13medium Classes and Objects - Constructor overloading - Quiz 7medium Collections - HashSet for unique elements - Quiz 3easy File IO - Using statement with file streams - Quiz 12easy File IO - File paths and Directory operations - Quiz 1easy Inheritance - Why inheritance is needed - Quiz 3easy Interfaces - Multiple interface implementation - Quiz 4medium Interfaces - Interface declaration syntax - Quiz 11easy Polymorphism and Abstract Classes - Abstract classes and methods - Quiz 10hard