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
Programming Articles
Page 827 of 2547
DateTime.AddSeconds() Method in C#
The DateTime.AddSeconds() method in C# is used to add the specified number of seconds to the value of this instance. This returns a new DateTime object without modifying the original DateTime instance. Syntax Following is the syntax − public DateTime AddSeconds(double sec); Parameters sec − A double value representing the number of seconds to be added. If you want to subtract seconds, then set a negative value. The value can include fractional seconds. Return Value This method returns a new DateTime object that represents the date and time after adding the ...
Read MoreC# Multiple Local Variable Declarations
In C#, you can declare and initialize multiple local variables of the same type in a single statement using the comma operator. This provides a concise way to declare several variables at once, making your code more readable and reducing repetition. Syntax Following is the syntax for declaring multiple local variables of the same type − dataType variable1 = value1, variable2 = value2, variable3 = value3; You can also declare variables without initialization − dataType variable1, variable2, variable3; Basic Multiple Variable Declaration The following example demonstrates declaring four integer ...
Read MoreWhat is the maximum possible value of an integer in C# ?
The maximum possible value of a 32-bit signed integer (int) in C# is 2, 147, 483, 647. This value is also available through the constant int.MaxValue. However, C# provides several integer types with different ranges and maximum values. Integer Types and Their Maximum Values C# offers multiple integer data types, each with different storage sizes and value ranges − Type Size Range Maximum Value sbyte 8-bit signed -128 to 127 127 byte 8-bit unsigned 0 to 255 255 short 16-bit signed -32, 768 to 32, 767 ...
Read MoreWhat is the difference between String and string in C#?
In C#, string and String are functionally identical − string is simply an alias for System.String. Both refer to the same .NET type and can be used interchangeably in your code. Syntax Both declarations are equivalent − string str1 = "Hello World"; String str2 = "Hello World"; Both can use static methods from the String class − string result1 = string.Format("Hello {0}", name); string result2 = String.Format("Hello {0}", name); Key Differences string (lowercase) String (uppercase) C# keyword and alias Actual .NET class ...
Read MoreFind a specific element in a C# List
In C#, you can find specific elements in a List using various built-in methods. The most common approach is using the Find() method with a lambda expression to specify search criteria. Syntax Following is the syntax for using the Find() method − T element = list.Find(predicate); Where predicate is a lambda expression that returns true for the element you want to find. Using Find() Method The Find() method returns the first element that matches the specified condition. If no element is found, it returns the default value for the type (0 for ...
Read MoreC# Program to return the only element that satisfies a condition
The Single() method in C# returns the only element from a sequence that satisfies a specified condition. If no element or more than one element satisfies the condition, it throws an InvalidOperationException. This method is part of LINQ and is commonly used when you expect exactly one element to match your criteria. Syntax Following is the syntax for the Single() method − // Without condition - returns the only element public static T Single(this IEnumerable source) // With condition - returns the only element that matches the predicate public static T Single(this IEnumerable source, ...
Read MoreHow 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 More