- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34487 Articles for Programming
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
326 Views
Set an array −int[] val = new int[5]; // integer elements val[0] = 15; val[1] = 25; val[2] = 40; val[3] = 58; val[4] = 70;Now set a list and add array in it −List myList = new List(val);The following is the code −Example Live Demousing System; using System.Collections.Generic; public class Demo { public static void Main() { int[] val = new int[5]; // integer elements val[0] = 15; val[1] = 25; val[2] = 40; val[3] = 58; val[4] = 70; List myList = new List(val); Console.WriteLine("Elements..."); foreach(int res in myList) { Console.WriteLine(res); } // count integer elements Console.WriteLine("Number of elements: "+myList.Count); } }OutputElements... 15 25 40 58 70 Number of elements: 5
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
368 Views
Firstly, set a list −List myList = new List(); myList.Add(45); myList.Add(77);Now, to clear the above list, use Clear() −myList.Clear();Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo { public static void Main() { List myList = new List(); myList.Add(45); myList.Add(77); Console.WriteLine("Elements: "+myList.Count); myList.Clear(); Console.WriteLine("Elements after using clear: "+myList.Count); } }OutputElements: 2 Elements after using clear: 0
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
107 Views
With TrueForAll() method in arrays, you can check every element for a condition.Let us see an example −Example Live Demousing System; using System.Text; public class Demo { public static void Main() { int[] val = { 97, 45, 76, 21, 89, 45 }; // checking whether all the array element are more than one or not bool result = Array.TrueForAll(val, res => res > 1); Console.WriteLine(result); } }OutputTrueWith TrueForAll() method in arrays, you can check every element for a condition.Let us see an example −Example Live Demousing System; ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
297 Views
To find the last matching element, use the Array.LastIndexOf method. Returns -1 if the element isn’t present in the integer array.The following is the array −int[] val = { 97, 45, 76, 21, 89, 45 };Now, let’s say you need to search the last Index of element 45. For that, use Array.LastIndexOf() method −int res = Array.LastIndexOf(val, 45);The following is an example −Example Live Demousing System; using System.Text; public class Demo { public static void Main() { int[] val = { 97, 45, 76, 21, 89, 45 }; // last Index of element 45 int res = Array.LastIndexOf(val, 45); // Display the index Console.WriteLine("Index of element 45 is = "+res); } }OutputIndex of element 45 is = 5
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
937 Views
Use the Array.Exists method to check if a value is in an array or not.Set a string array −string[] strArray = new string[] {"keyboard", "screen", "mouse", "charger" };Let’s say you need to find the value “keyboard” in the array. For that, use Array.Exists() −Array.Exists(strArray, ele => ele == "keyboard");It returns a true value if element exists as shown below −Example Live Demousing System; using System.Text; public class Demo { public static void Main() { string[] strArray = new string[] {"keyboard", "screen", "mouse", "charger" }; bool res1 = Array.Exists(strArray, ele => ele == "harddisk"); ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
1K+ Views
Use the ConvertAll method to convert integer array to string array.Set an integer array −int[] intArray = new int[5]; // Integer array with 5 elements intArray[0] = 15; intArray[1] = 30; intArray[2] = 44; intArray[3] = 50; intArray[4] = 66;Now use Array.ConvertAll() method to convert integer array to string array −Array.ConvertAll(intArray, ele => ele.ToString());Let us see the complete code −Example Live Demousing System; using System.Text; public class Demo { public static void Main() { int[] intArray = new int[5]; // Integer array with 5 elements intArray[0] = 15; intArray[1] = 30; intArray[2] = 44; intArray[3] = 50; intArray[4] = 66; string[] strArray = Array.ConvertAll(intArray, ele => ele.ToString()); Console.WriteLine(string.Join("|", strArray)); } }Output15|30|44|50|66
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
61 Views
Get the location of array elements using the BinarySearch method.Set a string array −string[] str = { "a", "m", "i", "t"};Now get the location of character ‘t’ using Array.BinarySearch −Array.BinarySearch(str, "t");Here is the complete code −Example Live Demousing System; using System.Text; public class Demo { public static void Main() { string[] str = { "a", "m", "i", "t"}; // Using BinarySearch method to get location of character 't' int res = Array.BinarySearch(str, "t"); // displaying the location Console.WriteLine("Index : "+res); } }OutputIndex : 3
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
3K+ Views
Use the Buffer.BlockCopy method to copy a range of bytes from one array to another −Set a byte array −byte[] b1 = new byte[] {22, 49}; byte[] b2 = new byte[5];Copy bytes from one array to another −Buffer.BlockCopy(b1, 0, b2, 0, 2);The following is the complete code −Example Live Demousing System; class Demo { static void Main(){ // byte arrays byte[] b1 = new byte[] {22, 49}; byte[] b2 = new byte[5]; // copying bytes from one to another Buffer.BlockCopy(b1, 0, b2, 0, 2); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ Views
Firstly, set two arrays −int[] arr1 = { 15, 20, 27, 56 }; int[] arr2 = { 62, 69, 76, 92 };Now create a new list and use AddRange() method to merge −var myList = new List(); myList.AddRange(arr1); myList.AddRange(arr2);After that, convert the merged collection to an array −int[] arr3 = myList.ToArray()Let us see the complete codeExample Live Demousing System; using System.Collections.Generic; class Demo { static void Main() { int[] arr1 = { 15, 20, 27, 56 }; int[] arr2 = { 62, 69, 76, 92 }; // displaying array1 ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
182 Views
The SetByte() method assigns a specified value to a byte at a particular location in a specified array.Firstly, set an array −int[] arr = { 3, 4, 12 };Now, use SetByte() to assign values −Buffer.SetByte(arr, 3, 20);Here is the complete code −Example Live Demousing System; using System.Text; public class Demo { public static void Main() { int[] arr = { 3, 4, 12 }; Console.WriteLine("Initial Array..."); // loop through the byte array for (int i = 0; i < Buffer.ByteLength(arr); i++) { Console.WriteLine(Buffer.GetByte(arr, i)); ... Read More