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
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to select a random element from a C# list?
Selecting a random element from a C# list is a common task in programming. This involves using the Random class to generate a random index within the bounds of the list, then accessing the element at that index. Syntax Following is the basic syntax for selecting a random element from a list − Random random = new Random(); int index = random.Next(list.Count); var randomElement = list[index]; Using Random.Next() Method The Random.Next() method generates a random integer between 0 (inclusive) and the specified maximum value (exclusive). When you pass list.Count as the parameter, it ...
Read MoreDateTime.AddTicks() Method in C#
The DateTime.AddTicks() method in C# is used to add a specified number of ticks to the value of this instance. It returns a new DateTime representing the adjusted time. A tick represents 100 nanoseconds, making this method useful for precise time calculations. Syntax Following is the syntax − public DateTime AddTicks(long ticks); Parameters ticks: A long value representing the number of ticks to add. Each tick equals 100 nanoseconds. Positive values add time, while negative values subtract time. Return Value Returns a new DateTime object whose value is the sum of ...
Read MoreC# program to check for URL in a String
In C#, you can check for URLs in a string using various methods such as StartsWith() for simple prefix matching or regular expressions for more comprehensive URL validation. The StartsWith() method is useful when you need to verify if a string begins with a specific URL pattern. Syntax Following is the syntax for using StartsWith() to check URL prefixes − string.StartsWith("url_prefix") For checking multiple URL patterns, you can combine conditions using logical operators − if (input.StartsWith("https://www.") || input.StartsWith("https://")) { // URL found } Using StartsWith() for Simple ...
Read MoreTry/catch/finally/throw keywords in C#
Exception handling in C# is implemented using four key keywords that work together to manage runtime errors gracefully. The try block contains code that might throw an exception, catch blocks handle specific exceptions, finally executes cleanup code regardless of whether an exception occurs, and throw is used to raise exceptions manually. Syntax Following is the basic syntax for exception handling in C# − try { // code that might throw an exception } catch (SpecificExceptionType ex) { // handle specific exception } catch (Exception ex) { // ...
Read MoreToDictionary method in C#
The ToDictionary method is a LINQ extension method in C# that converts any collection into a Dictionary. It allows you to specify how to extract the key and value from each element in the source collection. Syntax Following is the basic syntax for the ToDictionary method − source.ToDictionary(keySelector) source.ToDictionary(keySelector, valueSelector) source.ToDictionary(keySelector, valueSelector, comparer) Parameters keySelector − A function to extract the key from each element. valueSelector − A function to extract the value from each element (optional). comparer − An equality comparer to compare keys (optional). ...
Read MoreC# SingleorDefault() Method
The SingleOrDefault() method in C# returns a single specific element from a sequence. If no element is found, it returns the default value for the type. If multiple elements are found, it throws an exception. This method is part of LINQ and can be used with any IEnumerable collection. It's particularly useful when you expect exactly zero or one element in the result. Syntax Following is the basic syntax for SingleOrDefault() − public static TSource SingleOrDefault(this IEnumerable source) With a predicate condition − public static TSource SingleOrDefault(this IEnumerable source, Func predicate) ...
Read MoreC# 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 need to verify that it has exactly two divisors. Algorithm To determine if a number is prime, we use a for loop to check all numbers from 1 to the given number. We count how many divisors exist by checking if the remainder is zero when dividing the number by each potential divisor − for (int i = 1; i
Read MoreHow to define custom methods in C#?
To define a custom method in C#, you create a reusable block of code that performs a specific task. Methods help organize your code, make it more readable, and avoid repetition by allowing you to call the same functionality multiple times. Syntax Following is the syntax for defining a custom method in C# − (Parameter List) { Method Body } Method Components The following are the various elements of a method − Access Specifier − This determines the visibility of a method from other classes. ...
Read MoreContainsValue in C#
The ContainsValue() method in C# is used to determine whether a Dictionary contains a specific value. It returns true if the value exists in the dictionary, and false otherwise. This method performs a sequential search through all values in the dictionary. Syntax Following is the syntax for using the ContainsValue() method − bool result = dictionary.ContainsValue(value); Parameters value − The value to search for in the dictionary. The type must match the value type of the dictionary. Return Value Returns true if the dictionary contains the specified value; otherwise, ...
Read MoreHow to sort a list of dictionaries by values of dictionaries in C#?
Sorting a list of dictionaries by their values is a common requirement in C# programming. There are multiple approaches to achieve this, ranging from sorting by keys to sorting by values, and even sorting lists containing multiple dictionaries. Syntax Following is the syntax for sorting dictionary entries by values using LINQ − var sortedByValue = dictionary.OrderBy(x => x.Value); var sortedByValueDesc = dictionary.OrderByDescending(x => x.Value); For sorting by keys − var sortedByKey = dictionary.OrderBy(x => x.Key); Sorting Dictionary by Keys The simplest approach is to sort dictionary entries by their ...
Read More