Explore common tasks and how to accomplish them using C# with examples and community insights.
Learn how to efficiently read the content of a text file into a string or process it line by line.
using System;
using System.IO;
public class FileReader
{
public static void Main(string[] args)
{
string filePath = "example.txt";
try
{
// Read the entire file content
string fileContent = File.ReadAllText(filePath);
Console.WriteLine("File Content:");
Console.WriteLine(fileContent);
// Or read line by line
Console.WriteLine("\nReading line by line:");
using (StreamReader sr = new StreamReader(filePath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (FileNotFoundException)
{
Console.WriteLine($"Error: File not found at {filePath}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
View Full Example & Discussions
Demonstrates how to write text to a file, overwriting existing content or appending to it.
using System;
using System.IO;
public class FileWriter
{
public static void Main(string[] args)
{
string filePath = "output.txt";
string contentToWrite = "This is the first line.\nThis is the second line.";
try
{
// Write text to the file, overwriting if it exists
File.WriteAllText(filePath, contentToWrite);
Console.WriteLine($"Successfully wrote to {filePath}");
// Append text to the file
string contentToAppend = "\nThis line is appended.";
File.AppendAllText(filePath, contentToAppend);
Console.WriteLine($"Successfully appended to {filePath}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
View Full Example & Discussions
Understand how to break a string into pieces based on a delimiter and how to combine an array of strings into a single string.
using System;
public class StringManipulation
{
public static void Main(string[] args)
{
string data = "apple,banana,cherry,date";
// Splitting a string
char delimiter = ',';
string[] fruits = data.Split(delimiter);
Console.WriteLine("Fruits array:");
foreach (string fruit in fruits)
{
Console.WriteLine($"- {fruit}");
}
// Joining an array of strings
string joinedString = string.Join(" - ", fruits);
Console.WriteLine($"\nJoined string: {joinedString}");
}
}
View Full Example & Discussions
A guide to using `List
using System;
using System.Collections.Generic;
public class ListExample
{
public static void Main(string[] args)
{
// Creating a list of strings
List names = new List();
// Adding elements
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");
Console.WriteLine("Initial list:");
foreach (string name in names)
{
Console.WriteLine(name);
}
// Removing an element
names.Remove("Bob");
Console.WriteLine("\nList after removing Bob:");
foreach (string name in names)
{
Console.WriteLine(name);
}
// Inserting an element
names.Insert(1, "David");
Console.WriteLine("\nList after inserting David:");
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
View Full Example & Discussions