建立基於棧的數組

來源:互聯網
上載者:User
using System;namespace exercise {    class Program {        static unsafe void Main(string[] args) {            //建立基於棧的數組            decimal* pDecimals = stackalloc decimal[10];            int size;            size = 20; //or some other value calculated at runtime            double* pDoubles = stackalloc double[size];            //把第一個元素(數組元素0)設定為3.0            *pDoubles = 3.0; // pDoubles[0] is the same as *pDoubles            //把數組的第二個元素(元素編號1)設定為8.4            *(pDoubles + 1) = 8.4; // pDoubles[1] is the same as *(pDoubles +1)            //可以用運算式 *(pDoubles + X) 訪問數組中下標為X的元素。            //如果p是任意指標類型,X是一個整數,運算式 p[X]就會被編譯器解釋為*(p + X),這適合於所有指標。            //下面的代碼會拋出一個異常,拋出異常的原因是:使用越界的下標來訪問數組            double[] myDoubleArray = new double[20];            myDoubleArray[50] = 3.0;            //如果使用stackalloc聲明一個等價的數組,對數組進行辯解檢查時,這個數組中就沒有封裝任何對象,            //因此下面的代碼不會拋出異常:            pDoubles[50] = 3.0;        }    }}

--摘自《C#進階編程》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.