Found 34494 Articles for Programming

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

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

936 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

60 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

Buffer GetByte Example in C#

Arjun Thakur
Updated on 22-Jun-2020 13:52:38

156 Views

Read individual bytes using GetByte() method in C# −Set an array −int[] arr = { 3, 4, 12 };Now, use Buffer.GetByte() to display the array elements and to read individual bytes −for (int i = 0; i < Buffer.ByteLength(arr); i++) {    Console.WriteLine(Buffer.GetByte(arr, i)); }The following is the code −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       int[] arr = { 3, 4, 12 };       // loop through the byte array       for (int i = 0; i < Buffer.ByteLength(arr); i++) {          Console.WriteLine(Buffer.GetByte(arr, i));       }    } }Output3 0 0 0 4 0 0 0 12 0 0 0

C# program to count number of bytes in an array

Ankith Reddy
Updated on 22-Jun-2020 13:53:14

1K+ Views

Set a byte array −byte[] b = { 5, 9, 19, 23, 29, 35, 55, 78 };To count number of bytes −Buffer.ByteLength(b)The following is the code −Example Live Demousing System; class Program {    static void Main() {       byte[] b = { 5, 9, 19, 23, 29, 35, 55, 78 };       int len = Buffer.ByteLength(b);       for (int i = 0; i < len; i++) {          Console.WriteLine(b[i]);       }       Console.WriteLine("Length of byte array = "+len);    } }Output5 9 19 23 29 35 55 78 Length of byte array = 8

Replace a C# array with a new array of different size

George John
Updated on 22-Jun-2020 13:53:43

772 Views

To replace a C# aray with a new array, use the Array.Resize.Withing that, set the size of the new array −Array.Resize(ref arr, 4);Now add the new elements to the array as shown below −Example Live Demousing System; class Program {    static void Main() {       char[] arr = new char[5];       arr[0] = 'J';       arr[1] = 'A';       Array.Resize(ref arr, 4);       // Set value for new elements       arr[2] = 'C';       arr[3] = 'K';       Console.WriteLine("Updated Array : "+ new string(arr));    } }OutputUpdated Array : JACK

Buffer BlockCopy in C#

Chandu yadav
Updated on 22-Jun-2020 13:54:20

517 Views

It copies the bytes from one byte array to another byte array.Example Live Demousing System; class Demo {    static void Main() {       // byte arrays       byte[] b1 = new byte[] {55, 66, 77, 88, 99};       byte[] b2 = new byte[8];       // copying bytes from one to another       Buffer.BlockCopy(b1, 0, b2, 0, 5);       /* calling the method with the byte array b2 that has the copied elements */       bufferFunc(b2);    }    static void bufferFunc(byte[] a) {       for (int j = 0; j < a.Length; j++) {          Console.Write(a[j]);       }       Console.WriteLine();    } }Output5566778899000

Advertisements