Karthikeya Boyini has Published 2383 Articles

Clear a list in C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 13:58:40

367 Views

Firstly, set a list −List myList = new List(); myList.Add(45); myList.Add(77);Now, to clear the above list, use Clear() −myList.Clear();Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       List myList = new List();       myList.Add(45); ... Read More

C# program to remove a range of characters by index using StringBuilder

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 13:47:13

418 Views

Use the Remove() method to remove a range of characters by index.Let’s say you need to remove the last 5 characters from the following string −StringBuilder myStr = new StringBuilder("Framework");For that, set the Remove() method as −str.Remove(3, 4);The following is the complete code −Example Live Demousing System; using System.Text; public ... Read More

CompareTo() method in C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 13:46:02

836 Views

To compare two values, use the CompareTo() method.The following are the return values −0 = both the numbers are equal1 = second number is smaller-1 = first number is smallerHere is the code to implement CompareTo() method in C# −Example Live Demousing System; public class Demo {    public static ... Read More

GroupBy() Method in C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 13:45:32

3K+ Views

The GroupBy() is an extension method that returns a group of elements from the given collection based on some key value.The following is our array −int[] arr = { 2, 30, 45, 60, 70 };Now, we will use GroupBy() to group the elements smaller than 50 −arr.GroupBy(b => chkSmaller(b));The above ... Read More

C# program to return an array from methods

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 13:41:19

9K+ Views

Call a method under Join method to join words −string.Join(" ", display())Now set a string array −string[] str = new string[5];Add individual elements −str[0] = "My"; str[1] = "name"; str[2] = "is"; str[3] = "Brad"; str[4] = "Pitt";And return the same string array from method −return str;The following is the ... Read More

C# program to get the last element from an array

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 13:40:53

3K+ Views

Firstly, set an array −string[] str = new string[]{    "Java",    "HTML",    "jQuery",    "JavaScript",    "Bootstrap" };To get the value of the last element, get the length and display the following value −str[str.Length - 1]The above returns the last element.Here is the complete code −Example Live Demousing System; ... Read More

How to clear screen using C#?

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 13:39:55

9K+ Views

Use the Console.Clear() method to clear screen and the console buffer. When the Clear method is called, the cursor automatically scrolls to the top-left corner of the window.Here, we have cleared the screen and then set the ForegroundColor and BackgroundColor −ConsoleColor newForeColor = ConsoleColor.Blue; ConsoleColor newBackColor = ConsoleColor.Yellow;The following is ... Read More

Append to StringBuilder in C# with a new line on the end

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 13:39:07

3K+ Views

The AppendLine() method appends the content and add a new line on the end.Firstly, set the StringBuilder −StringBuilder str = new StringBuilder();Use AppendLine() −str.AppendLine("Accessories"); str.AppendLine(); str.AppendLine("Electronics");The following is the complete code −Example Live Demousing System; using System.Text; class Demo {    static void Main() {       StringBuilder str ... Read More

Compare the content of two StringBuilders

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 13:38:04

2K+ Views

Equals method is used in C# to compare the content of two StringBuilders.The following are our two StringBuilders −// first StringBuilder str1 = new StringBuilder(); str1.Append("Tim"); str1.Append("Tom"); str1.Append("Henry"); // second StringBuilder str2 = new StringBuilder(); str2.Append("John"); str2.Append("David"); str2.Append("Beth");Now use the Equals() method to compare both the methods −if (str1.Equals(str2)) ... Read More

Clear a StringBuilder in C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 13:36:36

2K+ Views

To clear a StringBuilder, use the Clear() method.Let’s say we have set the following StringBuilder −string[] myStr = { "One", "Two", "Three", "Four" }; StringBuilder str = new StringBuilder("We will print now...").AppendLine();Now, use the Clear() method to clear the StringBuilder −str.Clear();Let us see the complete code −Example Live Demousing System; using ... Read More

Advertisements