c#接口,预处理指令,异常处理,文件操作

C#接口

和抽象类有相似之处,都是只定义一个规则,本身不处理任何事情,在接口中可以定义,属性,方法,事件

interface IMyInterface2
{
    void Text2();
}
interface IMyInterface//声明
{
    void Text();
}
class TestInterface : IMyInterface,IMyInterface2     //实现了接口中的方法,继承多个接口
{
    public void Test()
    {
       Console.WriteLine("Text Interface Method");
    }  
    public void Test2()
    {
       Console.WriteLine("Text Interface Method2");
    }  
}
class Program
{
    static void Main(string[] args)
    {
       TextOmterface textOmterface = new TestInterface();
        textInterface.Text();
        textInterface.Text2();
        Console.WriteLine();
    }
}

接口和抽象方法的区别:

抽象类是一个类,一个类只能有一个父亲,不能同事继承多个类,接口可以在一个类实现多个接口用以实现多重的继承。

抽象类

abstract class interface IMyInterface//声明
{
   protected abstract void Text();
}
class TestInterface : IMyInterface{}//只能同时继承一个类

C#预处理指令

在编译之前就能进行一些处理

#define

#define TestDef
using System;
    namespace XXX
{
       class Program
       {
           static void Main(string[] args)
           {
    #if(TestDef)           
               Console.WriteLine("TestDef is defined");
    #else
               Console.WriteLine("TestDef is not defined")
    #endif          
           }
       }
}
   

多个版本,可以通过预处理的指令,控制指定的代码

#define#if#else#endif

#define TestDef
#undef TestDef//取消预定义
using System;
    namespace XXX
{
       class Program
       {
           static void Main(string[] args)
           {
    #if(TestDef)           
               Console.WriteLine("TestDef is defined");
    #else
               Console.WriteLine("TestDef is not defined")
    #endif          
           }
       }
}

@#error #warning

#error "这里有一个错误"
using System;
#define DEBUG//版本1
#define RELESSE//版本2
    #if DEBUG &&RELEASE
    #error "DEBUG和RELEASE不能同时定义"
    #endif
#warning "这是一个警告!"  

#region #endregion

#region 属性//对一串代码进行标记,没有特殊含义
public string name;
public int age;
#endregion//对一串代码进行标记,没有特殊含义

异常处理

try
{
    Console.WriteLIne(a[10]);
}
catch(错误提示)
{
    Console.WriteLine("提示文字(自编辑)");
}
try
{
    Console.WriteLine(a/b);//想要检测错误的代码
}
catch(Exception e)//Exception是所有异常错误的基类
{
    Console.WriteLine("数据输入错误,请重新输入");
}

任何类型的异常都可以通过Exception捕捉。

自定义异常

class UserNameOrPasswordErrorException : Exception
{
    public UserNameOrPasswordErrorException(string message) : base("用户名或者密码错误!")
    {
    }
}
class Program
{
    static void Login(string name,string password)
    {
        if(name == "123"&&password == "123")
    }
    else{
        throw new UserNameOrPasswordErrorException();
    }
    static void Main(string[] args)
    {
        string name,password;
        Console.WriteLine("请输入用户名");
            name = Console.ReadLine();
        Console.WriteLine("请输入密码");
            name = Console.ReadLine();
       
        try
        {
             Login(name,password;)
        }
        catch(UserNameOrPasswordErrorException)
        {
            Console.WriteLine(c.Message);
            //给用户一个提示
        }
        catch(Exception e)
        {
            
        }
        finally//不管用户输入对错与否都会跳到这里
        {
           Console.WriteLine("操作结束!"); 
        }
           
    }
}

C#文件操作

创建文件

FilezStream fileStream = new FileStream
    (
    "helloword.txt",FileMode.
     Append打开现有文件,并将光标定位在最后面,可以往文件里面追加一些内容,如果这个文件不存在就创建一个新的文件/
     Create穿件一个文件,如果这个文件已经有的话就删掉旧的文件然后穿件一个新的文件/
     CrealeNew创建一个新的文件,如果这个文件存在就抛出一个异常/
     Open打开一个文件,如果这个文件不存在就报错/
     OpenOrCreate打开或者创建,若不存在就创建一个,若存在就打开当前文件/
     Truncate打开一个文件,并删除文件里的内容,如果没有文件就会抛出一个异常
    )
    

写入字符串

FilezStream fileStream = new FileStream("helloword.txt",FileMode.OpenOrCreate,FileAccess.Read/ReadWrite/Write);//FileAccess.Read/ReadWrite/Write)访问权限,读,读写,写
//写
    string content = "hello word!!!"; fileStream.Write(ENcoding,UTF8.GetBytes(content),0,ENcoding,UTF8.GetBytes(content).Length);//ENcoding将字节转为字符串,GetBytes方法:将一个字符串转为一个字节数组
    fileStream.Close();//关闭
//读
byte[]content = new byte[fileStream.Length];
fileStream.Read(content,0,content.Length);
Console.WriteLine("读取成功!" + Encoding.UTF8.GetString(content));

FileInfo操作文件属性

FileInfo fileInfo = new FileInfo("helloworld.txt");
fileInfo Attributes = FileAttributes.Hidden;//Attributes属性,后面设置的能够覆盖掉原来的;

File.文件的复制和移动等

System.IO.File.Copy("helloword.txt","helloworld2.txt");

删除文件

System.IO.File.Delete("helloworld2.txt");

读取内容

Console.WriteLine(File.ReadAllText("helloworld.txt"));

写入内容

string[] str = new string[] {"helloworld1","helloworld2","helloworld3"}
File.WriteAllLines("helloworld.txt",str);

读取内容

String [] strs = File.ReadAllLines("helloworld.txt");
foreach (string str in strs)
{
    Console.WriteLIne(str);
}
Console.ReadLine();

Directory对目录做操作

Directory.Exists("test_directiry");//判断一个目录是否存在
if(Directory.Exists("test_directiry"))
{
    Directory.Delete("test_directiry");//删除创建的目录    
}
else{
    Directory.CreateDirectiry("test_directiry");//CreateDirectiry创建目录
}
String [] strs = Directory.GetFiles("test_directiry")//返回刚才目录的所有文件
    foreach (string str in strs)
{
    Console.WriteLIne(str);
}

Path类:对路径进行操作

Console.WriteLine(Path.GetFileName(str) + "后缀:" + Path。GetExtension(str));//获取文件名和后缀
Console.WriteLine("FullPath:"Path.GetFullPath(str));//获取路径名
Console.WriteLine("PathRoot:" + Path.GetPathRoot(Path.GetFullPath(str)));//获取文件根目录
Console.WriteLine("GetDirectoryName:" + Path.DirectoryName(str));//获取目录名
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值