调试技巧
System.Diagnostics.Debug.WriteLine("信息"); //输出
System.Diagnostics.Debug.Assert(false,"信息"); //断言
定义数组
一、一维:int[] numbers = new int[]{1,2,3,4,5,6}; //不定长
int[] numbers = new int[3]{1,2,3};//定长
二、多维
int[,] numbers = new int[,]{{1,2,3},{1,2,3}}; //不定长
int[,] numbers = new int[2,2]{{1,2},{1,2}}; //定长
三、取得数组元素个数:
int b;
b = sizeof(a)/sizeof(*a);
字符串操作
string s = "" ;s ="ABCD";
Console.WriteLine(s[0]); // 输出"A";
Console.WriteLine(s.Length); // 输出4
char[] arr = s.ToCharArray(); // 把字符串打散成字符数组{'A','B','C','D'}
Console.WriteLine(s.Substring(1)); // 从第2位开始(索引从0开始)截取一直到字符串结束,输出"BCD"
Console.WriteLine(s.Substring(1, 2)); // 从第2位开始截取2位,输出"BC"
Console.WriteLine(s.IndexOf('A')); // 从字符串头部开始搜索第一个匹配字符A的位置索引,输出"0"
Console.WriteLine(s.ToLower()); // 转化为小写,输出"abcd"
Console.WriteLine(s.PadRight(6, '_')); // 使用'_'填充字符串右部,使它扩充到6位总长度,输出"ABCD__"
s ="__AB__CD__";
Console.WriteLine(s.Trim('_')); // 移除字符串中头部和尾部的'_'字符,输出"AB__CD"
s ="ADEF";
Console.WriteLine(s.Insert(1, "BC")); // 在字符串的第2位处插入字符串"BC",输出"ABCDEF"
s ="A_B_C_D";
Console.WriteLine(s.Replace('_', '-')); // 把字符串中的'_'字符替换为'-',输出"A-B-C-D"
s ="AA,BB,CC,DD";
string[] arr1 = s.Split(',');
Console.WriteLine(string.Format("{0} / {1} = {2:0.000}", 1, 3, 1.00/3.00));//格式化(静态方法Format)
s ="A,B,C,D";
string[] arr3 = s.Split(','); // arr = {"A","B","C","D"}
Console.WriteLine(string.Concat(arr3)); // 将一个字符串数组连接成一个字符串,输出"ABCD"
Console.WriteLine(string.Join(",", arr3)); // 以","作为分割符号将一个字符串数组连接成一个字符串,输出"A,B,C,D"
StringBuilder sb =new StringBuilder(); // 声明一个字符串构造器实例
sb.Append("A"); // 使用字符串构造器连接字符串能获得更高的性能
sb.Append('B');
Console.WriteLine(sb.ToString());// 输出"AB"
string str1 = "I"; string str2 = "you";
string str3 = str1 + " love " + str2;//str3 值:I love you
获得路径
桌面路径:Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
获取DLL的目录 :
Assembly myAssembly = Assembly.GetEntryAssembly();
string path = myAssembly.Location;
DirectoryInfo dr = new DirectoryInfo(path);
path=dr.Parent; //当前目录的上一级目录
获取程序当前路径的方法
string str =System.Environment.CurrentDirectory;
//例: c:\test
string str =Application.ExecutablePath;(包括名称)
//例: c:\test\myapp.exe
string str =Application.StartupPath;(不包括名称)
//例: c:\test