C#| How to change the size of one-dimensional array

Array.Resize(T[], Int32) Method is used to resize the number of elements present in the array. Or in other words, this method is used to change the number of elements of a one-dimensional array to the specified new size. It resizes the only 1-D array, not multidimensional array.

Syntax:

public static void Resize<T> (ref T[] array, int newSize);

Parameters:



array: It is a one-dimensional, zero-based array to resize, or null to create a new array with the specified size.
newsize: It is the size of the new array.

Exception:If the value of nsize is less than zero, then this method will give ArgumentOutOfRangeException.

Note: This method is an O(n) operation, where n is new size.

Below given are some examples to understand the implementation in a better way:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to resize the
// elements of the 1-D array
using System;
  
public class GFG {
      
    // Main method
    static public void Main()
    {
  
        // create and initialize array
        string[] myarray = {"C#", "Java", "C++", "Python",
                             "HTML", "CSS", "JavaScript"};
  
        // Display original array before Resize
        Console.WriteLine("Original Array:");
          
        foreach(string i in myarray)
        {
            Console.WriteLine(i);
        }
  
        int len = myarray.Length;
        Console.WriteLine("Length of myarray: "+len);
        Console.WriteLine();
          
        // Resize the element of myarray and 
        // create a new array. Here new array
        // is smaller than the original array 
        // so, elements are copied from the 
        // myarray to the new array until the
        // new one is filled. The rest of the
        // elements in the old array are ignored
        Array.Resize(ref myarray, len - 3);
  
        Console.WriteLine("New array is less than myarray:");
          
        foreach(string j in myarray)
        {
            Console.WriteLine(j);
        }
          
    }
}

chevron_right


Output:

Original Array:
C#
Java
C++
Python
HTML
CSS
JavaScript
Length of myarray: 7

New array is less than myarray:
C#
Java
C++
Python

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to resize the 
// elements of the 1-D array
using System;
  
public class GFG {
      
    // Main method
    static public void Main()
    {
  
        // create and initialize array
        string[] myarray = {"C#", "C++", "Ruby"
                         "Java", "PHP", "Perl"};
  
        // Display original string before Resize
        Console.WriteLine("Original Array:");
          
        foreach(string i in myarray)
        {
            Console.WriteLine(i);
        }
  
        // Length of old array
        int len = myarray.Length;
        Console.WriteLine("Length of myarray: "+len);
        Console.WriteLine();
          
        // Resize the element of myarray 
        // and create a new array
        // Here new array is greater than
        // original array so, all the element 
        // from myarray is copied to new array
        // and remaining will be null
        Array.Resize(ref myarray, 10);
          
        Console.WriteLine("New array is greater than myarray:");
          
        foreach(string j in myarray)
        {
            Console.WriteLine(j);
        }
          
        // Length of new array
        int len1 = myarray.Length;
        Console.WriteLine("Length of New Array: "+len1);
   
    }
}

chevron_right


Output:

Original Array:
C#
C++
Ruby
Java
PHP
Perl
Length of myarray: 6

New array is greater than myarray:
C#
C++
Ruby
Java
PHP
Perl




Length of New Array: 10

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.array.resize?view=netcore-2.1



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.