using System.IO; string path = @"C:\temp\example.txt"; // Write string to the file File.WriteAllText(path, "Hello World!"); // Read string from the file string text = File.ReadAllText(path); Console.WriteLine(text); // Output: Hello World!
using System.Net; string url = "https://www.example.com"; // Web request WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); // Read response stream Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); string text = reader.ReadToEnd(); Console.WriteLine(text); // Output: Contents of example.com webpage
using System.Collections.Generic; // Create a list of strings ListDetermining the package library depends on the namespace used. In the examples above, the packages are part of the .NET framework.words = new List (); words.Add("hello"); words.Add("world"); // Loop through list and print elements foreach (string word in words) { Console.WriteLine(word); // Output: hello \n world } // Create a dictionary of string keys and int values Dictionary counts = new Dictionary (); counts["hello"] = 1; counts["world"] = 2; // Loop through dictionary and print keys and values foreach (KeyValuePair pair in counts) { Console.WriteLine(pair.Key + ": " + pair.Value); // Output: hello: 1 \n world: 2 }