Csharp Articles

Page 105 of 196

C# Program to generate random lowercase letter

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 2K+ Views

Firstly, set Random class −Random random = new Random();Set a range under the Next() method. This displays a letter between 0 and 26.int a = random.Next(0, 26);Here is the complete code −Exampleusing System; using System.IO; using System.Linq; class Demo {    static void Main() {       Random random = new Random();       // random lowercase letter       int a = random.Next(0, 26);       char ch = (char)('a' + a);       Console.WriteLine(ch);    } }Outputt

Read More

C# Program to match all the digits in a string

George John
George John
Updated on 11-Mar-2026 442 Views

To match all digits in a string, use C# Regex.Firstly, set a string with digits −string str = "These are my marks: 90 out of 100!";Use the following regular expression to get digits in a string −@"\d+"The following is the code −Exampleusing System; using System.Text.RegularExpressions; namespace Demo {    class Program {       private static void showMatch(string text, string expr) {          Console.WriteLine("The Expression: " + expr);          MatchCollection mc = Regex.Matches(text, expr);          foreach (Match m in mc) {             Console.WriteLine(m);     ...

Read More

Replace parts of a string with C# Regex

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 2K+ Views

Set a string −string str = "Bit and Bat";Let’s say you need to replace whatever comes inside B and t to A and capitalize the complete string. For that, use Replace −Regex.Replace(str, "B.t", "BAT");Let us see the complete code −Exampleusing System; using System.Text.RegularExpressions; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "Bit and Bat";          Console.WriteLine(str);          string res = Regex.Replace(str, "B.t", "BAT");          Console.WriteLine(res);       }    } }OutputBit and Bat BAT and BAT

Read More

C# program to get the extension of a file in C#

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 328 Views

To handle file paths, the Path class is used in C#.Set the file name in a string −string myPath = "D:ew\quiz.txt";Now, to get the extension, use the GetExtension() method −Path.GetExtension(myPath)Here is the complete code −Exampleusing System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          string myPath = "D:ew\quiz.txt";          Console.WriteLine(Path.GetExtension(myPath));       }    } }Output.txt

Read More

C# program to get the file name in C#

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

Set the path name in a string −string myPath = "D:ew\quiz.txt";Now, use the GetFileName() method to get the name of the file −Path.GetFileName(myPath)The following is the complete code −Exampleusing System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          string myPath = "D:ew\quiz.txt";          // get extension          Console.WriteLine("Extension: "+Path.GetExtension(myPath));          // get path          Console.WriteLine("File Path: "+Path.GetFileName(myPath));       }    } }OutputExtension: .txt File Path: D:ew\quiz.txt

Read More

Environment.NewLine in C#

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 2K+ Views

The Enviornment.NewLine in C# is used to add newline.To set a new line in between words −str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!";The following is the code −Exampleusing System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!";          Console.Write(str);       }    } }OutputThis is demo text! This is demo text on next line!

Read More

Func generic type in C#

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

The Func generic type store anonymous methods and is a parameterized type.In the below example, we have 4 func type instance −The first type receives int and returns stringFunc one = (p) => string.Format("{0}", p);The second type receives bool & long and returns stringFunc two = (q, p) =>string.Format("{0} and {1}", q, p);The third type receives bool & int and returns stringFunc three = (q, p) => string.Format("{0} and {1}", q, p);The fourth type receives decimal and returns stringFunc four = (p) =>string.Format("{0}", p);Let us see how to display them −Exampleusing System; using System.IO; namespace Demo {    class ...

Read More

C# program to find additional values in two lists

Samual Sam
Samual Sam
Updated on 11-Mar-2026 383 Views

Firstly, set two lists in C#.List OneList list1 = new List (); list1.Add("P"); list1.Add("Q"); list1.Add("R"); list1.Add("S"); list1.Add("T"); list1.Add("U"); list1.Add("V"); list1.Add("W");List TwoList list2 = new List (); list2.Add("T"); list2.Add("U"); list2.Add("V"); list2.Add("W");Now, to get the different values in both the lists, use the Except method. It returns the values in the first list which is not present in the second list.Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List list1 = new List ();       list1.Add("P");       list1.Add("Q");       ...

Read More

How to iterate two Lists or Arrays with one foreach statement in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 6K+ Views

Set two arrays.var val = new [] { 20, 40, 60}; var str = new [] { "ele1", "ele2", "ele3"};Use the zip() method to process the two arrays in parallel.var res = val.Zip(str, (n, w) => new { Number = n, Word = w });The above fetches both the arrays with int and string elements respectively.Now, use foreach to iterate the two arrays −Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       var val = new [] { 20, 40, 60};       var str = new [] ...

Read More

C# program to find the length of the Longest Consecutive 1's in Binary Representation of a given integer

Samual Sam
Samual Sam
Updated on 11-Mar-2026 338 Views

To fetch the consecutive 1’s, use the Bitwise Left Shift Operator. Here is our decimal number.i = (i & (i

Read More
Showing 1041–1050 of 1,951 articles
« Prev 1 103 104 105 106 107 196 Next »
Advertisements