- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 34487 Articles for Programming
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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