C# 2.0 Self-Test
In the .NET course I'm teaching (".NET Complete") I gave a little project to the class, of which all of them are new to OO design, C#, .NET, and all the related items. The project requirement was for them to study the example and see what it does. The point of the project was to allow them to test their knowledge of C# syntax, collections, generics, and nullable types
It's almost as useless as "Hello World". Or,rather, hello world is almost as useless as this.
You simply type in string and it tokenized it into characters...but they didn't know that. Anyhow here you go...
using System; using System.Collections.ObjectModel; using System.Collections.Generic; namespace CodeExamples { class Program { class Storage<T> where T : ICollection<Char>, new( ) { private Dictionary<String, T> list = new Dictionary<String, T>( ); public void ShowLines( ){ Int32? i; foreach(KeyValuePair<String, T> collection in list) { i = null; if(collection.Key.Length > 2) { i = collection.Key.Length; } Console.Write((i ?? -1) + " : " + collection.Key + " : "); foreach(Char thing in collection.Value) { Console.Write(thing); } Console.WriteLine(); } } public void Save(String text) { T parts = new T( ); foreach(Char c in text.ToCharArray( )) { parts.Add(c); } if(!list.ContainsKey(text)) { list.Add(text, parts); } } } static void Main(String[] args) { Storage<Collection<Char>> storage = new Storage<Collection<Char>>( ); Boolean done = false; while(!done) { String text = Console.ReadLine( ); if(text == "end") { done = true; continue; } storage.Save(text); } storage.ShowLines( ); Console.ReadLine( ); } } }