static void ApplyStroke(XmlReader re, IStrokeable s) { re.MoveToContent(); for (int i = 0; i < re.AttributeCount; i++) { re.MoveToAttribute(i); try { if (re.LocalName == COLOR_ATTR) s.Stroke = DColor.FromString(re.Value); else if (re.LocalName == STROKEWIDTH_ATTR) { double sw = 1; double.TryParse(re.Value, out sw); s.StrokeWidth = sw; } else if (re.LocalName == STROKESTYLE_ATTR) s.StrokeStyle = (DStrokeStyle)Enum.Parse(typeof(DStrokeStyle), re.Value, true); else if (re.LocalName == STROKECAP_ATTR) s.StrokeCap = (DStrokeCap)Enum.Parse(typeof(DStrokeCap), re.Value, true); else if (re.LocalName == STROKEJOIN_ATTR) s.StrokeJoin = (DStrokeJoin)Enum.Parse(typeof(DStrokeJoin), re.Value, true); } catch { } } }
static void Main(string[] args) { //List is a generic data type! //Automatic type inference: use "var" on the left side! var fishtank = new List <Fish> { new Goldfish("Hans-Joachim"), new Guppy("Herbert", "Jonas") }; var garden = new List <Mammal> { new Cat("Frieda"), new Bunny("Günther", "Peter") }; //Make the pets swim resp. move using for-each loops: foreach (var fish in fishtank) { fish.Swim(); } foreach (var mammal in garden) { mammal.Move(); } //Create a combined Pet list: var zoo = new List <Pet>(fishtank); zoo.AddRange(garden); //Attend all pets: foreach (var pet in zoo) { //Polymorphie (de) / Polymorphism (en): //"pet" is a base class object, but the subclass methods will be called. pet.Attend(); //Stroke those pets that implement IStrokeable: IStrokeable strokeablePet = pet as IStrokeable; if (strokeablePet != null) { strokeablePet.Stroke(); } else { Console.WriteLine("{0} is not strokeable :( so sad", pet); } /* if (pet is IStrokeable) * { * IStrokeable strokeablePet = (IStrokeable)pet; * strokeablePet.Stroke(); * } * else * { * ... * } */ } }