亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

C#中下限非零的數(shù)組解析

Original 2016-11-10 14:47:35 634
abstract:談到數(shù)組時,當被問及數(shù)組是從什么數(shù)開始時,估計大部分程序員都會直接說出數(shù)組當然是從0開始的。這個回答當然沒有錯,現(xiàn)在我們就來了解一下C#中的下限非0的數(shù)組。  首先看一下數(shù)組的相關(guān)介紹:      1.數(shù)組:是允許將多個數(shù)據(jù)項當作一個集合來處理的機制。      2.數(shù)組的分類:在CLR中,數(shù)組可分為一維數(shù)組,多維數(shù)組,交錯數(shù)組

談到數(shù)組時,當被問及數(shù)組是從什么數(shù)開始時,估計大部分程序員都會直接說出數(shù)組當然是從0開始的。這個回答當然沒有錯,現(xiàn)在我們就來了解一下C#中的下限非0的數(shù)組。

  首先看一下數(shù)組的相關(guān)介紹:

      1.數(shù)組:是允許將多個數(shù)據(jù)項當作一個集合來處理的機制。

      2.數(shù)組的分類:在CLR中,數(shù)組可分為一維數(shù)組,多維數(shù)組,交錯數(shù)組。

      3.數(shù)組的類型:由于所有的數(shù)組都是繼承自System.Array這個抽象類型,而這個類型又是繼承自System.Object,這就說明數(shù)組是引用類型。

  在創(chuàng)建數(shù)組時,除了有數(shù)組元素,數(shù)組對象占據(jù)的內(nèi)存塊還包含一個類型對象指針,一個同步索引塊和一個額外的成員。上面對數(shù)組的分類中提到“交錯數(shù)組”,由于CLR支持交錯數(shù)組,所以在C#中可以實現(xiàn)交錯數(shù)組,交錯數(shù)組即由數(shù)組構(gòu)成的數(shù)組,在訪問交錯數(shù)組的元素意味著必須進行兩次或多次數(shù)組訪問。

  在對數(shù)組進行相關(guān)操作的過程中,數(shù)組作為實參傳給一個方法時,實際傳遞的是對該數(shù)組的引用,因此被調(diào)用的方法能夠修改數(shù)組中的元素。(如果不想被修改,必須生成數(shù)組的一個拷貝,并將這個拷貝傳給方法。)

  下面介紹一種將數(shù)組轉(zhuǎn)化為DataTable的方法:

  


  /// <summary>
        /// 整數(shù)型二維數(shù)組轉(zhuǎn)換成DataTable        /// </summary>
        /// <param name="intDyadicArray"></param>
        /// <param name="messageOut"></param>
        /// <param name="dataTableColumnsName"></param>
        /// <returns></returns>
        public DataTable DyadicArrayToDataTable(int[,] intDyadicArray, out string messageOut,            params object[] dataTableColumnsName)
        {            var returnDataTable = new DataTable();            //驗證列與所傳入的字符是否相符
            if (dataTableColumnsName.Length != intDyadicArray.GetLength(1))
            {
                messageOut = "DataTable列數(shù)與二維數(shù)組列數(shù)不符,請調(diào)整列數(shù)";                return returnDataTable;
            }            //添加列
            for (var dataTableColumnsCount = 0;
                dataTableColumnsCount < dataTableColumnsName.Length;
                dataTableColumnsCount++)
            {
                returnDataTable.Columns.Add(dataTableColumnsName[dataTableColumnsCount].ToString());
            }            //添加行
            for (var dyadicArrayRow = 0; dyadicArrayRow < intDyadicArray.GetLength(0); dyadicArrayRow++)
            {                var addDataRow = returnDataTable.NewRow();                for (var dyadicArrayColumns = 0;
                    dyadicArrayColumns < intDyadicArray.GetLength(1);
                    dyadicArrayColumns++)
                {
                    addDataRow[dataTableColumnsName[dyadicArrayColumns].ToString()] =
                        intDyadicArray[dyadicArrayRow, dyadicArrayColumns];
                }
                returnDataTable.Rows.Add(addDataRow);
            }            //返回提示與DataTable
            messageOut = "DataTable成功轉(zhuǎn)換";            return returnDataTable;
        }


     以上是將整數(shù)數(shù)組轉(zhuǎn)化為DataTable的操作方法,至于其他類型,如字節(jié),浮點型等類型的轉(zhuǎn)化,修改相關(guān)參數(shù)即可,也可將參數(shù)類型進行對應的修改,這里就不做詳細介紹了。

     接下來我們具體來了解一下“下限非零數(shù)組”的相關(guān)知識:

       下限非零數(shù)組由于在性能上沒有做更好的優(yōu)化,因此在一般的使用中會較少,如果不計較性能損失或者需要跨語言移植,可以考慮使用非零數(shù)組?!跋孪薹橇銛?shù)組”的概念就不做介紹,正如其名稱所見。

      C#中使用Array的CreateInstance()方法進行創(chuàng)建,此方法有若干個重載,允許指定數(shù)組元素類型,數(shù)組維數(shù),每一維的下限和每一維的元素數(shù)目。

      在調(diào)用CreateInstance()時,為數(shù)組分配內(nèi)存,將參數(shù)信息保存到數(shù)組的內(nèi)存的開銷部分,然后返回對數(shù)組的一個引用。

      接下來看一下此方法的底層實現(xiàn)代碼:

[System.Security.SecuritySafeCritical]  // auto-generated 
        public unsafe static Array CreateInstance(Type elementType, int length)
        { 
            if ((object)elementType == null)                throw new ArgumentNullException("elementType");            if (length < 0)                throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); 
            Contract.Ensures(Contract.Result<Array>() != null);
            Contract.Ensures(Contract.Result<Array>().Length == length); 
            Contract.Ensures(Contract.Result<Array>().Rank == 1); 
            Contract.EndContractBlock();
 
            RuntimeType t = elementType.UnderlyingSystemType as RuntimeType;            if (t == null)                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"elementType");            return InternalCreate((void*)t.TypeHandle.Value,1,&length,null); 
        }


   看到以上的代碼,應該對非零基數(shù)組的創(chuàng)建有一個大致了解,接下來具體看一下Ensures()方法的底層代碼:

public static void Ensures(bool condition)
       {
           AssertMustUseRewriter(ContractFailureKind.Postcondition, "Ensures"); 
       }
static partial void AssertMustUseRewriter(ContractFailureKind kind, String contractKind); 

       [SecuritySafeCritical]        static partial void AssertMustUseRewriter(ContractFailureKind kind, String contractKind) 
       {            if (_assertingMustUseRewriter) 
               System.Diagnostics.Assert.Fail("Asserting that we must use the rewriter went reentrant.", "Didn't rewrite this mscorlib?"); 
           _assertingMustUseRewriter = true;
           Assembly thisAssembly = typeof(Contract).Assembly;  
           StackTrace stack = new StackTrace(); 
           Assembly probablyNotRewritten = null;            for (int i = 0; i < stack.FrameCount; i++) 
           { 
               Assembly caller = stack.GetFrame(i).GetMethod().DeclaringType.Assembly;                if (caller != thisAssembly) 
               {
                   probablyNotRewritten = caller;                    break;
               } 
           } 
           if (probablyNotRewritten == null) 
               probablyNotRewritten = thisAssembly;
           String simpleName = probablyNotRewritten.GetName().Name; 
           System.Runtime.CompilerServices.ContractHelper.TriggerFailure(kind, Environment.GetResourceString("MustUseCCRewrite", contractKind, simpleName), null, null, null);

           _assertingMustUseRewriter = false;
       }


     有關(guān)非零基數(shù)組的創(chuàng)建方法就不做深入的介紹,如果需要使用,可以根據(jù)提供的方法重載選擇對應的版本實現(xiàn)。


Release Notes

Popular Entries