Home Backend Development C#.Net Tutorial C# basic knowledge compilation: basic knowledge (14) array

C# basic knowledge compilation: basic knowledge (14) array

Feb 11, 2017 pm 01:34 PM

No matter which language, there will definitely be the concept of collection. The simplest and most intuitive collection should be an array. An array is a continuous space in memory. Take a look at the definition of array

in C#.
1. int[] intArry;
intArry= new int[6];
Here declares an int array type variable intArry and saves an int array object with 6 units;
int [,] intArry2 = new int[3, 4];
Declare an int two-dimensional array type variable and initialize an array object with 3 rows and 4 columns;
int[][] intArry3 = new int[9 ][];
Declare an array unit as an array variable of int array type. Each array element is an object reference of int array type.
Because it is an object-oriented language, references and objects are mentioned above. In fact:
1. The .net Frameword array is not a simple data structure, but a type, called an array type;
2. The array variable in the .net Framework stores references to array type objects. That is to say, the array is an object.
All .net Framework arrays (int[], string[], object[]) are subclasses inherited from Array. Generally, the Array class is not used directly, because various languages ​​under the .net Framework, including C# of course, map array objects to their own special syntax, such as int[], string[].

The array mainly has two attributes: index and length, that is, index and length. The index is used to access the elements in the array object, and the length is the length of the array.

Look at a piece of contact code:

public class MyArray
    {
        /// <summary>
        /// 定义数组测试
        /// </summary>
        public void TestInt()
        {
            int[] intArry1 = null;

            intArry1 = new int[6];

            int[,] intArry2 = new int[3, 4];

            int[][] intArry3 = new int[9][];
        }
        
        /// <summary>
        /// 值类型数组转引用类型数组测试
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static object[] Int32ToArrayOfObject(int[] array)
        {
            object[] objArray = new object[array.Length];

            for (int i = 0; i < array.Length; i++)
            {
                objArray[i] = array[i];
            }

            return objArray;
        }
        /// <summary>
        /// 数组的主要特性测试
        /// </summary>
        public static void MainTest()
        {
            //声明一个包含是个元素的字符串型数组
            string[] sArray = new string[10];
            //访问数组
            //赋值
            for (int i = 0; i < sArray.Length; i++)
            {
                sArray[i] = @"string" + i;
            }

            ConsoleToClientString(sArray);

            //另一种方式声明数组,所谓的枚举法
            sArray = new string[] { "TestString0", "TestString1", "TestString2" };

            ConsoleToClientString(sArray);

            //数组复制
            string[] newSArray = sArray.Clone() as string[];

            ConsoleToClientString(newSArray);

            //使用Array的CreateInstance方法声明10元素的整形数组
            int[] intArray = Array.CreateInstance(typeof(int), 10) as int[];

            for (int i = 0; i < intArray.Length; i++)
            {
                intArray[i] = i;
            }

            ConsoleToClientInt(intArray);

            //数组之间的复制,指定位置,指定长度
            int[] newIntArray = new int[20];

            Array.Copy(intArray, 3, newIntArray, 4, intArray.Length - 3);

            ConsoleToClientInt(newIntArray);

            object[] objArray = sArray;

            ConsoleToClientObject(objArray);

            objArray = Int32ToArrayOfObject(intArray);

            ConsoleToClientObject(objArray);

            //数组的数组
            int[][] intArrayArray = new int[9][];

            Console.WriteLine("数组长度:" + intArrayArray.Length);

            //赋值
            for (int i = 1; i < 10; i++)
            {
                intArrayArray[i - 1] = new int[i];

                for (int j = 1; j <= i; j++)
                {
                    intArrayArray[i - 1][j - 1] = i * j;
                }
            }

            ConsoleToClientArrayArrayInt(intArrayArray);
            
            //二维数组
            int[,] intArray2D = new int[9, 9];

            Console.WriteLine(string.Format("二维数组 长度:{0},维数:{1}*{2}", intArray2D.Length, 

intArray2D.GetLength(0), intArray2D.GetLength(1)));

            for (int i = 1; i < 10; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    intArray2D[i - 1, j - 1] = i * j;
                }
            }

            int count = 0;

            foreach (int item in intArray2D)
            {
                if (item > 0)
                {
                    Console.Write("{0,2}", item);
                }

                if (++count >= 9)
                {
                    Console.WriteLine();

                    count = 0;
                }
            }
        }

        static void ConsoleToClientArrayArrayInt(int[][] intArrayArray)
        {
            foreach (int[] item1 in intArrayArray)
            {
                foreach (int item2 in item1)
                {
                    Console.Write("{0,2}", item2);
                }

                Console.WriteLine();
            }

            Console.WriteLine();
        }

        static void ConsoleToClientString(string[] sArray)
        {
            foreach (string item in sArray)
            {
                Console.Write(item + @",");
            }

            Console.WriteLine();
        }

        static void ConsoleToClientInt(int[] intArray)
        {
            foreach (int item in intArray)
            {
                Console.Write(item + @",");
            }

            Console.WriteLine();
        }

        static void ConsoleToClientObject(object[] objArray)
        {
            foreach (object item in objArray)
            {
                Console.Write(item.ToString() + @",");
            }

            Console.WriteLine();
        }

    }
Copy after login

Call

    class Program
    {
        static void Main(string[] args)
        {
            MyArray.MainTest();

            Console.ReadLine();
        }
    }
Copy after login

Result


You can know from the above: The array has a reference Type array and value type array. For reference type array, the element is used to save the reference of the object, and the initialization value is null; for value type array, the element saves the value of the

object, and for the numeric type, the initialization value is 0.

Arrays have dimensions, but multidimensional arrays and arrays of arrays are different concepts. intArrayArray and intArray2D are different. The array of array represents an m*n determinant


, and the multidimensional array is an array in which each element is an array object.

Arrays, like other collection classes, implement the ICollection interface and have enumeration and iteration functions.


The above is the compilation of C# basic knowledge: Basic knowledge (14) Array content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

See all articles