Found 34488 Articles for Programming

How to iterate efficiently through an array of integers of unknown size in C#

Samual Sam
Updated on 22-Jun-2020 12:12:32

373 Views

To iterate efficiently through an array of integers of unknown size in C# is easy. Let’s see how.Firstly, set an array, but do not set the size −int[] arr = new int[] { 5, 7, 2, 4, 1 };Now, get the length and iterate through an array using for loop −for (int i = 0; i< arr.Length; i++) {    Console.WriteLine(arr[i]); }Let us see the complete example −Example Live Demousing System; public class Program {    public static void Main() {       int[] arr = new int[] {          5,          7,          2,          4,          1       };       // Length       Console.WriteLine("Length:" + arr.Length);       for (int i = 0; i < arr.Length; i++) {          Console.WriteLine(arr[i]);       }    } }OutputLength:5 5 7 2 4 1

How to iterate any Map in C#

karthikeya Boyini
Updated on 22-Jun-2020 12:13:07

1K+ Views

C# has no built-in Math type. For the same, use a Dictionary.Firstly, create a Dictionary −Dictionary d = new Dictionary(); d.Add("keyboard", 1); d.Add("mouse", 2);Get the keys −var val = d.Keys.ToList();Now, use the foreach loop to iterate over the Map −foreach (var key in val) {    Console.WriteLine(key); }To iterate it, try to run the following code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       Dictionary d = new Dictionary();       d.Add("keyboard", 1);       d.Add("mouse", 2);       // get keys   ... Read More

How to instantiate a class in C#?

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

4K+ Views

Use the new operator to instantiate a class in C#.Let’s say our class is Line. Instantiation will create a new object as shown below −Line line = new Line();Using the object, you can now call the method −line.setLength(6.0);Let us see the example −Example Live Demousing System; namespace LineApplication {    class Line {       private double length; // Length of a line       public Line() {          Console.WriteLine("Object is being created");       }       public void setLength( double len ) {          length = len; ... Read More

How to insert an item in ArrayList in C#?

karthikeya Boyini
Updated on 22-Jun-2020 12:14:40

402 Views

To insert an item in an already created ArrayList, use the Insert() method.Firstly, set elements −ArrayList arr = new ArrayList(); arr.Add(45); arr.Add(78); arr.Add(33);Now, let’s say you need to insert an item at 2nd position. For that, use the Insert() method −// inserting element at 2nd position arr.Insert(1, 90);Let us see the complete example −Example Live Demousing System; using System.Collections; namespace Demo {    public class Program {       public static void Main(string[] args) {          ArrayList arr = new ArrayList();          arr.Add(45);          arr.Add(78);       ... Read More

How to input multiple values from user in one line in C#?

Samual Sam
Updated on 22-Jun-2020 11:40:25

2K+ Views

Use a while loop to input multiple values from the user in one line.Let’s say you need to get the elements of a matrix. Get it using Console.ReadLine() as shown below −Console.Write("Enter elements - Matrix 1 : "); for (i = 0; i < m; i++) {    for (j = 0; j < n; j++) {       arr1[i, j] = Convert.ToInt16(Console.ReadLine());    } }The following is an example showing how we can input multiple values from user −Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) ... Read More

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

karthikeya Boyini
Updated on 06-Sep-2023 20:56:09

42K+ Views

To initialize a list to an empty list in C#, set it like the following statement without any elements −List list = new List();Now, use the Any() method to check whether the list is empty or not −bool chk = !list.Any();Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Program {    public static void Main() {       // empty list       List list = new List();       // check for empty list       bool chk = !list.Any();       if(chk) {          Console.WriteLine("List is Empty!");       } else {          Console.WriteLine("List isn't Empty!");       }    } }OutputList is Empty!

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

Samual Sam
Updated on 22-Jun-2020 11:59:00

11K+ Views

To initialize a dictionary to an empty dictionary, use the Clear() method. It clears the dictionary and forms it as empty.dict.Clear();After that, use the Dictionary count property to check whether the list is empty or not −if (dict.Count == 0) {    Console.WriteLine("Dictionary is empty!"); }Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; namespace Demo {    public class Program {       public static void Main(string[] args) {          var dict = new Dictionary < string, string > ();          dict.Clear();          if (dict.Count == 0) {             Console.WriteLine("Dictionary is empty!");          }       }    } }OutputDictionary is empty!

Sets in C#

mkotla
Updated on 22-Jun-2020 11:57:09

6K+ Views

Sets in C# is a HashSet. HashSet in C# eliminates duplicate strings or elements in an array. In C#, it is an optimized set collectionTo declare HashSet −var h = new HashSet(arr1);Above, we have set the already declared array arr1 in the HashSet.Now set it on the array to remove the duplicate words −string[] arr2 = h.ToArray();Let us see an example to remove duplicate strings using C# HashSet.Here, we have duplicate elements −Exampleusing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       string[] arr1 = {"Table", "Chair", "Pen", "Clip", "Table"};     ... Read More

Main thread vs child thread in C#

varma
Updated on 22-Jun-2020 12:00:04

629 Views

Main ThreadThe first thread to be executed in a process is called the main thread. When a C# program starts execution, the main thread is automatically created.Child ThreadThe threads created using the Thread class are called the child threads of the main thread.Here is an example showing how to create a main and child thread −Example Live Demousing System; using System.Threading; namespace Demo {    class Program {       static void Main(string[] args) {          Thread th = Thread.CurrentThread;          th.Name = "MainThread";          Console.WriteLine("This is {0}", th.Name);          Console.ReadKey();       }    } }OutputThis is MainThread

Valid variant of Main() in C#

varun
Updated on 22-Jun-2020 12:01:04

426 Views

The Main method is the entry point for all C# programs. It states what the class does when executed.The valid variant of Main() is −static void Main(string[] argsHere,static − the object is not needed to access static membersvoid − return type of the methodMain − entry point for any C# program. Program execution begins here.string[] args − for command line arguments in C#.ExampleHere is an example −using System; namespace Program {    public class Demo {       public static void Main(string[] args) {          Console.WriteLine("Welcome!");          Console.ReadKey();       }    }   }

Advertisements