Found 34487 Articles for Programming

C# program to create a List with elements from an array

Samual Sam
Updated on 22-Jun-2020 13:58:10

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

Clear a list in C#

karthikeya Boyini
Updated on 22-Jun-2020 13:58:40

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

TrueForAll() method in C# Arrays

Samual Sam
Updated on 22-Jun-2020 13:59:12

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

C# program to find the last matching element in an array

karthikeya Boyini
Updated on 22-Jun-2020 13:59:37

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

C# program to check if a value is in an array

Samual Sam
Updated on 22-Jun-2020 13:59:58

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

C# Program to convert integer array to string array

karthikeya Boyini
Updated on 22-Jun-2020 14:00:24

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

Array.BinarySearch Method in C#

Samual Sam
Updated on 22-Jun-2020 14:01:25

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

C# program to copy a range of bytes from one array to another

karthikeya Boyini
Updated on 22-Jun-2020 14:01:56

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

Merge two arrays using C# AddRange() method

Samual Sam
Updated on 22-Jun-2020 14:02:58

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

Buffer SetByte Example in C#

Chandu yadav
Updated on 22-Jun-2020 14:03:27

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

Advertisements