Ankith Reddy has Published 719 Articles

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

Ankith Reddy

Ankith Reddy

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

1K+ 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

1K+ 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

440 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

13K+ 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

1K+ 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

1K+ 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

4K+ 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

String Template Class in C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 09:29:37

512 Views

StringTemplate class is used to parse the format string, so that it is compatible with String.Format. The StringTemplate class comes under the NString library that has extension methods. These methods makes string manipulations easy to use like.IsNullOrEmpty() IsNullOrWhiteSpace() Join() Truncate() Left() Right() Capitalize()StringTemplate.Format is better than String.Format since it is ... Read More

How to convert a list to string in C#?

Ankith Reddy

Ankith Reddy

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

3K+ Views

Declare a list.List < string > l = new List < string > ();Now, add elements to the list.// elements l.Add("Accessories"); l.Add("Footwear"); l.Add("Watches");Now convert it into a string.string str = string.Join(" ", l.ToArray());Let us see the final code to convert a list to string in C# −Exampleusing System; using System.Collections.Generic; ... Read More

How to print one dimensional array in reverse order?

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 09:08:24

589 Views

Firstly, declare and initialize one dimensional array.int[] arr = { 35, 12, 66, 90, 34, 2, 64 };Now, use the following to reverse −Array.Reverse(arr);The following is the complete code to reverse a one-dimensional array;Example Live Demousing System; class Demo {    static void Main() {       int[] arr ... Read More

Advertisements