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
Server Side Programming Articles
Page 762 of 2109
C# Program to Create a Thread Pool
A thread pool in C# is a collection of pre-created threads that can be reused to execute multiple tasks efficiently. Instead of creating new threads for each task, the thread pool manages a pool of worker threads, reducing the overhead of thread creation and destruction. The ThreadPool class provides methods to queue work items for execution by available threads in the pool. This approach is particularly useful for short-running tasks that don't require dedicated threads. Syntax Following is the syntax for queueing a method for execution in the thread pool − ThreadPool.QueueUserWorkItem(new WaitCallback(methodName)); ...
Read MoreC# Program to find whether the Number is Divisible by 2
A number is divisible by 2 if the remainder is 0 when the number is divided by 2. This is checked using the modulo operator (%) in C#, which returns the remainder of a division operation. Numbers divisible by 2 are called even numbers, while numbers not divisible by 2 are called odd numbers. Divisibility by 2 Check Even Numbers num % 2 == 0 Examples: 2, 4, 6, 8, 10 Odd Numbers num % 2 ...
Read MoreC# 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 More