C Sharp (C#) - File IOYou want to append a new line "Log entry" to an existing file "log.txt" without overwriting it. Which C# code snippet correctly does this?AFile.AppendAllText("log.txt", "Log entry\n");BFile.WriteAllText("log.txt", "Log entry");Cnew StreamWriter("log.txt").WriteLine("Log entry");DFile.ReadAllText("log.txt").WriteLine("Log entry");Check Answer
Step-by-Step SolutionSolution:Step 1: Understand append vs overwriteFile.AppendAllText adds text to the end without erasing existing content.Step 2: Validate optionsFile.AppendAllText("log.txt", "Log entry\n"); uses AppendAllText correctly; others overwrite or misuse methods.Final Answer:File.AppendAllText("log.txt", "Log entry\n"); -> Option AQuick Check:Appending text = File.AppendAllText [OK]Quick Trick: Use File.AppendAllText to add without overwriting [OK]Common Mistakes:MISTAKESUsing WriteAllText which overwrites fileNot disposing StreamWriter properlyTrying to write on string returned by ReadAllText
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