Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Visual C # array

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the Visual C # array, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Storing related data item sets is a basic requirement of most software applications; this can be achieved by using Visual C # arrays and collections.

Visual C # array

An array is a collection of objects of the same type. Because arrays can be of almost any length, you can use arrays to store thousands or even millions of objects, but you must determine the size of the array when you create it. Each item in the array is accessed by index, which is a number indicating where or where the object is stored in the array. Arrays can be used to store both reference types and value types.

One-dimensional array

An array is an indexed collection of objects. The declaration of an one-dimensional object array is as follows:

Type [] arrayName

The elements in the array are generally initialized at the same time, as follows:

C#

Int [] array = new int [5]

The default value of a numeric array element is zero, and the default value of a reference element is null, but you can initialize the value during the creation of the array, as follows:

C#

Int [] array1 = new int [] {1,3,5,7,9}

Or even initialize like this:

C#

Int [] array2 = {1,3,5,7,9}

The index of the array starts at zero, so the * * elements in the array are element 0.

C#

String [] days = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"}; System.Console.WriteLine (days [0]); / / Outputs "Sun"

Multidimensional array

Conceptually, a two-dimensional array is similar to a grid and a three-dimensional array is similar to a cube.

C#

Staggered array

One variant of a multidimensional array is an interlaced array, an array made up of arrays. An interlaced array is an one-dimensional array, and each element itself is an array. Arrays as elements do not need to be the same size.

The interlaced array is declared as follows:

C#

Int [] [] jaggedArray = new int [3] []

Doing so creates an array of three arrays. These arrays can be initialized as follows:

C#

JaggedArray [0] = new int [5]; jaggedArray [1] = new int [4]; jaggedArray [2] = new int [2]

Use the foreach statement

Foreach statements are typically used to access each element stored in an array:

C#

Object array

The process of creating an array of objects rather than an array of simple data types such as integers is divided into two parts. First declare the array, and then you must create the objects stored in the array. This example creates a class that defines an audio CD. Then create an array of 20 audio CD.

C#

The Visual C # array is just one of many options for using C # to store datasets. The specific choice depends on a number of factors, such as the operation or the way the item is accessed. For example, if you need to insert items at the beginning or middle of the collection, the list is generally faster than the array. Other types of collection classes include mappings, trees, and stacks, each with its own advantages. For more information, see System.Collections and System.Collections.Generic.

The following example shows how to use List

< (Of < (T>

) >) class. Note that unlike the Array class, items can be inserted in the middle of the list. This example restricts that items in the list must be strings.

C#

Thank you for reading this article carefully. I hope the article "how to use Visual C # Array" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report