C#编程之面向对象与常用API的学习
Vivian 2018-05-22 来源 : 阅读 1333 评论 0

摘要:C#编程面向对象与常用API的学习:创建一个集合对象,删除指定的元素,获取集合中实际的元素的个数。集合中的某一个元素,我们将一个对象输出到控制台,默认打印的是这个对象所在类的命名空间。希望对大家学习C#编程有所帮助。

C#编程面向对象与常用API的学习:创建一个集合对象,删除指定的元素,获取集合中实际的元素的个数。集合中的某一个元素,我们将一个对象输出到控制台,默认打印的是这个对象所在类的命名空间。希望对大家学习C#编程有所帮助。

一: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)乱码产生的原因:你保存这个文件的编码格式与打开这个文件的编码格式不一致。
 
        }
    }
}

本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言C#.NET频道! 


本文由 @Vivian 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 1
看完这篇文章有何感觉?已经有1人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程