Found 34488 Articles for Programming

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

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

131 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

How to instantiate delegates in C#?

George John
Updated on 22-Jun-2020 12:17:50

966 Views

Use the new keyword to instantiate a delegate. When creating a delegate, the argument passed to the new expression is written similar to a method call, but without the arguments to the method.For example −public delegate void printString(string s); printString ps1 = new printString(WriteToScreen);You can also instantiate a delegate using an anonymous method −//declare delegate void Del(string str); Del d = delegate(string name) {    Console.WriteLine("Notification received for: {0}", name); };Let us see an example that declare and instantiates a delegate −Example Live Demousing System; delegate int NumberChanger(int n); namespace DelegateAppl {    class TestDelegate {     ... Read More

How to get last 2 characters from string in C# using Regex?

Sreemaha
Updated on 22-Jun-2020 12:16:45

905 Views

Set the string −string str = "Cookie and Session";Use the following Regex to get the last 2 characters from string −Regex.Match(str,@"(.{2})\s*$")The following is the code −Example Live Demousing System; using System.Text.RegularExpressions; public class Demo {    public static void Main() {       string str = "Cookie and Session";       Console.WriteLine(Regex.Match(str,@"(.{2})\s*$"));    } }Outputon

How to insert an item in a list at a given position in C#?

Chandu yadav
Updated on 22-Jun-2020 12:19:34

2K+ Views

To insert an item in an already created List, use the Insert() method.Firstly, set elements −List list = new List(); list.Add(989); list.Add(345); list.Add(654); list.Add(876); list.Add(234); list.Add(909);Now, let’s say you need to insert an item at 4th position. For that, use the Insert() method −// inserting element at 4th position list.Insert(3, 567);Let us see the complete example −Exampleusing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          List < int > list = new List < int > ();         ... Read More

How to get last 4 characters from string inC#?

radhakrishna
Updated on 22-Jun-2020 12:19:58

2K+ Views

Firstly, set the string −string str = "Football and Tennis";Now, use the substring() method to get the last 4 characters −str.Substring(str.Length - 4);Let us see the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       string str = "Football and Tennis";       string res = str.Substring(str.Length - 4);       Console.WriteLine(res);    } }Outputnnis

How to initialize a tuple to an empty tuple in C#?

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

2K+ Views

To initialize a tuple to an empty tuple −Tuple myTuple;If you want to check for values in a tuple, that whether it is null or not −Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          Tuple tuple = new Tuple(10, null);          if (tuple.Item1 == 10) {             Console.WriteLine(tuple.Item1);          }          if (tuple.Item2 == null) {             Console.WriteLine("Item is null");          }       }    } }

Advertisements