装箱和拆箱
值类型转换为对象类型为装箱
对象类型转化为值类型为拆箱
c#中类的默认访问级别是internal
string与String
string 只是String的别名
C#中的转义字符和@
\(反斜杠是转义字符),@可以取消转义字符
StringBuilder构建字符串
string s = s1 +"123"; //浪费内存
StringBuilder sb = new StringBuilder();
sb.Append("123");
sb.ToString();
类型转换
隐式转换:小的转大的,比如short转int
显式转换:大的转小的(有精度丢失危险)
Convert类型转化
DateTime dateTime = Convert.ToDateTime("2020/5/12 12:05:54");
C#特有运算符
Console.WriteLine(a is bool);//is运算符,判断变量是否属于这个类
b = (int)c;
b = c as int;//as运算符,强制类型转换,但是若失败不会抛出异常,会置初始值
C#可空类型
int? i =null;
int a = i ?? 0;//如果i的值为空,则a为0,否则a为i
C#声明二维数组
int[,] numbers = new int[2,3]{
{
1,2,3},{
4,5,6}};
Console.WriteLine(numbers[0,0]);
C#中的集合
//ArrayList(不指定类型)
ArrayList arrayList = new ArrayList();
arrayList.Add(5);
arrayList.Add(79);
arrayList.Add(22);
//arrayList.Add("asd45"); Sort时会有异常
arrayList.Count;
arrayList.Sort();
//List
List<int> list = new List<int>();
list.Add(100);
list.Add(8);
list.Add(84);
list.Remove(100);
list.Clear();
//Hashtable(不指定类型)
Hashtable hashtable = new Hashtable();
hashtable.Add("001","a");
hashtable.Add("002","b");
hashtable.Add("003","c");
foreach(object key in hashtable.Keys)
{
Console.WriteLine(hashtable[key]);
}
hashtable.Remove("001");
Console.WriteLine(hashtable[0]);
Dictionary<string,string> dict = new Dictionary<string,string>();//使用放大与上方一致
C#中存在析构函数(与C++用法一致)
~Box()
{
}
C#多态
静态多态(编译时多态)
- 函数(方法)重载
- 运算符重载
class Box
{
int length,width,height;
public static Box operator +(Box a,Box b)
{
Box box = new Box();
box.length = a.lengh*b.length;
return box;
}
}
动态重载(执行时才发生)
- 抽象类
- 虚方法
abstract class Shape
{
abstract public int area();
}
class Shape
{
public virtual int area()
{
return 0;
}
}
class Rect:Shape
{
public int width;
public int height;
public override int area()
{
return width*height;
}
}
- 接口:与抽象类不同,一个类可以实现多个接口。
interface IMyInterface
{
void Test();
}
class TestInterface:IMyInterface
{
public void Test()
{
}
}
static void main()
{
TestInterface testInterface = new TestInterface();
testInterface.Test();
}
C#的预处理指令
简介
在编译之前可以对信息进行处理的指令为预处理指令。
功能
程序发布多个版本,屏蔽某些功能
代码样例
#define TestDef//定义TestDef
#undef TestDef//取消定义
#define DEBUG
#define RELEASE
#if DEBUG && RELEASE
#error "这里有个错误:DEBUG和RELEASE不能同时定义"
#warning "这里有个警告"
#endif
using System;
namespace test
{
class Program
{
#region 字段区域
//要写的代码
#endregion
static void main(string[] args)
{
#if(TestDef)//定义了字符,执行下方代码
#else
#endif
}
}
}
C#中的异常处理
class LoginErrorException:Exception
{
public LoginErrorException():base("用户名或密码错误")
{
}
}
if(Login("123","456"))
{
//登陆成功
}
else
{
throw new LoginErrorException();
}
try
{
a[4]=6;
}
catch(Exception e)
{
Console.WriteLine(e.message);
}
finally
{
}
C#文件操作
using System.IO;
static void Main(String[] args)
{
FileStream fileStream = new FileStrea("a.txt",FileMode.OpenOrCreate,FileAccess.Write);
//写
string content="hello worl1!";
fileStream.Write(Encoding.UTF8.GetBytes(content),0,Encoding.UTF8.GetBytes(content).Length);//先转为字节数组
//读
byte[] content = new byte[fileStream.Length];
fileStream.Read(content,0,content.Length);
Console.WriteLine("读取成功:"+Encoding.UTF8.GetString(content));
fileStream.Close();
FileInfo fileInfo = new FileInfo("a.txt");
fileInfo.Attributes = FileAttribute.Hidden;
fileInfo.Attributes = FileAttribute.ReadOnly;
File.Copy("a.txt","b.txt");
File.Delete("b.txt");
string[] str = new string[]{
"asdasd","fesdf","asfrg"};
File.WriteAllText("a.txt",str);
string[] strs = File.ReadAllText("a.txt");
//目录操作
Diretory.CreateDirectory("test");
if(Diretory.Exist(test))
{
//doSomthing
}
string[] strs = Diretory.GetFiles("test");
//Path操作
PathGetFileName(str)+"后缀:"+Path.GetFullPath(strs[0])+"绝对路径:"+Path.GetFullPath(strs[0])+"根目录:"+Path.GetPathRoot(Path.GetFullPath(strs[0]))+"目录:"+Path.GetDirectoryName(str[0]);
}