1. 結構體,數組。
namespace Struct_array結構體數組{ //結構體的聲明,關鍵字是Struct。 struct people { public char sex; //結構體裡面不但能存放屬性,還能存放方法。 public int age; //如果屬性不用public修飾,外面就不能訪問該屬性。 public void sayHello() { Console.WriteLine("hello,Struct!"); } } class Program { static void Main(string[] args) { people Jim; //使用結構必須先聲明變數。 Jim.age = 11; Jim.sex='男'; Jim.sayHello(); //結構體裡面的方法調用 Console.WriteLine("Jim 今年"+Jim.age+"歲了。"); Program ss = new Program(); ss.arrayexpra(); Console.ReadKey(); } public void arrayexpra() { int[] nums; nums = new int[] { 1, 2, 3 }; int[] names=new int[10]; //聲明一個最大長度為十的數組; int[] ages = { 3,2,1,4,5,8,7}; //直接聲明一個數組。 Array.Sort(ages); //對數組排序 for (int i = 0; i < ages.Length; i++) { Console.WriteLine(ages[i]); } Array.Reverse(ages); //反轉一個數組 for (int i = 0; i < ages.Length; i++) { Console.WriteLine(ages[i]); } } }}
2.函數。
namespace 方法的重載{ class Program { static void Main(string[] args) { Program p = new Program(); Console.WriteLine(p.fun("1",2)); int i,j=100; p.funOut(2, 4, out i); //out參數方法的調用,i可以不付初始值。 Console.WriteLine(i); p.funref(2, 4, ref j); //ref參數方法的調用,j不可以不付初始值。 Console.WriteLine(j); Console.ReadKey(); } int fun(string i, int c) //方法的重載,就是一樣名字的兩個不同方法。這樣做為了方便程式遠記憶繁雜的函數名稱。 { return Convert.ToInt32(i) + c; } string fun(int r, int c, int b) { return (r + c + b).ToString(); } void funOut(int r, int c, out int b)//out參數方法,b必須賦值。 { b = 100; b = r + c + b; } void funref(int r, int c, ref int b) //ref參數方法 { b = r + c + b; } }}