Csharp Articles

Found 1,951 articles

Infinity or Exception in C# when divide by 0?

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

Divide by zero is the System.DivideByZeroException, which is a class that handles errors generated from dividing a dividend with zero.Let us see an example.Exampleresult = num1 / num2;Above, if num2 is set to 0, then the DivideByZeroException is caught since we have handled exception.

Read More

C# Program to convert Digits to Words

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

Firstly, declare the words from 0 to 9 −// words for every digits from 0 to 9 string[] digits_words = { "zero", "one", "two",    "three", "four", "five",    "six", "seven", "eight",    "nine" };The following are the digits to be converted to words −// number to be converted into words val = 4677; Console.WriteLine("Number: " + val);Use the loop to check for every digit in the given number and convert into words −do {    next = val % 10;    a[num_digits] = next;    num_digits++;    val = val / 10; } while(val > 0);ExampleYou can try to ...

Read More

C# Program to Convert Decimal to Binary\\n

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

Let’ s say you have set the decimal to be −decVal = 34; Console.WriteLine("Decimal: {0}", decVal);Use the ToString() method for the values you get as a binary number for the decimal value −while (decVal >= 1) {    val = decVal / 2;    a += (decVal % 2).ToString();    decVal = val; }Now set a new empty variable to display the binary number using a loop −string binValue = "";ExampleYou can try to run the following code to convert decimal to binary in C#.using System; using System.Collections.Generic; using System.Text; namespace Demo {    class MyApplication {       ...

Read More

What is the difference between a class and an object in C#?

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

When you define a class, you define a blueprint for a data type.Objects are instances of a class. The methods and variables that constitute a class are called members of the class.To access the class members, you use the dot (.) operator after the object name. The dot operator links the name of an object with the name of a member for example, Box Box1 = new Box();Above you can see Box1 is our object. We will use it to access the members −Box1.height = 7.0;You can also use it to call member functions −Box1.getVolume();The following is an example showing ...

Read More

Delete a file in C#

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

Use File.Delete method to delete a file.Firstly, set the path of the file you want to delete.String myPath = @"C:\New\amit.txt";Now, use the File.Delete method to delete the file.File.Delete(myPath);The following is the complete code −Exampleusing System; using System.IO; public class Program {    public static void Main() {       String myPath = @"C:\New\amit.txt";       Console.WriteLine("Deleting File");       File.Delete(myPath);    } }OutputDeleting File

Read More

Represent Int64 as a Hexadecimal String in C#

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

To represent Int64 as a Binary string in C#, use the ToString() method and set the base as the ToString() method’s second parameter i.e.16 for Hexadecimal.Int64 represents a 64-bit signed integer.Firstly, set an Int64 variable.long val = 947645;Now, convert it to a hex string by including 16 as the second parameter.Convert.ToString(val, 16)Exampleusing System; class Demo {    static void Main() {       long val = 947645;       Console.WriteLine("Long: "+val);       Console.Write("Hex String: "+Convert.ToString(val, 16));    } }OutputLong: 947645 Hex String: e75bd

Read More

Sort the words in lexicographical order in C#

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

Firstly, set a string array −string[] arr = new string[] {    "Indian",    "Moroccon",    "American", };To sort the words in lexicographical order −var sort = from a in arr orderby a select a;Exampleusing System; using System.Linq; class Program {    static void Main() {       string[] arr = new string[] {          "Indian",          "Moroccon",          "American",       };       var sort = from a in arr       orderby a       select a;       foreach(string res in sort) {          Console.WriteLine(res);       }    } }outputAmerican Indian Moroccon

Read More

How to find a matching substring using regular expression in C#?

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

Our string is − string str = " My make "; Use the following regular expression to find the substring “make” @"\bmake\b" Here is the complete code − Example using 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); ...

Read More

Print first m multiples of n in C#

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

To print m multiples of n, first set the value of m and n − int n = 6, m = 1; Now loop through the value of m, increment it and multiply with n on every iteration − while (m

Read More

How to sort 0,1,2 in an Array (Dutch National Flag) without extra space using C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 287 Views

We need to take three-pointers, low, mid, high. We will use low and mid pointers at the start, and the high pointer will point at the end of the given array.If array [mid] =0, then swap array [mid] with array [low] and increment both pointers once.If array [mid] = 1, then no swapping is required. Increment mid pointer once.If array [mid] = 2, then we swap array [mid] with array [high] and decrement the high pointer once.Time complexity − O(N)Exampleusing System; namespace ConsoleApplication{    public class Arrays{       private void Swap(int[] arr, int pos1, int pos2){     ...

Read More
Showing 1–10 of 1,951 articles
« Prev 1 2 3 4 5 196 Next »
Advertisements