Programming Articles

Page 98 of 2547

Write a C# program to check if a number is Palindrome or not

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

A palindrome is a number or string that reads the same forward and backward. In C#, we can check if a number is a palindrome by reversing its digits and comparing it with the original number. Syntax Following is the syntax for reversing a string using Array.Reverse() method − char[] charArray = string.ToCharArray(); Array.Reverse(charArray); string reversedString = new string(charArray); Following is the syntax for comparing strings ignoring case − bool isEqual = string1.Equals(string2, StringComparison.OrdinalIgnoreCase); Using String Reversal Method The most straightforward approach is to convert the number to a ...

Read More

How to capture file not found exception in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 435 Views

The FileNotFoundException is thrown when you try to access a file that does not exist on the system. This commonly occurs when using classes like StreamReader, File.ReadAllText(), or other file I/O operations with an incorrect file path. Syntax Following is the syntax for handling FileNotFoundException using try-catch − try { // File operation that might throw FileNotFoundException } catch (FileNotFoundException ex) { // Handle the exception Console.WriteLine("File not found: " + ex.Message); } Using StreamReader with Exception Handling When using StreamReader to read a ...

Read More

C# program to find the most frequent element

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

Finding the most frequent element in a string is a common programming problem that involves counting the occurrences of each character and determining which appears most often. In C#, this can be efficiently solved using an array to track character frequencies. Syntax Following is the basic approach to count character frequencies − int[] frequency = new int[256]; // ASCII character set for (int i = 0; i < str.Length; i++) { frequency[str[i]]++; } Using Array-Based Frequency Counting The most straightforward approach uses an integer array where each index corresponds ...

Read More

Convert.ToChar Method in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 1K+ Views

The Convert.ToChar() method in C# converts a specified value to its equivalent Unicode character representation. This method accepts various data types including integers, bytes, strings, and other numeric types, and returns a char value. Syntax Following are the common overloads of the Convert.ToChar() method − public static char ToChar(byte value) public static char ToChar(int value) public static char ToChar(string value) public static char ToChar(object value) Parameters value − The value to be converted to a Unicode character. This can be a numeric type, string, or object. Return Value ...

Read More

Uri.ToString() Method in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 293 Views

The Uri.ToString() method in C# is used to get a canonical string representation for the specified Uri instance. This method returns the complete URI as a formatted string, including all components such as scheme, host, path, query, and fragment. Syntax Following is the syntax − public override string ToString(); Return Value This method returns a string that contains the unescaped canonical representation of the Uri instance. All characters are unescaped except #, ?, and %. Using Uri.ToString() Method Basic Example Let us see how to use the Uri.ToString() method to ...

Read More

Write a C# program to calculate a factorial using recursion

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 3K+ Views

Factorial of a number is the product of all positive integers less than or equal to that number. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. We can calculate factorial using recursion, where a function calls itself with a smaller value until it reaches the base case. Syntax Following is the syntax for a recursive factorial function − public int Factorial(int n) { if (n == 0 || n == 1) return 1; else ...

Read More

How to capture out of memory exception in C#?

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

The System.OutOfMemoryException occurs when the CLR fails to allocate sufficient memory for an operation. This exception is inherited from the System.SystemException class and can be caught using try-catch blocks to handle memory allocation failures gracefully. Common scenarios that trigger this exception include attempting to create very large arrays, working with StringBuilder objects that exceed their capacity, or processing extremely large datasets. Syntax Following is the syntax for catching OutOfMemoryException − try { // Code that might throw OutOfMemoryException } catch (OutOfMemoryException ex) { // Handle the exception ...

Read More

C# program to return an array from methods

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 12K+ Views

In C#, methods can return arrays by specifying the array type in the method signature and using the return statement. This allows you to create, populate, and return arrays from methods to be used by the calling code. Syntax Following is the syntax for declaring a method that returns an array − dataType[] MethodName() { // create and populate array return arrayVariable; } To call the method and use the returned array − dataType[] result = MethodName(); Returning String Arrays from Methods ...

Read More

Convert.ToDateTime Method in C#

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

The Convert.ToDateTime method in C# converts a string representation of a date and time to a DateTime object. This method is commonly used when you need to parse date strings from user input, files, or external data sources into a usable DateTime format. The method supports various date formats and provides automatic parsing based on the current culture settings of your system. Syntax Following are the most commonly used overloads of Convert.ToDateTime − public static DateTime ToDateTime(string value) public static DateTime ToDateTime(string value, IFormatProvider provider) public static DateTime ToDateTime(object value) Parameters ...

Read More

How do you sort an array in C# in ascending order?

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 10K+ Views

In C#, you can sort an array in ascending order using the built-in Array.Sort() method. This method modifies the original array directly and arranges elements from smallest to largest value. Syntax Following is the syntax for sorting an array in ascending order − Array.Sort(arrayName); Parameters arrayName − The array to be sorted. This parameter is required and the array is modified in-place. Using Array.Sort() Method The Array.Sort() method automatically sorts elements in ascending order. Here's how to sort an integer array − using System; ...

Read More
Showing 971–980 of 25,467 articles
« Prev 1 96 97 98 99 100 2547 Next »
Advertisements