Ankith Reddy has Published 1070 Articles

How to join two lists in C#?

Ankith Reddy

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 ... Read More

How to check if String is Palindrome using C#?

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 10:54:50

334 Views

Let’s say we need to find that the following string is Palindrome or not −str = "Level";For that, convert the string into character array to chec each character −char[] ch = str.ToCharArray();Now find the reverse −Array.Reverse(ch);Use the Equals method to find whether the reverse is equal to original array or ... Read More

Synchronization of ArrayList in C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 10:52:04

209 Views

Use the ArrayList.Synchronized Method in C# for synchronization of ArrayList in C#.Let us see an example to lock the collection using SyncRoot property in C# −ArrayList arr = new ArrayList(); lock(arr.SyncRoot) {    foreach (object ele in arr) {    } }The following is the complete example to check the ... Read More

How to check if array contains a duplicate number using C#?

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 10:18:36

916 Views

Firstly, set an array −int[] arr = {    87,    55,    23,    87,    45,    23,    98 };Now declare a dictionary and loop through the array and get the count of all the elements. The value you get from the dictionary displays the occurrence of ... Read More

Find free disk space using C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 10:08:39

943 Views

Firstly, create an instance of DriveInfo −DriveInfo dInfo = new DriveInfo("E");Display free space −Console.WriteLine("Disk Free space = {0}", dInfo.AvailableFreeSpace); Now, use AvailableFreeSpace property and get the percentage of free space −Double pc = (dInfo.AvailableFreeSpace / (float)dInfo.TotalSize) * 100;Here, you will get the percentage of free size in comparison with the ... Read More

How to find the length and rank of a jagged array in C#?

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 10:06:48

254 Views

Firstly, set a jagged array.int[][] arr = new int[][] {    new int[] {       0,       0    }, new int[] {       1,       2    },    new int[] {       2,       4   ... Read More

Declare a const array in C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 09:57:59

8K+ Views

In C#, use readonly to declare a const array.public static readonly string[] a = { "Car", "Motorbike", "Cab" };In readonly, you can set the value at runtime as well unlike const.Another alternative of achieving what we saw above −public ReadOnlyCollection a { get { return new List { "Car", "Motorbike", ... Read More

C# Program to replace a character with asterisks in a sentence

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 09:57:18

939 Views

Use the Replace() method to replace a character with asterisks.Let’s say our string is −string str = "dem* text";To replace it, use the Replace() method −str.Replace('*', 'o');Here is the complete code −Example Live Demousing System; public class Program {    public static void Main() {       string str = ... Read More

How to find the length of jagged array using a property?

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 09:49:50

961 Views

Firstly, declare and initialize a jagged array.int[][] arr = new int[][] { new int[] {    0,    0 }, new int[] {    1,    2 }, new int[] {    2,    4 }, new int[] {    3,    6 }, new int[] {    4,   ... Read More

Reverse a Stack using C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 09:39:38

3K+ Views

Set a stack and add elements to it.Stack st = new Stack(); st.Push('P'); st.Push('Q'); st.Push('R');Now set another stack to reverse it.Stack rev = new Stack();Until the count of ths Stack is not equal to 0, use the Push and Pop method to reverse it.while (st.Count != 0) {    rev.Push(st.Pop()); ... Read More

Advertisements