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 98 of 196
Time Functions in C#
The DateTime structure in C# provides numerous methods and properties for working with dates and times. These time functions allow you to manipulate DateTime instances by adding or subtracting specific time intervals, extracting time components, and performing various time-related operations. Time functions in C# are essential for applications that need to handle scheduling, logging, time calculations, and date arithmetic operations. Common Time Functions The following table shows the most commonly used time manipulation methods in the DateTime class − Method Description AddDays(Double) Returns a new DateTime that adds the ...
Read MoreHow to display Absolute value of a number in C#?
To find the absolute value of a number in C#, use the Math.Abs method. The absolute value represents the distance of a number from zero on the number line, always returning a non-negative result. Syntax The Math.Abs method is overloaded to work with different numeric types − Math.Abs(int value) Math.Abs(long value) Math.Abs(float value) Math.Abs(double value) Math.Abs(decimal value) Parameters value − The number whose absolute value is to be computed. Return Value Returns the absolute value of the specified number. If the number is positive or zero, it ...
Read MoreTrigonometric Functions in C#
Trigonometric functions in C# are part of the Math class in the System namespace. These functions enable you to perform mathematical calculations involving angles, including basic trigonometric operations (Sin, Cos, Tan) and their inverse functions (Asin, Acos, Atan). All angle measurements in C# trigonometric functions are in radians, not degrees. To convert degrees to radians, multiply by π/180. Common Trigonometric Functions Function Description Return Type Math.Sin(double) Returns the sine of the specified angle double Math.Cos(double) Returns the cosine of the specified angle double Math.Tan(double) Returns the ...
Read MoreHow to capture file not found exception in C#?
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 MoreHow to capture out of memory exception in C#?
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 MoreHow to capture index out of range exception in C#?
The IndexOutOfRangeException occurs when you try to access an array element with an index that is outside the bounds of the array. This is a common runtime exception in C# that can be captured using try-catch blocks. Understanding Array Bounds Arrays in C# are zero-indexed, meaning the first element is at index 0. If an array has 5 elements, the valid indices are 0, 1, 2, 3, and 4. Accessing index 5 or higher will throw an IndexOutOfRangeException. Array Index Bounds ...
Read MoreHow to control for loop using break and continue statements in C#?
The break and continue statements provide powerful control flow mechanisms within for loops in C#. The break statement terminates the loop entirely, while the continue statement skips the current iteration and moves to the next one. Syntax Following is the syntax for using break statement in a for loop − for (initialization; condition; increment) { if (someCondition) { break; // exits the loop completely } // other statements } Following is the syntax for using continue statement in a ...
Read MoreWhat does Array.IsSynchronized property of array class do in C#?
The Array.IsSynchronized property in C# gets a boolean value indicating whether access to the Array is synchronized (thread-safe). This property is part of the ICollection interface implementation and helps determine if an array can be safely accessed by multiple threads simultaneously. For standard C# arrays, this property always returns false, meaning that arrays are not inherently thread-safe. When multiple threads need to access the same array, you must implement your own synchronization using the SyncRoot property or other synchronization mechanisms. Syntax Following is the syntax for the Array.IsSynchronized property − public bool IsSynchronized { get; ...
Read MoreHow to determine if the string has all unique characters using C#?
To determine if a string has all unique characters, you need to check if any character appears more than once. This can be accomplished using several approaches in C#, from simple nested loops to more efficient data structures like HashSet. Using Nested Loop Approach The basic approach involves comparing each character with every other character in the string − using System; class Program { public static bool HasUniqueCharacters(string val) { for (int i = 0; i < val.Length - 1; i++) { ...
Read MoreHow to use ReadKey() method of Console class in C#?
The Console.ReadKey() method in C# reads the next character or function key pressed by the user. This method is commonly used to pause program execution until the user presses a key, which is particularly useful when running console applications from Visual Studio to prevent the console window from closing immediately. Syntax The Console.ReadKey() method has two overloads − public static ConsoleKeyInfo ReadKey(); public static ConsoleKeyInfo ReadKey(bool intercept); Parameters intercept (optional): A boolean value that determines whether to display the pressed key in the console. If true, the key is not displayed; if ...
Read More