標籤:style blog color 使用 os io strong 資料
1.引用傳遞參數ref
static void Main(string[] args) { int n1 = 10, n2 = 20; Swap(ref n1,ref n2); Console.WriteLine("n1={0},n2={1}",n1,n2); Console.ReadKey(); } private static void Swap(ref int n1, ref int n2) { int temp = n1; n1 = n2; n2 = temp; }
2.字串的Length屬性工作表示字串中的字元數,無論中英文還是符號。區分於位元組數。 3.可變參數、數組求最大值 static void Main(string[] args) { //4.用方法來實現:計算兩個數的最大值。思考:方法的參數?傳回值?擴充(*):計算任意多個數間的最大值(提示:params)。 int max=GetMaxValue(1, 2, 3, 4); Console.WriteLine(max); Console.ReadKey(); } static int GetMaxValue(params int[] nums) { //對數組中的元素求最大值 int max=nums[0]; for (int i = 1; i < nums.Length; i++) { if(nums[i]>max) { max=nums[i]; } } return max; }
4.shift+alt+F10 寫一個沒有定義的函數可以直接產生這個函數 5.質數的判斷一個數是小數乘以大數,或者是兩個相同的數相乘。所以為了減少迴圈次數,對需要判斷是否為指數的數開方去整,以此作為迴圈的邊界。將: for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true;改為(注意不要漏掉小於等號): for (int i = 2; i <=Math.Sqrt(n) ; i++) { if (n % i == 0) return false; } return true;
6.返回名字最長的 static void Main(string[] args) { string[] name = { "馬龍", "邁克爾喬丹", "雷吉米勒", "蒂姆鄧肯", "科比布萊恩特" }; string userName = GetLongestName(name); Console.WriteLine(userName); Console.ReadKey(); } private static string GetLongestName(string[] name) { string longName = name[0]; for (int i = 1; i < name.Length; i++) { if (name[i].Length > longName.Length) longName = name[i]; } return longName; }
7.Math.Round(數,精度) 四捨五入 foreach迴圈遍曆數組 8.拼接字串用StringBulider 9.List<> List<string> list = new List<string>();//集合 while (num >= 2) { //商 int shang = num / 2; int yushu = num % 2; list.Add(yushu.ToString()); num = shang; } list.Add(num.ToString()); list.Reverse();//集合反轉 return string.Join("", list.ToArray());//用Null 字元串拼接集合成字串
10.Random()函數執行個體化不能放在迴圈內, Random random = new Random(); //使用系統時間作為種子 for (int i = 0; i < 10; i++) //產生一組隨機序列 { Console.WriteLine(random.Next(1, 100)); } for (int i = 0; i < 10; i++) { Random random = new Random(); //使用系統時間作為種子 Console.WriteLine(random.Next(1, 100)); //產生一個隨機數,而不是一組序列,因為運算速度很快,所以在一定時間內,同樣的種子所產生的隨機序列的第一個數字總是相同的 }
11.indexof()該方法返回在整個字串中,指定的字元或字串第一次出現的索引位置,未找到返回-1,
string msg = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年記?” 患者:“七十五歲。” 大夫:“二十歲咳嗽嗎”患者:“不咳嗽。” 大夫:“四十歲時咳嗽嗎?” 患者:“也不咳嗽。” 大夫:“那現在不咳嗽,還要等到什麼時咳嗽?”"; int count = 0; int index = 0;//記錄索引 string word="咳嗽"; while((index=msg.IndexOf(word,index))!=-1) count++; Console.WriteLine("第{0}次出現【咳嗽】,出現的索引位置是{1}", count, index); //下次尋找的時候要從上一次找到的【咳嗽】的索引位置加上"咳嗽"這個字串的length index += word.Length; } Console.ReadKey();
12.split() //將字串" hello world,你 好 世界 ! "兩端空格去掉,並且將其中的所有其他空格都替換成一個空格,輸出結果為:"hello world,你 好 世界 !"。 string msg = " hello world,你 好 世界 ! "; msg = msg.Trim(); string[] result = msg.Split(new char[] { ‘ ‘ }, StringSplitOptions.RemoveEmptyEntries); Console.WriteLine(string.Join(" ", result)); Console.ReadKey();
13.斷點調試 )F9 F5 F10 F11 ,CTRL+F10直接從游標位置設定斷點開始調試,可以對變數添加監視,或者滑鼠移到變數上即可
14.輸入 cw敲兩下Tab即出Console.WriteLine();輸入switch敲兩下Tab同樣出Switch的架構,節省打代碼時間 15.Ctrl+k+C注釋,Ctrl+K+U取消注釋 整理VS提示 16.把char數組變成string類型 char[] chs;string s=new string(chs)
17.ctrl+r+e 自動封裝屬性把類中屬性 : private double _salary;封裝成: private double _salary; public double Salary { get { return _salary; } set { _salary = value; } }
18.重寫類方法override+方法名 public override void SayHi() { Console.WriteLine("HI~~~~in Employee"); }
19.索引器索引器格式: public int this[int index]{get{}set{}}如果索引器要重載的話要定義成object public object this[]{...}索引器的目的是為了方便,可以在該類型的對象後面直接加一對中括弧[]就能訪問類型中的成員內容了索引器最終編譯的時候會產生一個名字叫Item的屬性 //20.請編寫一個類:ItcastClass,該類中有一個私人欄位_names,資料類型為:字串數組,長度為5,並且有5個預設的姓名。要求:為ItcastClass類編寫一個索引器,要求該索引器能夠通過下標來訪問_names中的內容。類: public class ItcastClass { private string[] _names = { "張三", "李四", "王五", "六六", "七七" }; public string this[int index] { get { return _names[index]; } set { _names[index] = value; } } public int Length { get { return _names.Length; } } }主函數: static void Main(string[] args) { ItcastClass ic = new ItcastClass(); for (int i = 0; i < ic.Length; i++) { Console.WriteLine(ic[i]); } Console.ReadKey(); }
20.char.char.isdigit 判斷是否數字char.isLetter 是否字母char.isWhiteSpaca 空格符
21.判斷方法是否重載,只看亮點:1方法名必須一致,2參數必須不一致 22.數組的方法,Array.Reverse()數組的反轉,Array.Sort()數組的排序