C#开发 —— 泛型,文件

泛型的目标是采用广泛适用和可交互性的形式来表示算法和数据结构 —— 参数化

泛型能子啊编译时提供强大的类型检查,减少数据类型之间的显式转换,装箱操作和运行时的类型检查

泛型的类型参数T可以被看作是一个占位符,代表了某种可能的类型

namespace Test01
{
    //创建一个泛型接口
    public interface IGenericInterface<T>
    {
        T CreateInstance();                                //接口中调用CreateInstance方法
    }
    //实现上面泛型接口的泛型类
    //派生约束where T : TI(T要继承自TI)
    //构造函数约束where T : new()(T可以实例化)
    public class Factory<T, TI> : IGenericInterface<TI> where T : TI, new()
    {
        public TI CreateInstance()                            //创建一个公共方法CreateInstance
        {
            return new T();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //实例化接口
            IGenericInterface<System.ComponentModel.IListSource> factory = new Factory<System.Data.DataTable, System.ComponentModel.IListSource>();
            //输出指定泛型的类型
            Console.WriteLine(factory.CreateInstance().GetType().ToString());
            Console.ReadLine();
        }
    }
}

在实例化泛型时可以使用约束对类型参数的类型种类施加限制,约束是使用where上下文关键字指定的

泛型方法,在声明中包括了类型参数T的方法

泛型方法可以使用多类型参数进行重载

namespace Test02
{
    public class Finder                                        //建立一个公共类Finder
    {
        public static int Find<T>(T[] items, T item)                    //创建泛型方法
        {
            for (int i = 0; i < items.Length; i++)                        //调用for循环
            {
                if (items[i].Equals(item))                            //调用Equals方法比较两个数
                {
                    return i;                                    //返回相等数在数组中的位置
                }
            }
            return -1;                                        //如果不存在指定的数,则返回-1
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int i = Finder.Find<int>(new int[] { 1, 2, 3, 4, 5, 6, 8, 9 }, 6);    //调用泛型方法,并定义数组指定数字
            Console.WriteLine("6在数组中的位置:" + i.ToString());        //输出中数字在数组中的位置
            Console.ReadLine();
        }
    }

}
namespace Test03
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> myList = new List<int>();
            for (int i = 0; i < 10; i++)
            {
                myList.Add(i);
            }
            foreach (int i in myList)
            {
                Console.WriteLine(i);
            }
            Console.ReadLine();
        }
    }
}
namespace Test04
{
    class Program
    {
        static void Main(string[] args)
        {
            myclass1<int> mclass1 = new myclass1<int>();
            myclass2<int> mclass2 = new myclass2<int>();
            Console.ReadLine();
        }
        class myclass1<T>
        {
            public myclass1()
            {
                Console.WriteLine("这是第一个泛型类");
            }
        }
        class myclass2<T> : myclass1<T>
        {
            public myclass2()
            {
                Console.WriteLine("这是第二个泛型类");
            }
        }
    }
}

System.IO 命名空间

File 类和Directory 类分别用来对文件和各种目录进行操作,可以被实例化,但不能被其他类继承

File类中的所有方法都是静态的,所以如果只想执行一个操作,那么使用File类中方法的效率比使用相应的FileInfo类中的方法更高

File类的静态方法对所有方法都执行安全检查,如果打算多次重用某个对象,可考虑FileInfo方法 —— 不用总是检查

if (File.Exists(textBox1.Text))       //使用File类的Exists方法判断要创建的文件是否存在
                {
                    MessageBox.Show("该文件已经存在");
                }
                else
                {
                    File.Create(textBox1.Text);       //使用File类的Create方法创建文件
                }

Directory 类,操作目录

if (Directory.Exists(textBox1.Text))          //使用Directory类的Exists方法判断要创建的文件夹是否存在
                {
                    MessageBox.Show("该文件夹已经存在");
                }
                else
                {
                    Directory.CreateDirectory(textBox1.Text);  //使用Directory类的CreateDirectory方法创建文件夹
                }

FileInfo类没有静态方法,该类中的方法可以用于实例化的对象

如果要在文件上执行几种操作(或重复操作),则实例化FileInfo对象效率更高

FileInfo finfo = new FileInfo(textBox1.Text);
                if (finfo.Exists)                        //使用FileInfo对象的Exists属性判断要创建的文件是否存在
                {
                    MessageBox.Show("该文件已经存在");
                }
                else
                {
                    finfo.Create();                       //使用FileInfo对象的Create方法创建文件
                }
DirectoryInfo dinfo = new DirectoryInfo(textBox1.Text);  //实例化DirectoryInfo类对象
                if (dinfo.Exists)                        //使用DirectoryInfo对象的Exists属性判断要创建的文件夹是否存在
                {
                    MessageBox.Show("该文件夹已经存在");
                }
                else
                {
                    dinfo.Create();                      //使用DirectoryInfo对象的Create方法创建文件夹
                }

获取文件信息

if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
                FileInfo finfo = new FileInfo(textBox1.Text);         //实例化FileInfo对象
                string strCTime, strLATime, strLWTime, strName, strFName, strDName, strISRead;
                long lgLength;
                strCTime = finfo.CreationTime.ToShortDateString();    //获取文件创建时间
                strLATime = finfo.LastAccessTime.ToShortDateString(); //获取上次访问该文件的时间
                strLWTime = finfo.LastWriteTime.ToShortDateString();  //获取上次写入文件的时间
                strName = finfo.Name;                                 //获取文件名称
                strFName = finfo.FullName;                            //获取文件的完整目录
                strDName = finfo.DirectoryName;                       //获取文件的完整路径
                strISRead = finfo.IsReadOnly.ToString();              //获取文件是否只读
                lgLength = finfo.Length;                              //获取文件长度
                MessageBox.Show("文件信息:
创建时间:" + strCTime + " 上次访问时间:" + strLATime + "
上次写入时间:" + strLWTime + " 文件名称:" + strName + "
完整目录:" + strFName + "
完整路径:" + strDName + "
是否只读:" + strISRead + " 文件长度:" + lgLength);
            }
原文地址:https://www.cnblogs.com/sprint1989/p/3830064.html