下麵我們繼續學習C#的語法。結構struct,C#中的結構和我們PLC中建立的UDT(結構體)是一樣的。裡面存儲了相關的不同類型的數據。 有一句話我覺得十分重要:方法是依存於結構和對象存在的。這以後我們會個更加深入的學習的。 Struct結構: 可以幫助我們一次性聲明不同類型的變數。 語法: [pu ...
下麵我們繼續學習C#的語法。結構struct,C#中的結構和我們PLC中建立的UDT(結構體)是一樣的。裡面存儲了相關的不同類型的數據。
有一句話我覺得十分重要:方法是依存於結構和對象存在的。這以後我們會個更加深入的學習的。
Struct結構:
可以幫助我們一次性聲明不同類型的變數。
語法:
[public] struct 結構名
{
成員;
}
如下例聲明:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 public struct Person 12 { 13 public string name; 14 public int age; 15 public char gender; 16 } 17 static void Main(string[] args) 18 { 19 Person zsPerson; 20 zsPerson.name = "張三"; 21 zsPerson.age = 18; 22 zsPerson.gender = '男'; 23 24 Console.ReadKey(); 25 } 26 } 27 }
值得我們註意的是,在聲明結構的時候,如果我們沒加public,我們是建立不了給結構賦值的,不加public系統預設為private私有的。並且我們在命名空間之下Main之上創建的變數其實不叫變數,而是叫欄位。
變數和欄位的區別在於:變數可以存一個值,之後不停被覆蓋,而欄位類似我們PLC背景數據,可以存儲若幹個數值。
而且我在這要提出一個問題,我看了幾個視頻和數據,對於欄位的命名說法不一樣的,總結如下
(1)欄位和變數要區別命名,例如:_Name
(2)也有反對這種命名方式的,理由是:在複雜的編程任務中,可能影響與其他語言的交互引用的作用,例如VB。net。
這在以後深入學習過程中我們在慢慢體會,也歡迎大神們給我解惑。
數組
一次性存儲多個相同類型的變數。
語法:
數組的類型[] 數組名 = new 數組類型[數組長度];
數組的長度一旦固定了,就不能在被改變了。
對於int[]類型的數組,初值為0,string[]數組初值為null,bool[]數組初值為false。
下麵我們介紹幾種聲明數組的方式
int[] nums = new int[10]; //沒有聲明數組元素,推薦
int[] nums = {1,2,3,4,5,6}; //隱式聲明瞭元素和長度,推薦
int[] nums = new int[3]{1,2,3}; //不推薦,麻煩且長度和元素數量必須一致。
int[] nums = new int[]{1,2,3,4,5}; //類似第2種
下麵看一個練習1:從一個整數數組中求出最大值,最小值,總和和平均值。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] nums = { 1,2,3,4,5,6,7,8,9,0}; 14 int max = nums[0]; 15 int min = nums[0]; 16 int sum = 0; 17 18 for (int i = 0; i < nums.Length; i++) 19 { 20 if (nums[i] > max) 21 { 22 max = nums[i]; 23 } 24 25 if (nums[i] < min) 26 { 27 min = nums[i]; 28 } 29 sum += nums[i]; 30 } 31 Console.WriteLine($"這個數組的最大值是{max},最小值是{min},總和是{sum},平均值是{sum/nums.Length}"); 32 Console.ReadKey(); 33 } 34 } 35 }
練習2:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string[] names = { "老楊","老蘇","老鄒","老虎","老牛","老馬"}; 14 string str = null; 15 16 for (int i = 0; i < names.Length-1; i++) 17 { 18 str += names[i] + "|"; 19 } 20 Console.WriteLine(str+names[names.Length-1]); 21 Console.ReadKey(); 22 } 23 } 24 }
練習3:對一個整數數組做如下處理:若元素為正數將這個元素+1,若為負數,將這個元素-1,元素為0,不變。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] nums = { 1,-2,3,-4,5,6,0}; 14 for (int i = 0; i < nums.Length; i++) 15 { 16 if (nums[i] > 0) 17 { 18 nums[i] += 1; 19 } 20 else if (nums[i] < 0) 21 { 22 nums[i] -= 1; 23 } 24 else 25 { 26 27 } 28 } 29 30 for (int i = 0; i < nums.Length; i++) 31 { 32 Console.WriteLine(nums[i]); 33 } 34 Console.ReadKey(); 35 } 36 } 37 }
練習4:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string[] names = { "我","是","好人"}; 14 for (int i = 0; i < names.Length/2; i++) 15 { 16 string temp = names[i]; 17 names[i] = names[names.Length - 1 - i]; 18 names[names.Length - 1 - i] = temp; 19 } 20 for (int i = 0; i < names.Length; i++) 21 { 22 Console.Write(names[i]); 23 } 24 Console.ReadKey(); 25 } 26 } 27 }
練習5:冒泡排序:就是將一個數組中的元素從大到小,從小到大排列。
分析:需要兩個迴圈,外層迴圈,控制比較次數,內層迴圈,控制交換次數。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int[] nums = { 9,8,7,6,5,4,3,2,1,0}; 14 for (int i = 0; i < nums.Length-1; i++) 15 { 16 for (int j = 0; j < nums.Length-1-i; j++) 17 { 18 if (nums[j] > nums[j+1]) 19 { 20 int temp = nums[j]; 21 nums[j] = nums[j + 1]; 22 nums[j + 1] = temp; 23 } 24 } 25 } 26 for (int i = 0; i < nums.Length; i++) 27 { 28 Console.WriteLine(nums[i]); 29 } 30 Console.ReadKey(); 31 } 32 } 33 }
這裡面有一點值得我們註意,C#中的數組下標和我們PLC中數組下標正好相反,C#中數組下標的0從左面元素開始計算。
其實,這種冒泡方式的寫法也就在面試的時候會用到,在我們C#中,可以直接用一個方法解決Array.Sort();(只能升序)
Array.Reverse();(反轉排列)若想降序:先調用Array.Sort();後調用Array.Reverse()。