TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# ArraySegment Example

This C# program uses the ArraySegment struct. ArraySegment stores information about ranges in arrays.

ArraySegment. Often we need to track ranges in arrays.

For example, part of an array might be used for a different purpose than other parts. ArraySegment is a generic struct that stores information about an array range.

Generic ClassStruct

Example. The ArraySegment must have a type parameter specified. The type parameter must be equivalent to the element type of the array. Once you construct the ArraySegment, you can read the Array, Offset, and Count to retrieve its fields.

Here: We use the original array, the Offset and Count properties, and loop through the elements specified in the ArraySegment.

C# program that uses the ArraySegment type

using System;

class Program
{
    static void Main()
    {
	// Create an ArraySegment from this array.
	int[] array = { 10, 20, 30 };
	ArraySegment<int> segment = new ArraySegment<int>(array, 1, 2);

	// Write the array.
	Console.WriteLine("-- Array --");
	int[] original = segment.Array;
	foreach (int value in original)
	{
	    Console.WriteLine(value);
	}

	// Write the offset.
	Console.WriteLine("-- Offset --");
	Console.WriteLine(segment.Offset);

	// Write the count.
	Console.WriteLine("-- Count --");
	Console.WriteLine(segment.Count);

	// Write the elements in the range specified in the ArraySegment.
	Console.WriteLine("-- Range --");
	for (int i = segment.Offset; i <= segment.Count; i++)
	{
	    Console.WriteLine(segment.Array[i]);
	}
    }
}

Output

-- Array --
10
20
30
-- Offset --
1
-- Count --
2
-- Range --
20
30

Discussion. What are some other usages of ArraySegment? Let's say you have a large data array in your program, and want to call methods that act upon different parts of this large array. Copying these parts would cause increased memory usage.

Instead: Pass an ArraySegment to these methods as an argument. In these methods, use the ArraySegment to access the large array.

I have found that types such as ArraySegment are less than useful. I usually prefer solutions that involve indexes alone. These are similar to the functionality of the Substring method—it receives a start index and a count as ints.

SubstringInt

Summary. The ArraySegment type is a useful struct in the C# programming language that allows you to specify a range inside a specific array. You can access properties of the ArraySegment to access the original data and also the positional data.

So: The ArraySegment facilitates optimizations that reduce memory copying and heap allocations.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf