记录一下C#的一些东西,基础好多仍是不会,仍是推荐微软的官网文档,网上的博客写的都太水了,仍是官网文档好一点c#
微软官方文档异步
其实我最想讲的就是这个,我举个例子,有两个方法,方法1和方法2,我如今想先执行方法1再执行方法2,若是我顺序执行的话,那么必须等待方法1执行完成以后才执行方法2 代码以下async
static void Main(string[] args) { method1(); method2(); } public static void method1() { for (int i = 0; i < 80; i++) { System.Console.WriteLine("method1: "+i); } } public static void method2() { for (int i = 0; i < 20; i++) { System.Console.WriteLine("method2: "+i); } }
执行一下就知道了,必须等待方法1执行完才会执行方法2,就好比我想烧水作饭,必须先等水烧开了我才能洗菜切菜......这明明是能够同时作的事情,咱们可使用异步方法解决code
这个分为两种状况,I/O和CPU运算,我这里暂时没用到I/O因此不写了,讲讲CPU运算的文档
static void Main(string[] args) { method1(); method2(); System.Console.ReadKey(); } public static async Task method1() { await Task.Run(() => { for (int i = 0; i < 80; i++) { System.Console.WriteLine("method1: " + i); } }); } public static void method2() { for (int i = 0; i < 20; i++) { System.Console.WriteLine("method2: "+i); } }
特色就是async,Task或者Task<T>
,await,Task.Run这几个字符串
返回Task<T>
static void Main(string[] args) { callMethod(); System.Console.ReadKey(); } public static async void callMethod() { Task<int> task = method1(); int count = await task; method3(count); } public static async Task<int> method1() { int count=0; await Task.Run(() => { for (int i = 0; i < 80; i++) { System.Console.WriteLine("method1: " + i); count++; } }); return count; } public static void method2() { for (int i = 0; i < 20; i++) { System.Console.WriteLine("method2: " + i); } } public static void method3(int count) { System.Console.WriteLine("Count is "+count); }
C#读取CSV的内容,以DataTable的格式返回get
string path = @"D:\360MoveData\Users\Justin\Desktop\dgkdata\Audio Products~Accessories.csv"; public static DataTable ReadData(string filePath) { //Encoding encoding = Common.GetType(filePath); //Encoding.ASCII;// Encoding encoding = Encoding.ASCII; //Encoding.ASCII;// DataTable dt = new DataTable(); FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); //StreamReader sr = new StreamReader(fs, Encoding.UTF8); StreamReader sr = new StreamReader(fs, encoding); //string fileContent = sr.ReadToEnd(); //encoding = sr.CurrentEncoding; //记录每次读取的一行记录 string strLine = ""; //记录每行记录中的各字段内容 string[] aryLine = null; string[] tableHead = null; //标示列数 int columnCount = 0; //标示是不是读取的第一行 bool IsFirst = true; //逐行读取CSV中的数据 while ((strLine = sr.ReadLine()) != null) { if (IsFirst == true) { tableHead = strLine.Split(','); IsFirst = false; columnCount = tableHead.Length; //建立列 for (int i = 0; i < columnCount; i++) { DataColumn dc = new DataColumn(tableHead[i]); dt.Columns.Add(dc); } } else { //MySplit这个方法看下面的介绍 List<string> dataList = MySplit(strLine); aryLine = dataList.ToArray(); DataRow dr = dt.NewRow(); for (int j = 0; j < columnCount; j++) { dr[j] = aryLine[j]; } dt.Rows.Add(dr); } } if (aryLine != null && aryLine.Length > 0) { dt.DefaultView.Sort = tableHead[0] + " " + "asc"; } sr.Close(); fs.Close(); return dt; }
而后接受这个DataTable同步
//先获取全部的列名 DataTable dt = Read.ReadData(path); string[] strColumns = null; if (dt.Columns.Count > 0) { int columnNum = 0; columnNum = dt.Columns.Count; strColumns = new string[columnNum]; for (int i = 0; i < dt.Columns.Count; i++) { strColumns[i] = dt.Columns[i].ColumnName; } } //在遍历开始处理数据 foreach (DataRow dataRow in dt.Rows) { foreach (var columsName in strColumns) { switch (columsName) { case "Datasheets": break; case "Image": break; 处理逻辑...... } string aaa = dataRow[columsName].ToString(); 处理逻辑...... Console.WriteLine(aaa); } }
这个也能够叫作,C#读取CSV文件逗号问题博客
我读取的一串字符串是这样的
"许嵩","蜀,云泉",1,22,"音乐"
我使用Split(',')以后蜀云泉就分开了,这显然不是我要的结果
解决方法可使用正则,可是我不会写,因此写一个最基础的substring
private static List<string> MySplit(string str) { const char mark = '"'; const char comma = ','; bool startMark = false; int startIndex = -1; int endIndex = -1; List<string> myList = new List<string>(); for (int i = 0; i < str.Length; i++) { if (str[0] == comma) { myList.Add(""); } if (startMark && str[i] == comma) { continue; } if (str[i] == comma && i > 0) { endIndex = i; } if (str[i] == mark && !startMark) { startMark = true; } else if (str[i] == mark && startMark) { startMark = false; } if (startIndex == -1) { startIndex = i; } if ((startIndex >= 0 && endIndex > 0) || (endIndex == -1 && i == str.Length - 1)) { if (endIndex == -1) { endIndex = i + 1; } myList.Add(str.Substring(startIndex, endIndex - startIndex)); startIndex = -1; endIndex = -1; } } return myList; }
这个strLine就是C#读取CSV的一行内容