zoukankan      html  css  js  c++  java
  • .NET基础回顾(六)

    一.HashTable

    1. 比起集合效率高,取table中东西的时候,根据键值计算出哈希值来取,不需要一个一个遍历。

    程序示例:

     1 static void Main(string[] args)
     2         {
     3             Hashtable table = new Hashtable();
     4             table.Add("1","a");
     5             table.Add("2", "b");
     6             table.Add("3", "c");
     7 
     8             for (int i = 0; i < table.Count; i++)
     9             {
    10                 Console.WriteLine(table[i]);
    11             }
    12              
    13             Console.ReadKey();
    14         }

    二.泛型

    1. 微软框架也提供了List<T>泛型集合,和一般集合的用法类似,如下:

    List<Person> list = new List<Person>();

    list.Add(new Person());

    2. 字典类型是特殊的HashTable 类型,需要指定键和值的类型,如下:

    Dictionary<string,Person> dic = new Dictionary<string,Person>();

    dic.Add(“1”,new Person());

    dic[“1”].SayHi();

    //遍历

    foreach(string key in dic.Keys)

    {

    }

    foreach(Person p in dic.Values)

    {

    }

    Foreach(KeyValuePair<string,Person> pair in dic)

    {

           Console.WriteLine(pair.Key+”:”+pair.Value.SayHi());

    }

    三.异常捕获

    1. 只把有可能发生异常的代码用try包围起来。

    2. try代码里如果发生异常,就会立即跳到catch里面去执行,执行完成之后,继续往下执行,try里面代码没有发生异常则跳过catch继续往下执行。

    程序示例:

    3. catch后面的Exception用来接收发生异常的具体信息。

    4. 可以自定义异常,如下:

     1 //自定义异常
     2 class MyException:Exception    
     3 {
     4 public MyException(string msg):base(msg)
     5 {
     6 }
     7 }
     8 //主函数
     9 static void Main(string[] args)
    10         {
    11             int i = 1;
    12             int j = 0;
    13             int k = 0;
    14             try
    15             {
    16                 if (j == 2 || j == 0)
    17                 {
    18                     throw new MyException("除数不能为0或2");
    19                 }
    20                 Console.WriteLine("666666");
    21                 k = i / j;
    22             }
    23 //子异常类应该写在前面,优先捕获,Exception异常汇总对象放在最后
    24             catch (MyException ex)
    25             {
    26                 Console.WriteLine("这是特殊的异常处理");
    27             }
    28             catch (DivideByZeroException ex)
    29             {
    30                 Console.WriteLine(ex.Message);
    31             }
    32             catch (InvalidOperationException ex)
    33             {
    34                 Console.WriteLine(ex.Message);
    35             }
    36             catch (Exception ex)
    37             {
    38                 Console.WriteLine(ex.Message);
    39             }
    40         }
    41 //不用自定义异常,但是可以调用系统的异常类来抛出自定义msg
    42         //static void Main(string[] args)
    43         //{
    44         //    int i = 1;
    45         //    int j = 0;
    46         //    int k = 0;
    47         //    try
    48         //    {
    49         //        if (j == 2 || j == 0)
    50         //        {
    51         //            throw new Exception("除数不能为0或2");
    52         //        }
    53         //        Console.WriteLine("666666");
    54         //        k = i / j;
    55         //    }
    56         //    catch (DivideByZeroException ex)
    57         //    {
    58         //        Console.WriteLine(ex.Message);
    59         //    }
    60         //    catch (Exception ex)
    61         //    {
    62         //        Console.WriteLine(ex.Message);//输出内容为"除数不能为0或2"
    63         //    }
    64 
    65         //    Console.ReadKey();
    66         //}

    5. try后面跟catch或者finally,不管有没有发生异常finally中代码都会被执行。

    try…catch…finally

    6. 程序练习:

     1 //因为p为引用类型,所以返回的p.Age值为2(可以到reflector里面反编译查看执行过程)
     2 static Person Test()
     3         {
     4             int i = 1;
     5             int j = 1;
     6             Peron p = new Person();
     7             p.Age = 1;
     8             try
     9             {
    10                 int k = i / j;
    11                 return p;
    12             }
    13             finally
    14             {
    15                 p.Age++;
    16             }
    17         }

    四.单例模式

    程序示例:

     1 class Person
     2 {
     3     public string Name{get;set;}
     4     
     5     //1.私有化构造函数
     6     //2.提供一个私有的静态的Person类型的变量
     7     //3.提供一个公共的静态的方法,用于返回上面的变量
     8     private static Person p;
     9     
    10     public static Person GetSingle()
    11     {
    12         if(p == null)
    13         {
    14             p = new Person();
    15         }
    16         return p;
    17     }
    18     
    19     private Person()
    20     {
    21     
    22     }
    23 }
    24 
    25 static void Main(string[] args)
    26 {
    27     //其实p1,p2,p3所new出来的对象都指向同一个对象,存储一样的地址
    28     //因为“private static Person p;”声明的Person p是静态的
    29     Person p1 = new Person.GetSingle();
    30     Person p2 = new Person.GetSingle();
    31     Person p3 = new Person.GetSingle();
    32 }
    33 
    34 
    35 
    36 //简单应用:利用单例模式来使得MDI新增窗体只能有一个
    37 //部分代码
    38 public partial class FormAdd:FormAdd
    39 {
    40     private static FormAdd f;
    41     public static FormAdd GetSingle()
    42     {
    43         if(f == null || f.Dispose)
    44         {
    45             f = new FormAdd();
    46         }
    47         return f;
    48     }
    49     
    50     private FormAdd()
    51     {
    52         InitializeComponet();
    53     }
    54 }

    五.Path

    程序示例:

     1 //Path类
     2 
     3 //引入命名空间
     4 using System.IO;
     5 
     6 //更改的是路径字符串的后缀名,不会更改实际的文件
     7 //结果是将"d:1.txt"改成"d:1.avi"
     8 string newPath = Path.ChangeExtension(@"d:1.txt","avi");
     9 
    10 //合并多个字符串路径
    11 newPath = Path.Combine(@"d:12","12.txt");
    12 
    13 //得到当前文件坐在文件夹的路径
    14 newPath = Path.GetDirectoryName();
    15 
    16 //得到指定文件路径的后缀名
    17 newPath = Path.GetExtension(path);
    18 
    19 //得到指定文件路径的文件名(文件名+后缀)
    20 newPath = Path.GetFileName(path);
    21 
    22 //得到指定文件路径的文件名(不包括后缀)
    23 newPath = Path.GetFileNameWithoutExtension(path);
    24 
    25 //获得绝对路径
    26 newPath = Path.GetFullPath(path);
    27 
    28 //得到临时文件名
    29 newPath = Path.GetTempFileName();
    30 
    31 //得到临时目录
    32 newPath = Path.GetTempPath();

    六.编码的发展

    如图所示:

    七.文件操作

    程序示例:

     1 //文件操作 
     2 
     3 //向已有的文本文件中追加字符
     4 //没有这个文件的话,会创建一个新的文件
     5 File.AppendAllText(@"d:2.txt","哈哈哈");
     6 
     7 //复制文件
     8 File.Copy(@"d:	est2.txt",@"d	esta3.txt");
     9 
    10 //删除文件,删除后不会存在于回收站中,慎用!
    11 File.Delete(@"d:	est2.txt");
    12 
    13 //判断指定路径的文件是否存在
    14 File.Exists(@"d:	est2.txt");
    15 
    16 //文件移动
    17 File.Move();
    18 
    19 //读文件,返回字符串
    20 //可能会因为编码的问题出现乱码
    21 string str = File.ReadAllText(@"d:	est2.txt");
    22 //可以指定读取文件时采用的编码方式
    23 string str = File.ReadAllText(@"d:	est2.txt",Encoding.Default);
    24 string str = File.ReadAllText(@"d:	est2.txt",Encoding.GetEncoding("GB2312"));
    25 
    26 //返回字符串数组,每一行占一个元素
    27 string[] lines = File.ReadAllLines(@"d:2.txt",Encoding.Default);
  • 相关阅读:
    SGU 107
    HDU 1724 自适应辛普森法
    POJ 1061 扩展欧几里得
    zzuli2424: 越靠近,越幸运(dfs)
    zzuli1519: 小P参加相亲大会(异或)
    zzuli1519: 小P参加相亲大会(异或)
    牛客练习赛42 A:字符串
    牛客练习赛42 A:字符串
    zzuli1511: 小P的loI
    zzuli1511: 小P的loI
  • 原文地址:https://www.cnblogs.com/HuoAA/p/4057578.html
Copyright © 2011-2022 走看看