How to insert an item into a C# list by using an index?

George John
Published on 31-Aug-2018 12:05:16
Firstly, set a list − List<int> list = new List<int>(); list.Add(456); list.Add(321); list.Add(123); list.Add(877); list.Add(588); list.Add(459); Now, to add an item at index 5, let say; for that, use the Insert() method − list.Insert(5, 999); Let us see the complete example − using System; using ... Read More

How to initialize a rectangular array in C#?

Chandu yadav
Published on 31-Aug-2018 12:01:30
An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations. Multi-dimensional arrays are also called rectangular array. Multidimensional arrays are initialized by specifying bracketed values ... Read More

How to obtain Status of the Current Thread in C#?

karthikeya Boyini
Published on 31-Aug-2018 11:58:03
To get the status of current thread, use the IsAlive() method − Firstly, create a new thread − Thread t = Thread.CurrentThread; t.Name = "Our Thread"; Now, to get the status of the current thread − t.IsAlive The following is the complete code ... Read More

Recommended IDEs for C# on Windows/Linux/Mac OS

Samual Sam
Published on 31-Aug-2018 11:56:33
The best IDE for C# on Windows is Microsoft Visual Studio. It is an IDE to develop websites, web apps, mobile apps, etc. The following are the features of Visual Studio IDE − Code Editor − Visual Studio has a code editor supporting syntax highlighting and ... Read More

C# Program to check if a number is Positive, Negative, Odd, Even, Zero

karthikeya Boyini
Published on 31-Aug-2018 11:54:52
Check for the following conditions − For odd and even, check for the remainder when the number is divided by 2 − // checking for odd/ even if(n % 2 == 0) { Console.WriteLine("Even"); } else { Console.WriteLine("Odd"); } For ... Read More

How to loop through all values of an enum in C#?

Samual Sam
Published on 31-Aug-2018 11:52:44
To loop through all the values of enum, use the Enum.GetValues(). Firstly, set an Enum − public enum Grade { A, B, C, D, E, F }; Now, with foreach loop, you need to loop through Enum.GetValues(typeof(Grade)) − foreach (Grade g in Enum.GetValues(typeof(Grade))) ... Read More

How to list down all the files available in a directory using C#?

karthikeya Boyini
Published on 31-Aug-2018 11:50:34
Firstly, use the DirectoryInfo object − //creating a DirectoryInfo object DirectoryInfo mydir = new DirectoryInfo(@"d:\amit"); Now, use the GetFiles() method to get all the files − FileInfo [] f = mydir.GetFiles(); To get the list of files in a directory, try to run the ... Read More

How to join or concatenate two lists in C#?

Samual Sam
Published on 31-Aug-2018 11:48:05
To concatenate two lists, use AddRange() method. Set the first list − var products1 = new List < string > (); products1.Add("Belts"); products1.Add("Tshirt"); products1.Add("Trousers"); Set the second list − var products2 = new List < string > (); products2.Add("Footwear"); products2.Add("Electronics"); To concatenate both ... Read More

How to iterate over a C# dictionary?

karthikeya Boyini
Published on 31-Aug-2018 11:44:24
Firstly, add elements − IDictionary<int, int> d = new Dictionary<int, int>(); d.Add(1, 97); d.Add(2, 89); d.Add(3, 77); d.Add(4, 88); Now, get the keys − List<int> myList = new List<int>(d.Keys); To iterate − foreach (int k in myList) { Console.WriteLine("{0}, ... Read More

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

Samual Sam
Published on 31-Aug-2018 11:42:01
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 ... Read More
Advertisements