Found 34487 Articles for Programming

C# program to get the last element from an array

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; public class Demo {    public static void Main() {       string[] str = new string[] {          "Java",          "HTML",          "jQuery",          "JavaScript",          "Bootstrap"       };       Console.WriteLine("Array...");       foreach(string res in str) {          Console.WriteLine(res);       }       Console.WriteLine("Last element: "+str[str.Length - 1]);    } }OutputArray... Java HTML jQuery JavaScript Bootstrap Last element: Bootstrap

C# program to return an array from methods

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 complete code −Example Live Demousing System; public class Demo {    public static void Main() {       Console.WriteLine(string.Join(" ", display()));    }    static string[] display() {       string[] str = new string[5];       // string array elements       str[0] = "My";       str[1] = "name";       str[2] = "is";       str[3] = "Brad";       str[4] = "Pitt";       return str;    } }OutputMy name is Brad Pitt

C# Program to read contents of a file into a string at once

Samual Sam
Updated on 03-Apr-2020 10:36:03

217 Views

Use ReadToEnd() method to read the contents of a file in a string.Set it under StreamReader and read the file −using (StreamReader sr = new StreamReader("new.txt")){    string res = sr.ReadToEnd();    Console.WriteLine(res); }The following is the complete code −Example Live Demousing System.IO; using System; public class Demo {    public static void Main() {       using (StreamWriter sw = new StreamWriter("new.txt")) {          sw.WriteLine("One");          sw.WriteLine("Two");       }       using (StreamReader sr = new StreamReader("new.txt")) {          string res = sr.ReadToEnd();       ... Read More

Read in a file in C# with StreamReader

karthikeya Boyini
Updated on 03-Apr-2020 10:43:20

430 Views

To read text files, use StreamReader class in C#.Add the name of the file you want to read −StreamReader sr = new StreamReader("hello.txt");Use the ReadLine() method and get the content of the file in a string −using (StreamReader sr = new StreamReader("hello.txt")) {    str = sr.ReadLine(); } Console.WriteLine(str);Let us see the following code −Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       string str;       using (StreamWriter sw = new StreamWriter("hello.txt")) {          sw.WriteLine("Hello");          sw.WriteLine("World");       }   ... Read More

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

157 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

837 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

Advertisements