Found 34494 Articles for Programming

Iterating C# StringBuilder in a foreach loop

Samual Sam
Updated on 22-Jun-2020 13:37:06

2K+ Views

Firstly, set a string array and StringBuilder −// string array string[] myStr = { "One", "Two", "Three", "Four" }; StringBuilder str = new StringBuilder("We will print now...").AppendLine();Now, use foreach loop to iterate −foreach (string item in myStr) {    str.Append(item).AppendLine(); }The following is the complete code −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       // string array       string[] myStr = { "One", "Two", "Three", "Four" };       StringBuilder str = new StringBuilder("We will print now...").AppendLine();       // foreach loop to append ... Read More

Compare the content of two StringBuilders

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)) {    Console.WriteLine("Contents are equal!"); }The following is the complete code −Example Live Demousing System; using System.Text; class Demo {    static void Main() {       // first       StringBuilder str1 = new StringBuilder();       str1.Append("Tim");       str1.Append("Tom");       str1.Append("Henry"); ... Read More

Replace a string using StringBuilder

Samual Sam
Updated on 22-Jun-2020 13:38:40

525 Views

Set a String −StringBuilder str = new StringBuilder("Fitness is important");Use the Replace() method to replace a string −str.Replace("important", "essential");The following is the code to replace a string using StringBuilder −Example Live Demousing System; using System.Text; class Demo {    static void Main() {       // Initial String       StringBuilder str = new StringBuilder("Fitness is important");       Console.WriteLine(str.ToString());       // Replace       str.Replace("important", "essential");       // New String       Console.WriteLine(str.ToString());       Console.ReadLine();    } }OutputFitness is important Fitness is essential

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

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 = new StringBuilder();       str.AppendLine("Accessories");       str.AppendLine();       str.AppendLine("Electronics");       Console.Write(str);    } }OutputAccessories Electronics

Append to StringBuilder in C#

Samual Sam
Updated on 22-Jun-2020 13:39:34

414 Views

The Append() method add content to a StringBuilder.Set a String −StringBuilder str = new StringBuilder();Now, loop through the number of elements you want and use Append () to append to StringBuilder −for (int j = 0; j < 5; j++) {    str.Append(j).Append(" "); }The following is the complete code −Example Live Demousing System; using System.Text; class Program {    static void Main() {       StringBuilder str = new StringBuilder();       for (int j = 0; j < 5; j++) {          str.Append(j).Append(" ");       }       Console.WriteLine(str);    } }Output0 1 2 3 4

How to clear screen using C#?

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 the complete code −Example Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       ConsoleColor foreColor = Console.ForegroundColor;       ConsoleColor backColor = Console.BackgroundColor;       Console.WriteLine("Clearing the screen!");       Console.Clear();       ConsoleColor newForeColor = ConsoleColor.Blue;       ConsoleColor newBackColor = ConsoleColor.Yellow;    } }OutputClearing the screen!

Scope of Variables in C#

Samual Sam
Updated on 22-Jun-2020 13:25:45

701 Views

The scope of a variable is a region of code that indicates where the variables are being accessed.For a variable, it has the following levels −Method LevelVariable declared inside a method is a local variable.Class LevelVariable declared inside a class is a local variable are class member variables.Let us see an example of scope of variables −Example Live Demousing System; namespace Demo {    class Program {       public int Divide(int num1, int num2) {          // local variable in a method          int result;          result = num1 ... Read More

Pair Class in C#

Ankith Reddy
Updated on 22-Jun-2020 13:20:45

4K+ Views

The Pair class is the KeyValuePair class that stores a pair of values in a single list with C#.Declare KeyValuePair −var myList = new Liststring, int>>(); Now, add some elements: myList.Add(new KeyValuePair("Laptop", 1)); myList.Add(new KeyValuePair("Desktop System", 2)); myList.Add(new KeyValuePair("Tablet", 3)); myList.Add(new KeyValuePair("Mobile", 4)); myList.Add(new KeyValuePair("E-Book Reader", 5)); myList.Add(new KeyValuePair("LED", 6));Display the KeyValuePair now as shown below −Example Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       var myList = new List();       // adding elements       myList.Add(new KeyValuePair ("Laptop", 1));       myList.Add(new KeyValuePair ("Desktop System", 2));   ... Read More

File Permissions in C#

George John
Updated on 22-Jun-2020 13:21:18

2K+ Views

For File Permission in C#, use the FileIOPermission Class. It controls the ability to access files and folders.The following are the properties of File Permissions class −Sr.No.Methods & Description1AllFilesGets or sets the permitted access to all files.2AllLocalFilesGets or sets the permitted access to all local files.The following are the methods of File Permission class −Sr.No.Methods & Description1AddPathList(FileIOPermissionAccess, String)This method adds access for the specified file or directory to the existing state of the permission2Copy()This method creates and returns an identical copy of the current permission.3GetType()The GetType() method gets the type of the current instance.4ToXml()Creates an XML encoding of the permission ... Read More

Difference between Dictionary and Hashtable in C#

Chandu yadav
Updated on 22-Jun-2020 13:22:20

493 Views

Hashtable is slower than Dictionary. For strongly-typed collections, the Dictionary collection is faster.HashtableHashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.Let us see an example −Example Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          Hashtable ht = new Hashtable();          ht.Add("E001", "Tom");          ht.Add("E098", "Amit");          ht.Add("E110", "Jack");         ... Read More

Advertisements