結構體,數組,方法的重載,ref,out

來源:互聯網
上載者:User

 

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;        }    }}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.