Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Csharp Articles
Page 87 of 196
C# program to find the sum of digits of a number using Recursion
A recursive function calls itself with modified parameters until it reaches a base case. To find the sum of digits of a number using recursion, we extract the last digit using the modulus operator (%) and add it to the sum of remaining digits. Syntax Following is the syntax for a recursive function to sum digits − public int SumOfDigits(int number) { if (number == 0) { return 0; // base case } return (number % 10) + SumOfDigits(number / ...
Read MoreCompare two strings lexicographically in C#
The String.Compare() method in C# performs lexicographic (alphabetical) comparison between two strings. It compares strings character by character based on their Unicode values and is case-sensitive by default. Syntax Following is the syntax for the basic String.Compare() method − int result = string.Compare(string1, string2); For case-insensitive comparison − int result = string.Compare(string1, string2, true); Return Value The String.Compare() method returns an integer value indicating the lexicographic relationship between the two strings − If str1 is less than str2, it returns -1. If str1 is equal to str2, ...
Read MoreComments in C#
Comments in C# are used to document and explain code, making it more readable and maintainable. The C# compiler ignores all comments during compilation, so they don't affect the program's performance. C# supports three types of comments: single-line, multi-line, and XML documentation comments. Syntax Following is the syntax for single-line comments − // This is a single-line comment Following is the syntax for multi-line comments − /* This is a multi-line comment that spans multiple lines */ Following is the syntax for XML documentation comments − ...
Read MoreWrite a C# program to check if a number is Palindrome or not
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 MoreWrite a C# program to calculate a factorial using recursion
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 MoreHow to use 'as' operator in C#?
The as operator in C# performs safe type conversions between compatible reference types. Unlike direct casting, the as operator returns null if the conversion fails instead of throwing an exception, making it ideal for checking type compatibility. The as operator can perform reference conversions, nullable conversions, and boxing conversions, but cannot perform user-defined conversions or value type conversions (except nullable types). Syntax Following is the syntax for using the as operator − TargetType variable = expression as TargetType; If the conversion succeeds, variable contains the converted value. If it fails, variable is null. ...
Read MoreWrite a C# program to check if a number is prime or not
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number is prime in C#, we count how many divisors it has by iterating through all numbers from 1 to the number itself. The basic approach uses a counter that increments each time we find a divisor. If the counter equals 2 at the end (divisible only by 1 and itself), the number is prime. Algorithm The algorithm for checking prime numbers follows these steps − Initialize a counter to 0 ...
Read MoreHow to use the sleep method in C#?
The Thread.Sleep() method in C# is used to pause the execution of the current thread for a specified period of time. This is commonly used to introduce delays in program execution or to simulate time-consuming operations. Syntax The Thread.Sleep() method has two overloads − Thread.Sleep(int millisecondsTimeout); Thread.Sleep(TimeSpan timeout); Parameters millisecondsTimeout − The number of milliseconds for which the thread is suspended. Use Timeout.Infinite (-1) to suspend the thread indefinitely. timeout − A TimeSpan that sets the amount of time for which the thread is suspended. Using Thread.Sleep() with Milliseconds ...
Read MoreHow to use RightShift Operators in C#?
The right shift operator (>>) in C# moves the bits of the left operand to the right by the number of positions specified by the right operand. This operation effectively divides the number by powers of 2. Syntax Following is the syntax for the right shift operator − result = operand >> numberOfPositions; Where operand is the value whose bits will be shifted, and numberOfPositions specifies how many positions to shift right. How Right Shift Works The right shift operator moves each bit to the right by the specified number of positions. ...
Read MoreHow do you loop through a C# array?
To loop through an array in C#, you can use several loop types including for, foreach, while, and do...while loops. Each loop provides different ways to iterate through array elements and access their values. The most commonly used loops for arrays are the for loop (when you need index access) and the foreach loop (when you only need element values). Syntax Following is the syntax for different loop types with arrays − // for loop for (int i = 0; i < array.Length; i++) { // access array[i] } // ...
Read More