static void Main(string[] args) { // DataTables // Dictionary Dictionary <string, string> animalFood = new Dictionary <string, string>(); animalFood.Add("Dog", "Bone,Meat"); animalFood["Cat"] = "Fish"; //string food = animalFood["Wolf"]; if (animalFood.TryGetValue("Wolf", out string food)) { } if (animalFood.ContainsKey("Wolf")) { string food2 = animalFood["Wolf"]; } // List string animals = "Dog,Cat,Elephont"; string[] arr1 = animals.Split(','); List <string> list1 = new List <string>(arr1); list1.Add("Wolf"); list1.Insert(0, "Fox"); string[] arr4 = { "Bird", "Mouse" }; list1.AddRange(arr4); list1.Remove("Dog"); // - 排序 //list1.Sort(); //list1.Sort((x, y) => x.Length < y.Length ? -1 : 1); list1.Sort((x, y) => x.Length.CompareTo(y.Length)); string[] arr3 = list1.ToArray(); List <string> list2 = list1.FindAll(x => x.Length == 4); list1.RemoveAll(x => x.Length == 4); // Array foreach (string item in arr1) { Console.WriteLine(item); } string[] arr2 = { "111", "2222", }; // Enum int score = (int)Enum.Parse(typeof(tagScoreLevel), "Good"); Console.WriteLine("输入您的考试分数:"); if (int.TryParse(Console.ReadLine(), out int iScore)) { tagScoreLevel level1 = tagScoreLevel.Low; if (iScore >= (int)tagScoreLevel.Good) { level1 = tagScoreLevel.Good; } //switch (level1) //{ // case tagScoreLevel.Low: // break; // case tagScoreLevel.Good: // break; // default: // break; //} Console.WriteLine("您的表现:" + level1.ToString()); } //Animal animal = Animal.CreateAnimal(5,"Cat"); //animal.Age = 3; //animal.Name = "Dog"; //Animal animal = new Animal() { Age = 4, Name = "Elephont" }; //animal.SayIt(); //Animal.Jump(); /* * Cat cat = new Cat(2,"Cat","It is a lovely Cat"); * cat.SayIt(); * cat.Run(); * * IRun runObj = cat; * runObj.Run(); * */ /* * Animal animal1 = new Cat(2, "Cat", "It is a lovely Cat"); * if (animal1 is IMouseHunter catObj) * { * catObj.CatchMouse(); * catObj.job1(); * } * * * * //Cat cat1 = new Cat(2, "Cat", "It is a lovely Cat"); * //cat1.Run(); * * IRun[] arrInfRun = * { * new Dog(3,"Dog","It is a brave Dog."), * new Cat(2,"Cat","It is a lovely Cat") * }; * * * foreach (var item in arrInfRun) * { * item.Run(); * * if(item is Dog dog) * {//类型转换安全的。返回true * dog.CatchABall(); * } * * //Dog dog1 = item as Dog; * //if (dog1 != null) * //{ * // dog1.CatchABall(); * //} * } */ //TestVarType(); //return; /* * int loop = 0; * while (loop<10) * { * Console.WriteLine($"loop value is {loop}"); * loop++; * } */ //Debug.WriteLine("调试输出内容"); //Console.Error.WriteLine("错误信息"); /* * int i1 = 10, i2 = 10; * * int r1 = i1 / i2; * * Console.WriteLine($"{i1} / {i2}的计算结果是 {r1}"); */ /* * Car1 car1 = new Car1(); * * car1.Say1(); * * car1.ChangeMyTitle("Red Name"); * car1.Say1(); * Console.WriteLine("Hello 2021!"); */ }
static void Main(string[] args) { // 让 Switch更加直观 tagScoreLevel level1 = tagScoreLevel.Good; string disp = ""; /* * switch (level1) * { * case tagScoreLevel.Low: * disp = "不行啊"; * break; * case tagScoreLevel.Good: * disp = "挺不错"; * break; * default: * disp = "unknown"; * break; * } */ disp = level1 switch { tagScoreLevel.Low => "不行啊", tagScoreLevel.Good => "挺不错", _ => "unknown", }; Console.WriteLine(disp); // 方法代替LINQ表达式 //int[] numbers = new[] { 1, 34, 67, 92,87,78,55, 100, 23, 87 }; //var r1 = numbers.Where(x => x > 50 && x < 80) // .OrderBy(x => x) // .Select(a => $"data is {a}, +100 = {a + 100}"); //var result = // from a in numbers // 数据源 // where (a > 50 && a < 80) // 条件 // orderby a ascending //排序 // select $"data is {a}, +100 = {a + 100}"; //返回 可以格式化,自定义计算 //foreach (var item in r1) //{ // Console.WriteLine(item); //} /* * * print1(); * * print2("ddd"); * * // Lambda 源自委托方法,匿名函数 * Console.WriteLine($"{add(2,7)}"); * Console.WriteLine($"{add2(2,1)}"); * * * * * * * * var animals = new[] * { * new{Name="Dog",Age=3,Type1="Home"}, * new{Name="Cat",Age=1,Type1="Home"}, * new{Name="Fox",Age=1,Type1="Wild"}, * new{Name="Wolf",Age=2,Type1="Wild"}, * new{Name="Monkey",Age=5,Type1="Wild"}, * }; * * var foods = new[] * { * new {Name="Dog",Food="Bone,Meat"}, * new {Name="Cat",Food="Fish"}, * new {Name="Wolf",Food="Meat"}, * new {Name="Monkey",Food="Banana,Apple"}, * }; * * var sounds = new[] * { * new {Name="Dog",Sound="汪汪"}, * new {Name="Cat",Sound="喵喵"}, * new {Name="Wolf",Sound="喔喔"}, * new {Name="Monkey",Sound="hi hi"}, * }; * * var animalFood = * from a in animals * join f in foods //join .. in * on a.Name equals f.Name // on ... equals * join s in sounds //join .. in * on a.Name equals s.Name // on ... equals * orderby a.Name//排序字段 * select $"{a.Name} like {f.Food}. {s.Sound}";//返回值 * * var result = * from a in animalFood * select $"123 {a}"; * * foreach (var item in result) * { * Console.WriteLine(item); * } * * */ /* * * // 方法名来调用分组 * var r1 = animals.GroupBy(a => a.Type1);//没有LINQ查询代码直观,灵活。 * foreach (var item in r1) * { * Console.WriteLine($"分类:{item.Key}"); * * foreach (var x in item) * { * Console.WriteLine(x); * } * } * * // 查询分组 * var result = * from a in animals * //where a.Age>2 * group a by a.Type1 into grp * //where grp.Count() > 2 * orderby grp.Count() descending * select new * { * Type = grp.Key, * Count = grp.Count(), * }; * * foreach (var item in result) * { * Console.WriteLine(item); * } * * * * * * foreach (var item in result) * { * Console.WriteLine($"分类:{item.Key}"); * * foreach (var x in item) * { * Console.WriteLine(x); * } * } * * var result = * from a in animals * group a by a.Type1; * foreach (var item in result) * { * Console.WriteLine($"分类:{item.Key}"); * * foreach (var x in item) * { * Console.WriteLine(x); * } * } */ /* * * var result = * from a in animals * where a.Type1 == "Wild" * orderby a.Age descending * select $"{a.Name},Age {a.Age}"; * * foreach (var item in result) * { * Console.WriteLine(item); * } */ // Group, join /* * * int[] numbers = new[] { 1, 34, 67, 92, 100, 23, 87 }; * * // var 编译的时候会自动识别类型 * var result = * from a in numbers // 数据源 * where (a > 50 && a<80) // 条件 * orderby a ascending //排序 * select $"data is {a}, add 100 = {a+100}"; //返回 可以格式化,自定义计算 * * foreach (var item in result) * { * Console.WriteLine($"{item}"); * } * * Console.WriteLine($"平均值:{numbers.Take(2).Average():F2}"); * * IEnumerable<int> result = numbers.Take(3); * foreach (var item in result) * { * Console.WriteLine(item.ToString()); * } */ }