Found 34494 Articles for Programming

Orderby clause in C#

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

383 Views

The orderby is used in C# to sort elements in the collection based on specified fields in a particular order. The order can be ascending or descending.The following is our list with elements −List myList = new List(); // adding elements myList.Add("iOS by Apple"); myList.Add("Android by Google"); myList.Add("Symbian by Nokia");Now, use Orderby to order the elements in descending order −var myLen = from element in myList orderby element.Length descending select element;The following is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List myList = new ... Read More

GroupBy() Method in C#

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 chkSmaller() finds the elements smaller than 50.Let us see the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 2, 30, 45, 60, 70 };       var check = arr.GroupBy(b => chkSmaller(b));   ... Read More

StreamWriter in C#

Samual Sam
Updated on 03-Apr-2020 10:37:22

155 Views

Write characters to a stream with StreamWriter in C#.With StreamWriter, create a new file −StreamWriter sw = new StreamWriter("hello.txt"))Add text to the file −sw.WriteLine("Hello"); sw.WriteLine("World");The following is the code −Exampleusing System.IO; public class Program {    public static void Main() {       using (StreamWriter sw = new StreamWriter("hello.txt")) {          sw.WriteLine("Hello");          sw.WriteLine("World");       }    } }It creates the file “hello.text” and adds text to it −OutputThe following is the output.Hello World

CompareTo() method in C#

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 void Main() {       int val1 = 100;       int val2 = 100;       int res = val1.CompareTo(val2);       Console.WriteLine(res);    } }Output0

Declare char arrays in C#

Samual Sam
Updated on 22-Jun-2020 13:46:46

5K+ Views

Declare a char array and set the size −char[] arr = new char[5];Now set the elements −arr[0] = 'h'; arr[1] = 'a'; arr[2] = 'n'; arr[3] = 'k'; arr[4] = 's';Let us see the complete code now to declare, initialize and display char arrays in C# −Example Live Demousing System; public class Program {    public static void Main() {       char[] arr = new char[5];       arr[0] = 'h';       arr[1] = 'a';       arr[2] = 'n';       arr[3] = 'k';       arr[4] = 's';       Console.WriteLine("Displaying string elements...");       for (int i = 0; i < arr.Length; i++) {          Console.WriteLine(arr[i]);       }    } }OutputDisplaying string elements... h a n k s

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

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 class Program {    public static void Main() {       StringBuilder myStr = new StringBuilder("Framework");       Console.WriteLine("Initial String: " + myStr);       // removing four characters       Console.Write("New string: ");       myStr.Remove(5, 4);       Console.WriteLine(myStr);    } }OutputInitial String: Framework New string: Frame

C# program to remove characters starting at a particular index in StringBuilder

Samual Sam
Updated on 22-Jun-2020 13:35:12

72 Views

Set StringBuilder −StringBuilder str = new StringBuilder("Airport");Let’s say you need to remove characters. For that, use the Remove() method, which removes a bunch of characters beginning with a particular index −str.Remove(3, 4);The above removes four characters beginning from 3rd index (i.e. 4th position) −Here is the complete code −Example Live Demousing System; using System.Text; public class Program {    public static void Main() {       StringBuilder str = new StringBuilder("Airport");       Console.WriteLine("String: "+str);       // removing four characters       Console.Write("String after removing characters: ");       str.Remove(3, 4);       Console.WriteLine(str);    } }OutputString: Airport String after removing characters: Air

C# Program to change a character from a string

karthikeya Boyini
Updated on 22-Jun-2020 13:35:34

217 Views

Let's say our string is −StringBuilder str = new StringBuilder(); str.Append("pre");To change a character, set the value at that particular index. The following sets a character at the 3rd position −str[2] = 'o';Here is the complete code −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       StringBuilder str = new StringBuilder();       str.Append("pre");       Console.WriteLine("String : "+str);       Console.WriteLine("Change 3rd character");       str[2] = 'o';       Console.WriteLine("New String : "+str);    } }OutputString : pre Change 3rd character New String : pro

Access a character in C# StringBuilder

Samual Sam
Updated on 22-Jun-2020 13:35:57

312 Views

Firstly, set the StringBuilder −StringBuilder str = new StringBuilder(); str.Append("premium");To access the 5th character and display it −Console.WriteLine(str[4]);The following is the complete code −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       StringBuilder str = new StringBuilder();       str.Append("premium");       Console.WriteLine("String : "+str);       Console.Write("Accessing 5th character : ");       Console.WriteLine(str[4]);    } }OutputString : premium Accessing 5th character : i

Clear a StringBuilder in C#

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 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

Advertisements