C#基础系列第一篇
前言: 完全是我在學習過程中記錄的筆記,只不過分享一下讓很多剛開始學習.net編程的人能夠很快的學會C#語言
(1) 凡是由.net生成的代碼,需要CLR運行的代碼均為托管代碼
(2) 不由CLR進行維護的執行代碼為非托管代碼
?(1)舉例說明:
??????????? static void Main(string[] args)
??????? {
??????????? for (int i = 0; i < 10; i++)
??????????? {
??????????????? if (i == 2)
??????????????? {
??????????????????? //break;
??????????????????? continue;
??????????????? }
??????????? }
??????? }
???? 注:在這里可以使用調試的功能看一下結構
??int num1 = 5;
????????????? int num2 = 10;
?????? (1)第一種寫法(建議使用這種最好)
????????????? int temp = num1;
????????????? num1 = num2;
????????????? num2 = temp;? ?
?????? (2)第二種寫法
????????????? num1 = num1 + num2;
??????? num2 = num1 - num2;
??????? num1 = num1 - num2;
?????? (3)第三種寫法
????????????? num1 = num1 | num2;
??????? num2 = num1 ^ num2;
??????? num1 = num1 ^ num2;
?????? (4)第四種寫法(只有基于堆棧的編譯起中可以執行)
?????? ?????? num1 = num2 + (num2 = num1) * 0;
?????? (5)第五種寫法(使用這種寫法的時候必須要保證兩個變量都不能為0)
????????????? num1 = num1 * num2;
??????? num2 = num1 / num2;
??????? num1 = num1 / num2;
?????? Console.WriteLine("num1={0},num2={1}", num1, num2);
(1) 沒有參數和返回值的方法
??????????? 語法:[public] void 方法的名字()
??????????? {
?????????????????? //方法體
??????????? }
??????????? public static void Func()
??????? {
??????????? Console.WriteLine("我要做網站了");
??????? }
???? (2)如果是Main方法調用該方法,現在要在方法前加上static(標有static(靜態)的方法只能直接調用)
???? (3)帶有參數的方法
??????????? 語法:public static void 方法名(方法類型 參數的變量名)
??????????? {
?????????????????? //方法體
??????????? }
??????????? public static void FuncReplace(int money)
??????? {
??????????? if (money >= 1000)
??????????? {
??????????????? Console.WriteLine("可選擇的網站");
??????????? }
??????????? else if (money >= 500)
??????????? {
??????????????? Console.WriteLine("簡單的網站");
??????????? }
??????????? else
??????????? {
??? ????????????Console.WriteLine("資金短缺");
??????????? }
??????? }
???? (4)帶有參數具有返回值的方法
??????????? 語法:[public] static 返回值類型 方法名(參數類型 參數)
??????????? {
?????????????????? //
??????????? }
??????????? public static int GetNum(int num)
??????? {
??????????? return num * 2;
??????? }
??????????? 執行方法
??????????? int res= GetNum(3);
??????? Console.WriteLine(res);
???? (5)實現Max方法與Min方法
??????????? class Program
??????????? {
?????????????????? static void Main(string[] args)
?????????????????? {
????????????????????????? int num1 = 12;
????????????????????????? int num2 = 34;
????????????????????????? int max = Max(num1, num2);
????????????????????????? int min = Min(num1, num2);
????????????????????????? Console.WriteLine("較大的數字是:{0}",max);
????????????????????????? Console.WriteLine("較小的數字是:{0}",min);
????????????????????????? Console.ReadKey();
?????????????????? }
?????????????????? public static int Max(int n1, int n2)
?????????????????? {
????????????????????????? /*
????????????????????????? if (n1 > n2)
????????????????????????? {
???????????????????????????????? return n1;
????????????????????????? }
????????????????????????? else
????????????????????????? {
???????????????????????????????? return n2;
????????????????????????? }
????????????????????????? ?*/
????????????????????????? int num = 0;
????????????????????????? if (n1 > n2)
????????????????????????? {
???????????????????????????????? num = n1;
????????????????????????? }
????????????????????????? else
????????????????????????? {
???????????????????????????????? num = n2;
????????????????????????? }
????????????????????????? return num;
?????????????????? }
?????????????????? public static int Min(int n1, int n2)
?????????????????? {
????????????????????????? return n1 > n2 ? n2 : n1;? //三元表達式
?????????????????? }
??????????? }????
???? (6)求Min和max之間的奇數的和
??????????? class Program
??????????? {
?????????????????? static void Main(string[] args)
?????????????????? {
?????????????????? ?????? int res = SumOdd(3, 7);
????????????????????????? Console.WriteLine(res);
????????????????????????? Console.ReadKey();
?????????????????? }
?????????????????? public static int SumOdd(int min, int max)
?????????????????? {
????????????????????????? int res = 0;
????????????????????????? for (int i= min; i <= max; i++)
????????????????????????? {
???????????????????????????????? if (i % 2 != 0)
???????????????????????????????? {
??????????????????????????????????????? res += i;
???????????????????????????????? }
????????????????????????? }
????????????????????????? return res;
?????????????????? }
??????????? }
???? (7)求最大數
??????????? class Program
??????????? {
?????????????????? static void Main(string[] args)
?????????????????? {
????????????????????????? int[] nums = { 12, 34, 54, 65, 76,454 };
????????????????????????? int res = GetNUum(nums);
????????????????????????? Console.WriteLine(res);
????????????????????????? Console.ReadKey();
?????????????????? }
?????????????????? public static int GetNUum(int[] nums)
?????????????????? {
????????????????????????? int max = 0;
????????????????????????? for (int i = 0; i < nums.Length; i++)
????????????????????????? {
???????????????????????????????? if (nums[i] > max)
???????????????????????????????? {
??????????????????????????????????????? max = nums[i];
???????????????????????????????? }
????????????????????????? }
????????????????????????? return max;
????????????????????????? //return nums.Max();
?????????????????? }
??????????? }
???? (8)寫三個方法,分別完成
??????????? 1、不帶參數,完成任意整數的輸入,如果出現錯誤提示,并繼續輸入,返回整數
?????????????????? int InputNum_1()
??????????? 2、帶一個參數,完成0到該數范圍的整數輸入
?????????????????? int InPutNum_2(int max)
??????????? 3、帶兩個參數,完成這兩個數字之間的整數輸入
?????????????????? int InPutNum_3(int min, int max)
class Program{static void Main(string[] args){Console.Write("請輸入一個數字:");int num = GetInputNum(9,17);Console.WriteLine(num * 2);Console.ReadKey();}/// <summary>/// 完成任意int范圍的數字的輸入并返回/// </summary>/// <returns></returns>public static int GetInputNum(){return GetInputNum(int.MinValue, int.MaxValue);}/// <summary>/// 完成int類型數據的輸入,并返回,要求輸入的數字在0到給定的數字之間/// </summary>/// <param name="max">給定的數字的上限</param>/// <returns></returns>public static int GetInputNum(int max){return GetInputNum(0, max);}/// <summary>/// 完成int數字的輸入,要求在給定范圍之間/// </summary>/// <param name="min">給定范圍的下線</param>/// <param name="max">給定范圍的上線</param>/// <returns></returns>public static int GetInputNum(int min,int max){string str = Console.ReadLine();int num;while (true){try{num = Convert.ToInt32(str);if (num > min && num < max){break;}Console.Write("輸入數字不再{0}到{1}之間,請重新輸入", min,max);str = Console.ReadLine();}catch{Console.Write("輸入有誤,請重新輸入");str = Console.ReadLine();}}return num;}}
(1) 如何創建自己的代碼段
???? 1)工具->代碼段管理器->切換到C#->復制路徑在我的電腦中用->然后可以仿照本機自帶的代碼段修改成一個文件放到桌面上
(2)如何添加自己定義的代碼段
???? 2)打開原來的界面->導入->放在自己的文件下->完成
(1) 重載就是將同一類型或者完成類似功能的不同方法,統一成同一個方法名,方便程序員進行調用
(2)方法重載本質上是不同的方法,只不過方法名相同,那么由方法調用時提供的參數有編譯器進行自動的判斷
(3)構成重載的條件
???? 1)去掉方法名前的所有內容
???? 2)去掉方法體
???? 3)去掉方法參數的參數名,留下類型名
???? 4)比較,可以重合的不能構成重載
(1) 語法:
??????????? [public] enum 枚舉名
??????????? {
?????????????????? //枚舉成員
??????????? }
?(2)如何定義枚舉
?(3)枚舉是一個類型(*)
?(4)如何聲明枚舉變量
?(5)如何為枚舉變量賦值與使用
???? ??? public enum Direction
??????????? {
?????????????????? 東,
?????????????????? 南,
?????????????????? 西,
?????????????????? 北
??????????? }
??????????? class Program
??????????? {
?????????????????? static void Main(string[] args)
?????????????????? {
????????????????????????? Direction dir = Direction.北;
????????????????????????? Console.WriteLine((Direction)3);
????????????????????????? Console.ReadKey();
?????????????????? }
??????????? }
(1) 語法:
??????????? [public] struct 結構名
??????????? {
?????????????????? //結構成員
??????????? }
??????????? 結構成員可以是"字段","方法","構造函數"
???? (2)結構可以定義方法
???? (3)結構字段如果需要外部訪問必須添加public
???? (4)不允許顯示的定義無參構造方法
???? (5)定義構造方法必須為每一個字段賦值
???? (6)提供有參構造方法后,無參構造方法依舊存在
???? (7)聲明結構變量的時候,可以不使用構造方法
??????????? 1)如果不使用構造方法,那么只能調用已經顯示賦值的字段
??????????? 2)結構的構造方法就是為了給字段賦初值的
???? (8)結構的成員不允許有初始值,除非是static或者consit修飾的
???? (9)結構可以繼承自結構
???? (10)一般情況不需要面向對象特征的使用結構
???? (11)一般情況封裝少量字段的時候,使用結構
(1) 如何創建
???? 1)新建項目
2)拖控件(修改屬性)
???? 3)添加事件
??????????? 因為執行某個動作而觸發的方法
?(2)Winform
???? 1)改變界面(一般都是通過)
???? 2)業務邏輯
?(3)關于窗體的位置(控制窗體的位置)
???? 1)設定窗體的StartPosition屬性
?(4)鼠標移動事件
???? 在MouseMove事件下面寫入如下代碼:Text="X="+e.X+",Y="+e.Y;
?(5)窗體滾動
? ? ??
public partial class Form1 : Form{public Form1(){InitializeComponent();StartPosition = FormStartPosition.Manual;Location = new Point(0, 100);}int step = 5;int dis = 1;private void timer1_Tick(object sender, EventArgs e){//循環實現//if (Location.X > 1366)//{// Location = new Point(0 - Size.Width, Location.Y);//}//Location = new Point(Location.X + 5, Location.Y);//過去回來//if (Location.X > 1366 - Size.Width || Location.X < 0)//{// step = -step;//}//Location = new Point(Location.X + step, Location.Y);//兩邊快,中間慢//判斷我的窗體距離邊距多大int left = Location.X;int right = 1366 - Location.X - Size.Width;if (Location.X > 1366 - Size.Width || Location.X < 0){step = -step; }if (step > 0){//右飄dis = right / 10 == 0 ? 1 : right / 10;}else{//左飄dis = left / 10 == 0 ? 1 : left / 10;}Location = new Point(Location.X + step*dis, Location.Y);}}
轉載于:https://www.cnblogs.com/hanyinglong/archive/2012/09/30/2709165.html
總結
- 上一篇: [读书] Computer Vision
- 下一篇: 数据结构——折半查找