What are the element types of jagged arrays in C#?

王林
Release: 2023-08-31 18:25:02
forward
649 people have browsed it

C# 中交错数组的元素类型是什么?

A jagged array is an array of arrays, so its elements are reference types and initialized to null.

Let's see how to use a jagged array -

Declare a jagged array-

int [][] marks;
Copy after login

Now, let's initialize it where marks is an integer consisting of 5 Array-

int[][] marks = new int[][]{new int[]{ 40,57 },new int[]{ 34,55 }, new int[]{ 23,44 },new int[]{ 56, 78 }, new int[]{ 66, 79 } };
Copy after login

Now let’s see a complete example of jagged array in C# and understand how to implement it-

Example

Live Demonstration

using System;

namespace MyApplication {
   class MyDemoClass {
      static void Main(string[] args) {
         int i, j;
         int[][] marks = new int[][] {
            new int[] {
               90,
               95
            }, new int[] {
               89,
               94
            }, new int[] {
               78,
               87
            }, new int[] {
               76,
               68
            }, new int[] {
               98,
               91
            }
         };

         for (i = 0; i < 5; i++) {
            for (j = 0; j < 2; j++) {
               Console.WriteLine("marks[{0}][{1}] = {2}", i, j, marks[i][j]);
            }
         }
         Console.ReadKey();
      }
   }
}
Copy after login

Output

marks[0][0] = 90
marks[0][1] = 95
marks[1][0] = 89
marks[1][1] = 94
marks[2][0] = 78
marks[2][1] = 87
marks[3][0] = 76
marks[3][1] = 68
marks[4][0] = 98
marks[4][1] = 91
Copy after login

The above is the detailed content of What are the element types of jagged arrays in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!