Csharp Articles

Page 92 of 196

How to open a plain text file in C#?

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

To open a plain text file, use the StreamReader class. The following opens a file for reading −StreamReader sr = new StreamReader("d:/new.txt")Now, display the content of the file −while ((line = sr.ReadLine()) != null) {    Console.WriteLine(line); }Here is the code −Exampleusing System; using System.IO; namespace FileApplication {    class Program {       static void Main(string[] args) {          try {             using (StreamReader sr = new StreamReader("d:/new.txt")) {                string line;                // Read ...

Read More

Different ways of Reading a file in C#

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

Here, we are reading two different files −Reading text file −Exampleusing System; using System.IO; namespace FileApplication {    class Program {       static void Main(string[] args) {          try {             using (StreamReader sr = new StreamReader("d:/new.txt")) {                string line;                // Read and display lines from the file until                // the end of the file is reached.                while ((line = ...

Read More

Global and Local Variables in C#

Giri Raju
Giri Raju
Updated on 11-Mar-2026 5K+ Views

Local VariablesA local variable is used where the scope of the variable is within the method in which it is declared. They can be used only by statements that are inside that function or block of code.Exampleusing System; public class Program {    public static void Main() {       int a;       a = 100;       // local variable       Console.WriteLine("Value:"+a);    } }OutputValue:100Global VariablesC# do not support global variables directly and the scope resolution operator used in C++ for global variables is related to namespaces. It is called global namespace alias.If ...

Read More

Print first letter of each word in a string using C# regex

seetha
seetha
Updated on 11-Mar-2026 520 Views

Let’s say our string is −string str = "The Shape of Water got an Oscar Award!";Use the following Regular Expression to display first letter of each word −@"\b[a-zA-Z]"Here is the complete code −Exampleusing System; using System.Text.RegularExpressions; namespace RegExApplication {    public 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

Print number with commas as 1000 separators in C#

Giri Raju
Giri Raju
Updated on 11-Mar-2026 2K+ Views

Firstly, set the number as string −string num = "1000000.8765";Now, work around differently for number before and after the decimal −string withoutDecimals = num.Substring(0, num.IndexOf(".")); string withDecimals = num.Substring(num.IndexOf("."));Use the ToString() method to set the format for 1000 separators −ToString("#,##0")The following is the complete code to display number with commas as 1000 separators −Exampleusing System; public class Program {    public static void Main() {       string num = "1000000.8765";       string withoutDecimals = num.Substring(0, num.IndexOf("."));       string withDecimals = num.Substring(num.IndexOf("."));       withoutDecimals = Convert.ToInt32(withoutDecimals).ToString("#,##0");       Console.WriteLine(withoutDecimals + withDecimals);    } }Output1,000,000.8765

Read More

C# program to multiply all numbers in the list

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

Firstly, set the list −List myList = new List () {    5,    10,    7 };Now, set the value of a variable to 1 that would help us in multiplying −int prod = 1;Loop through and get the product −foreach(int i in myList) {    prod = prod*i; }The following is the code −Exampleusing System; using System.Collections.Generic; public class Program {    public static void Main() {       List myList = new List() {          5,          10,          7       };       Console.WriteLine("List: ");       foreach(int i in myList) {          Console.WriteLine(i);       }       int prod = 1;       foreach(int i in myList) {          prod = prod*i;       }       Console.WriteLine("Product: {0}",prod);    } }OutputList: 5 10 7 Product: 350

Read More

C# program to check for a string that contains all vowels

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 660 Views

To check for all vowels, firstly set the condition to check −string res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();Above, we have used the string −string str = "the quick brown fox jumps over the lazy dog";Now, using the Any() method checks whether the string has any vowels or not −if(!res.Any()) Console.WriteLine("No vowels!");Loop through the string to get the vowels −Exampleusing System; using System.Linq; public class Program {    public static void Main() {       string str = "the quick brown fox jumps over the lazy dog";       var res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();       ...

Read More

How to convert a number from Decimal to Binary using recursion in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 507 Views

To get the binary of Decimal, using recursion, firstly set the decimal number −int dec = 30;Now pass the value to a function −public int displayBinary(int dec) { }Now, check the condition until the decimal value is 0 and using recursion get the mod 2 of the decimal num as shown below. The recursive call will call the function again with the dec/2 value −public int displayBinary(int dec) {    int res;    if (dec != 0) {       res = (dec % 2) + 10 * displayBinary(dec / 2);       Console.Write(res);       return ...

Read More

How to Convert Hex String to Hex Number in C#?

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

Firstly, set the Hex String −string str = "7D";Now, use the Convert.ToSByte() method to convert the Hex string to Hex number −Console.WriteLine(Convert.ToSByte(str, 16));Let us see the complete code −Exampleusing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string str = "7D";          Console.WriteLine(Convert.ToSByte(str, 16));       }    } }Output125Another way of converting Hex String to Hex Number −Exampleusing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string str = "7D";          Console.WriteLine(Convert.ToInt32(str, 16));       }    } }Output125

Read More

C# program to check whether a given string is Heterogram or not

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 448 Views

Heterogram for a string means the string isn’t having duplicate letters. For example −Mobile Cry LaptopLoop through each word of the string until the length of the string −for (int i = 0; i < len; i++) {    if (val[str[i] - 'a'] == 0)    val[str[i] - 'a'] = 1;    else    return false; }Above, len is the length of the string.Let us see the complete code −Exampleusing System; public class GFG {    static bool checkHeterogram(string str, int len) {       int []val = new int[26];       for (int i = ...

Read More
Showing 911–920 of 1,951 articles
« Prev 1 90 91 92 93 94 196 Next »
Advertisements