Array resolution for lower bound non-zero in C #

Source: Internet
Author: User
When it comes to arrays, it is estimated that most programmers will directly say that the array starts from 0 when asked what number the array is from. This answer is certainly not wrong, now let's look at the lower bound in C # is not 0 array.

Let's take a look at an array of related descriptions:

1. Arrays: A mechanism that allows multiple data items to be treated as a collection.

2. Classification of arrays: In the CLR, arrays can be divided into one-dimensional arrays, multidimensional arrays, and jagged arrays.

3. Type of array: Since all arrays are inherited from the abstract type System.Array, and this type is inherited from System.Object, this means that the array is a reference type.

When creating an array, in addition to the array elements, the block of memory occupied by the array object also contains a type object pointer, a synchronous index block, and an additional member. The above classification of the array refers to "jagged array", because the CLR supports jagged arrays, so in C # can implement a jagged array, a jagged array is an array of arrays, in accessing the elements of a jagged array means that must be two or more array access.

When an array is passed as an argument to a method when it is related to a group, it is actually passing a reference to the array, so the called method can modify the elements in the array. (If you do not want to be modified, you must generate a copy of the array and pass the copy to the method.) )

Here's a way to convert an array to a DataTable:

////// integer two-dimensional array converted to DataTable //////////////
 
   
   
         Public DataTable dyadicarraytodatatable (int[,] intdyadicarray, out string messageout, params object[] D            Atatablecolumnsname) {var returndatatable = new DataTable (); Verify that the column matches the passed-in character if (datatablecolumnsname.length! = intdyadicarray.getlength (1)) {Me                Ssageout = "The number of DataTable columns is not the same as the number of two-dimensional array columns, please adjust columns";            return returndatatable;                }//Add column for (var datatablecolumnscount = 0;                Datatablecolumnscount < Datatablecolumnsname.length; datatablecolumnscount++) {RETURNDATATABLE.COLUMNS.ADD (Datatablecolumnsname[datatablecolumnscoun T].            ToString ()); }//Add line 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);            }//Return hint with datatable messageout = "DataTable successfully converted";        return returndatatable; }

The above is to convert an array of integers into a DataTable operation method, as for other types, such as Byte, floating-point type conversion, modify the relevant parameters can also be the parameter type of the corresponding modification, here does not do a detailed introduction.

Next, let's take a look at the "lower bound non-zero group" knowledge:

Lower bound non-zero group due to the performance of not better optimization, so in general use will be less, if you do not care about the performance loss or need to cross-language porting, you can consider using non-zero groups. The concept of "lower bound non-zero group" is not introduced, as its name shows.

Created using the CreateInstance () method of array in C #, this method has several overloads that allow you to specify the array element type, the array dimension, the lower bound of each dimension, and the number of elements for each dimension.

When you call CreateInstance (), the array allocates memory, saves the parameter information to the cost portion of the array's memory, and then returns a reference to the logarithmic group.

Next look at the underlying implementation code for this method:

[system.security.securitysafecritical]//auto-generated public unsafe static Array CreateInstance (Type elementtype, int length) {if (object) ElementType = = null) t            Hrow new ArgumentNullException ("ElementType"); if (length < 0) throw new ArgumentOutOfRangeException ("Length", Environment.getresourcestring ("Argumento             Utofrange_neednonnegnum "));            Contract.ensures (contract.result () = null); Contract.ensures (Contract.result ().             Length = = length); Contract.ensures (Contract.result ().             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); }

To see the above code, you should have a general understanding of the creation of non-zero cardinality groups, and then look at the underlying code of the ensures () method in detail:

public static void ensures (bool condition)        {            assertmustuserewriter (contractfailurekind.postcondition, " Ensures ");         }
[securitysafecritical] static partial void Assertmustuserewriter (Contractfailurekind kind, String contractkind) {if (_assertingmustuserewriter) System.Diagnostics.Assert.Fail ("asserting that we must us             E 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; }

The creation of non-zero cardinality groups is not an in-depth introduction, and if needed, the corresponding version implementation can be selected based on the provided method overloads.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.