Csharp Articles

Page 96 of 196

Orderby clause in C#

Samual Sam
Samual Sam
Updated on 11-Mar-2026 507 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 −Exampleusing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List myList = new List(); ...

Read More

Read in a file in C# with StreamReader

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 608 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 −Exampleusing 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

C# program to return an array from methods

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 12K+ 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 −Exampleusing 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

Read More

C# program to get the last element from an array

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 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 −Exampleusing 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

Read More

C# program to iterate over a string array with for loop

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 3K+ Views

Create a string array −string[] str = new string[] {    "Videos",    "Tutorials",    "Tools",    "InterviewQA" };Loop until the length of the array −for (int i = 0; i < str.Length; i++) {    string res = str[i];    Console.WriteLine(res); }Here is the complete code −Exampleusing System; public class Demo {    public static void Main() {       string[] str = new string[] {          "Videos",          "Tutorials",          "Tools",          "InterviewQA"       };           Console.WriteLine("String Array...");       for (int i = 0; i < str.Length; i++) {          string res = str[i];          Console.WriteLine(res);       }    } }OutputString Array... Videos Tutorials Tools InterviewQA

Read More

C# program to create an empty string array

George John
George John
Updated on 11-Mar-2026 4K+ Views

To create an empty string array −string[] str = new string[] {};Above, we haven’t added elements to the array, since it is empty.Even if we will loop though the array, it won’t display anything as shown below −Exampleusing System; public class Demo {    public static void Main() {       string[] str = new string[] {};       Console.WriteLine("String Array elements won't get displayed since it's empty...");       for (int i = 0; i < str.Length; i++) {          string res = str[i];          Console.WriteLine(res);       }    } }OutputString Array elements won't get displayed since it's empty...

Read More

C# program to find the index of a word in a string

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 635 Views

Declare and initialize an array −string[] str = new string[] {    "Cat",    "Mat",    "Rat" };Now, ue IndexOf() method to find the index of the word “Mat” −Array.IndexOf(str, "Mat");The following is the code −Exampleusing System; public class Demo {    public static void Main() {       string[] str = new string[] {          "Cat",          "Mat",          "Rat"       };       Console.WriteLine("Our Array =");       for (int i = 0; i < str.Length; i++) {          string res = str[i];          Console.WriteLine(res);       }       int findIndex = Array.IndexOf(str, "Mat");       Console.Write("Element Mat found at the following index: ");       Console.WriteLine(findIndex);    } }OutputOur Array = Cat Mat Rat Element Mat found at the following index: 1

Read More

C# program to separate joined strings in C#

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 98 Views

The following is the string array −string[] str = { "Java", "AngularJS", "Python", "jQuery", "HTML5" };Firstly, join it −string.Join(" ", str);Now to separate the above joined strings, use the Split() method as shown in the following code −Exampleusing System; public class Demo {    public static void Main() {       string[] str = { "Java", "AngularJS", "Python", "jQuery", "HTML5" };       // join words       string res = string.Join(" ", str);       Console.WriteLine("Joined Strings... "+res);       string[] convert = res.Split(' ');       Console.WriteLine("Separate Joined Strings...");       foreach (string val in convert) {          Console.WriteLine(val);       }    } }OutputJoined Strings... Java AngularJS Python jQuery HTML5 Separate Joined Strings... Java AngularJS Python jQuery HTML5

Read More

Bool Array in C#

George John
George John
Updated on 11-Mar-2026 10K+ Views

In a bool array, you can store true and false values. To set a bool array, use the new operator −bool[] arr = new bool[5];To add elements in the array −arr[0] = true; arr[1] = true; arr[2] = false; arr[3] = true; arr[4] = true;Let us see the complete code −Exampleusing System; public class Demo {    public static void Main() {       bool[] arr = new bool[5];       arr[0] = true;       arr[1] = true;       arr[2] = false;       arr[3] = true;       arr[4] = true;       Console.WriteLine("Displaying values...");       foreach (bool res in arr) {          Console.WriteLine(res);       }    } }OutputDisplaying values... True True False True True

Read More

Default operator in C#

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 721 Views

Using the default, you can get the default value of every reference and value type. The expressions set as default are evaluated at compile-time.To get the default for int −default(int);To get the default for long −default(long)Let us see the code to display default values −Exampleusing System; public class Demo {    public static void Main() {       int val1 = default(int);       long val2 = default(long);       bool val3 = default(bool);       // default for int       Console.WriteLine(val1);       // default for long       Console.WriteLine(val2);       // default for bol       Console.WriteLine(val3);    } }Output0 0 False

Read More
Showing 951–960 of 1,951 articles
« Prev 1 94 95 96 97 98 196 Next »
Advertisements