Found 34494 Articles for Programming

Conversion of ArrayList to Array in C#

Ankith Reddy
Updated on 22-Jun-2020 12:38:40

944 Views

To convert an ArrayList to Array, use the ToArray() method in C#.Firstly, set an ArrayList −ArrayList arrList = new ArrayList(); arrList.Add("one"); arrList.Add("two"); arrList.Add("three");Now, to convert, use the ToArray() method −arrList.ToArray(typeof(string)) as string[];Let us see the complete code −Example Live Demousing System; using System.Collections; public class Program {    public static void Main() {       ArrayList arrList = new ArrayList();       arrList.Add("one");       arrList.Add("two");       arrList.Add("three");       string[] arr = arrList.ToArray(typeof(string)) as string[];       foreach (string res in arr) {          Console.WriteLine(res);       }    } }Outputone two three

How to implement Traversal in Singly Linked List using C#?

Arjun Thakur
Updated on 22-Jun-2020 12:26:27

371 Views

Set a linkelist collection −var list = new LinkedList();Now, add elements −list.AddLast("One"); list.AddLast("Two"); list.AddLast("Four");Now, let us add new elements in the already created LinkedList −LinkedListNode node = list.Find("Four"); list.AddBefore(node, "Three"); list.AddAfter(node, "Five");Let us now see how to traverse through the nodes in a Singly Linked List −Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       var list = new LinkedList < string > ();       list.AddLast("One");       list.AddLast("Two");       list.AddLast("Four");       Console.WriteLine("Travering...");       foreach(var res in list) {   ... Read More

How to loop through all the elements of an array in C#?

Ankith Reddy
Updated on 22-Jun-2020 12:26:59

183 Views

Firstly, set an array and initialize it −int[] arr = new int[] {34, 56, 12};To loop through all the elements of an array −for (int i = 0; i < arr.Length; i++) {    Console.WriteLine(arr[i]); }Let us see the complete code −Exampleusing System; public class Program {    public static void Main() {       int[] arr = new int[] {34, 56, 12};       // Length       Console.WriteLine("Length:" + arr.Length);       for (int i = 0; i< arr.Length; i++) {          Console.WriteLine(arr[i]);       }    } }

How to List all Substrings in a given String using C#?

George John
Updated on 22-Jun-2020 12:27:41

538 Views

To list all the substrings, use the Substring method and loop through the length of the string.Let’s say our string is −string myStr = "pqrz";Use nested loop and get the substring in a new string −for (int i = 1; i < myStr.Length; i++) {    for (int start = 0; start

How do I identify if a string is a number in C#?

vanithasree
Updated on 02-Apr-2020 11:06:44

242 Views

Let us say our string is −string str = "3456";Now, to check whether the entered string is a number or not −str.All(c => char.IsDigit(c))The above returns true if the string is a number, else false.Here is the complete code −Example Live Demousing System; using System.Linq; namespace Demo {    public class MyApplication {       public static void Main(string[] args) {          string str = "3456";          // checking if string is a number or not          Console.WriteLine(str.All(c => char.IsDigit(c)));       }    } }OutputTrue

How do I determine the size of my array in C#

seetha
Updated on 22-Jun-2020 12:28:58

130 Views

Firstly, set an array −int[] arr = {6, 3, 8, 4};Now, use the Length property to get the size of the array −arr.LengthLet us see the complete code −Example Live Demousing System; namespace Demo {    public class Demo {       public static void Main(string[] args) {          int[] arr = {6, 3, 8, 4};          Console.WriteLine("Array...");          foreach (int i in arr) {             Console.Write(i + " ");          }          Console.WriteLine("Size of Array: "+arr.Length);       }    } }OutputArray... 6 3 8 4 Size of Array: 4

How to iterate over a C# tuple?

Chandu yadav
Updated on 22-Jun-2020 12:28:17

2K+ Views

Firstly, declare a tuple and add values −Tuple tuple = new Tuple(100, "Tom");With C#, to iterate over a tuple, you can go for individual elements −tuple.Item1 // first item tuple.Item2 // second item To display the complete tuple, just use: // display entire tuple Console.WriteLine(tuple);Let us see the complete code −Exampleusing System; using System.Threading; namespace Demo {    class Program {       static void Main(string[] args) {          Tuple tuple = new Tuple(100, "Tom");          if (tuple.Item1 == 100) {             Console.WriteLine(tuple.Item1);          }          if (tuple.Item2 == "Tom") {             Console.WriteLine(tuple.Item2);          }          // display entire tuple          Console.WriteLine(tuple);       }    } }

How to empty a C# list?

Giri Raju
Updated on 22-Jun-2020 12:29:44

13K+ Views

To empty a C# list, use the Clear() method.Firstly, set a list and add elements −List myList = new List() {    "one",    "two",    "three",    "four",    "five",    "six" };Now, let us empty the list −myList.Clear();Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       List myList = new List() {          "one",          "two",          "three",          "four",          "five",          "six"       };       ... Read More

How to iterate over a C# list?

Arjun Thakur
Updated on 02-Apr-2020 11:03:52

1K+ Views

Declare a list and add elements −var products = new List < string > (); // adding elements products.Add("Belts"); products.Add("T-Shirt"); products.Add("Trousers");Use a loop to iterate −foreach(var p in products) {    Console.WriteLine(p); }ExampleLet us see the complete example −using System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       var products = new List < string > ();       products.Add("Belts");       products.Add("T-Shirt");       products.Add("Trousers");       Console.WriteLine("Our list....");       foreach(var p in products) {          Console.WriteLine(p);       }    } }

How to join two lists in C#?

Ankith Reddy
Updated on 22-Jun-2020 12:16:21

5K+ Views

To join two lists, use AddRange() method.Set the first list −var list1 = new List < string > (); list1.Add("Keyboard"); list1.Add("Mouse");Set the second list −var list2 = new List < string > (); list2.Add("Hard Disk"); list2.Add("Pen Drive");To concatenate both the lists −lists1.AddRange(lists2);The following is the complete code −Exampleusing System.Collections.Generic; using System; namespace Demo {    public static class Program {       public static void Main() {          var list1 = new List < string > ();          list1.Add("Keyboard");          list1.Add("Mouse");          Console.WriteLine("Our list1....");   ... Read More

Advertisements