C#

Array

Generic Class, Method

Int, uint

Array Length

using System;
using System.Buffers;
class Program
{
static void UseSharedArray(int x)
{
*// Part 1: reference the ArrayPool and Rent an array.*
var shared = ArrayPool<int>.**Shared**;
var rentedArray = shared.**Rent**(5);
*// Part 2: store values in rented array and print them.*
rentedArray[0] = 0;
rentedArray[1] = 1 * x;
rentedArray[2] = 2 * x;
rentedArray[3] = 3 * x;
rentedArray[4] = 4 * x;
*// ... Print integers.*
for (int i = 0; i < 5; i++)
{
Console.WriteLine(*"ARRAY {0}: {1}"*, x, rentedArray[i]);
}
*// Part 3: return the rented array.*
shared.**Return**(rentedArray);
}
static void Main()
{
UseSharedArray(1);
UseSharedArray(2);
UseSharedArray(3);
}
}ARRAY 1: 0
ARRAY 1: 1
ARRAY 1: 2
ARRAY 1: 3
ARRAY 1: 4
ARRAY 2: 0
ARRAY 2: 2
ARRAY 2: 4
ARRAY 2: 6
ARRAY 2: 8
ARRAY 3: 0
ARRAY 3: 3
ARRAY 3: 6
ARRAY 3: 9
ARRAY 3: 12

Static Array

using System;
class Program
{
static int[] _shared = new int[5];
static void UseStaticArray(int x)
{
var rentedArray = _shared;
rentedArray[0] = 0;
rentedArray[1] = 1 * x;
rentedArray[2] = 2 * x;
rentedArray[3] = 3 * x;
rentedArray[4] = 4 * x;
*// ... Print integers.*
for (int i = 0; i < 5; i++)
{
Console.WriteLine(*"ARRAY {0}: {1}"*, x, rentedArray[i]);
}
}
static void Main()
{
UseStaticArray(1);
UseStaticArray(2);
UseStaticArray(3);
}
}ARRAY 1: 0
ARRAY 1: 1
ARRAY 1: 2
ARRAY 1: 3
ARRAY 1: 4
ARRAY 2: 0
ARRAY 2: 2
ARRAY 2: 4
ARRAY 2: 6
ARRAY 2: 8
ARRAY 3: 0
ARRAY 3: 3
ARRAY 3: 6
ARRAY 3: 9
ARRAY 3: 12

Static

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on Dec 7, 2022 (simplify).