
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2587 Articles for Csharp

755 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

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

521 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

11K+ 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!

993 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

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

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

622 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

1K+ Views
Set an array −int[] arr = { 89, 12, 56, 89, };Now, create a new Dictionary −var d = new Dictionary < int, int > ();Using the dictionary method ContainsKey(), find the duplicate elements in the array −foreach(var res in arr) { if (d.ContainsKey(res)) d[res]++; else d[res] = 1; }Here is the complete code −Example Live Demousing System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { int[] arr = { 89, ... Read More

106 Views
Set an array −int[] arr = { 23, 66, 96, 110 };Now, create a new list −var list = new List();Use the Add method and add the array elements to the list −for (int i = 0; i < arr.Length; i++) { list.Add(arr[i]); }The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program { public static void Main() { int[] arr = { 23, 66, 96, 110 }; var list = new List(); for (int i = 0; i < arr.Length; i++) { list.Add(arr[i]); } foreach(int res in list) { Console.WriteLine(res); } } }Output23 66 96 110