C#面向对象与常用API的学习

来源:互联网 发布:奇迹归来坐骑进阶数据 编辑:程序博客网 时间:2024/06/08 11:00

一:C#中的集合ArrayList

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 集合{    class Program    {        static void Main(string[] args)        {            //1)创建一个集合对象            ArrayList list = new ArrayList();            list.Add(1);            list.Add(true);            list.Add('男');            list.Add("陈如水");            list.Add(1.2);            list.Add(1.2m);            list.Add(new string[] { "陈诗悦", "陈诗音" });            list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 });            //删除指定的元素            list.Remove('男');            list.AddRange(list);            list.Clear();            //获取集合中实际的元素的个数。            for (int i = 0; i < list.Count; i++)            {                //集合中的某一个元素                Console.WriteLine(list[i]);                //我们将一个对象输出到控制台,默认打印的是这个对象所在类的命名空间。            }            //集合练习题1:求集合中元素的和与平均值,最大值和最小值            //创建一个集合            ArrayList list1 = new ArrayList();            //向集合中添加元素            list1.AddRange(new int[] { 1, 3, 56, 78, 9, 0, 3, 4, 5, 7, 8, 9 });            int sum = 0;            int max = (int)list1[0];            int min = (int)list1[0];            //遍历集合            for (int i = 0; i < list1.Count; i++)            {                //求集合里面的最大值                if (max < (int)list1[i])                {                    max = (int)list1[i];                }                //求集合里面的最小值                if (min > (int)list1[i])                {                    min = (int)list1[i];                }                //把每一个元素强转成int类型 因为Object类型是int类型的父类,所以可以强转                sum += (int)list1[i];            }            //Console.WriteLine("这几个数的和是{0},平均值是{1},最大值是{2},最小值是{3}", sum, sum / list1.Count, max, min);            //集合练习题2:写一个长度为10的集合,要求里面的元素不重复            ArrayList list2 = new ArrayList();            //创建一个随机数类            Random r = new Random();            for (int i = 0; i < 10; i++)            {                int number = r.Next(0, 10);                if (!list2.Contains(number))                {                    list2.Add(number);                }                else                {                    //一旦有重复的元素,当前的循环不算,重新来,当前减1即可                    i--;                }            }            for (int i = 0; i < list2.Count; i++)            {                Console.WriteLine(list2[i]);            }            Console.ReadKey();        }    }}

Hashtable集合的使用(键值对集合):

using System;using System.Collections;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 集合1{    class Program    {        static void Main(string[] args)        {            //第二个单词的首字母小写            Hashtable ht = new Hashtable();            ht.Add(1, "陈如水");            ht.Add(2, "陈诗音");            ht.Add(3, "陈诗悦");            ht.Add(false, true);            int a = 10;            //获取变量的类型            Type t = a.GetType();            Console.WriteLine(t);            //推断类型            var b = 10;            Type type = b.GetType();            Console.WriteLine(type);            //在键值对集合中是根据键寻找值的            //for (int i = 0; i < ht.Count; i++)            //{            //    Console.WriteLine(ht[i]);            //}            //Console.ReadKey();            foreach (var item in ht.Keys)            {                //var表示变量类型                //item表示集合里面的每一项,表示一个键值对                //collection表示待遍历的集合                //ht.Keys 表示遍历集合中的键                //ht.Values表示遍历集合中的值                Console.WriteLine("遍历集合中的每一项,获取值{0}", ht[item]);            }            Console.ReadKey();        }    }}
二:C#中的foreach使用以及Stopwatch类的使用

using System;using System.Collections;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 集合foreach循环{    class Program    {        static void Main(string[] args)        {            //for循环与foreach循环的效率问题:如果循环次数比较多,foreach循环的效率比较高。            int[] sum = { 2, 34, 5, 7, 9, 5, 34, 2, 6, 7, 89, 9, 0, 5, 4, 3, 2, 2, 12 };            //查看循环的耗时  //00:00:00.0062843            Stopwatch sw = new Stopwatch();            sw.Start();            //for (int i = 0; i < sum.Length; i++)            //{            //    Console.WriteLine(sum[i]);            //}            //00:00:00.0053999            foreach (var item in sum)            {                Console.WriteLine(item);            }            sw.Stop();            Console.WriteLine(sw.Elapsed);            //练习1:将用户输入的简体字转化成繁体字            Console.WriteLine("请输入要转化的简体字:");            string input = Console.ReadLine();            Hashtable map = new Hashtable();            //此时必须有把简体字作为键,把对应的繁体字最为值,这样才可以进行寻找到。            for (int i = 0; i < map.Count; i++)            {                if (map.ContainsKey(input[i]))                {                    //打印键所对应的值                    Console.WriteLine(map[input[i]]);                }                else                {                    //直接打印出用户的输入                    Console.WriteLine(input[i]);                }            }            Console.ReadKey();        }    }}

三:C#中的path类的使用

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace path练习{    class Program    {        static void Main(string[] args)        {            //Path类的练习:静态类,类中全部是是静态成员(其实是对字符串方法的封装)            string path = @"C:\Users\Tuhuadmin\Desktop\面向对象初级\老赵.avi";            //截取老赵.avi  取消转义            int lastIndex = path.LastIndexOf("\\");            string name = path.Substring(lastIndex + 1);            Console.WriteLine(name);            //1)获取文件的名字:            string fileName = Path.GetFileName(path);            Console.WriteLine(fileName);            //2)获取文件的名字(忽略扩展名):            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);            Console.WriteLine(fileNameWithoutExtension);            //3)获取文件扩展名:            string extensionName = Path.GetExtension(path);            Console.WriteLine(extensionName);            //4)返回指定文件的目录信息:即当前文件位于哪个文件夹路径下            string dirInfo = Path.GetDirectoryName(path);            Console.WriteLine(dirInfo);            //5)获取指定文件的全路径(包括文件名和扩展名)            string fullPath= Path.GetFullPath(path);            Console.WriteLine(fullPath);            //6)连接两个字符串成为一个路径            string str=Path.Combine(@"c:\a\","b.txt");            Console.WriteLine(str);            Console.ReadKey();        }    }}

四:C#中的File类的使用

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace file练习{    /// <summary>    /// date: 20161018     /// author: crs    /// describe: File类的练习:专门用来操作文件的类,静态类    /// 对文件常见的操作:创建、删除、复制    /// </summary>    class Program    {        static void Main(string[] args)        {            //1)在指定的路径下创建文件(如果指定的路径下已经存在此文件,就不会继续创建)            //File.Create(@"C:\Users\Tuhuadmin\Desktop\new.txt");            //Console.WriteLine("创建成功!");            //2)删除指定路径下的文件            //File.Delete(@"C:\Users\Tuhuadmin\Desktop\new.txt");            //Console.WriteLine("删除成功!");            //3)复制一个文件到指定的路径            File.Copy(@"C:\Users\Tuhuadmin\Desktop\new.txt", @"C:\Users\Tuhuadmin\new.txt");            Console.WriteLine("复制成功!");            Console.ReadKey();        }    }}

五:C#中的编解码问题

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _3编码{    class Program    {        static void Main(string[] args)        {            //1)文本文件有不同的存储方式,将字符串以什么样的形式保存为二进制文件,这就是编码问题。            //2)如果出现乱码现象,就是编码出现了问题。            //3)什么是文本文件,用记事本能够打来的文件。.doc结尾的不算。            //4)编解码为类Encoding            //5)乱码产生的原因:你保存这个文件的编码格式与打开这个文件的编码格式不一致。        }    }}

0 1
原创粉丝点击