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 by Ankith Reddy
Page 3 of 73
Full Date Short Time ("f") Format Specifier in C#
The Full Date Short Time ("f") format specifier in C# represents a combination of the long date ("D") and short time ("t") patterns. It displays the complete date information along with hours and minutes in a readable format. This format specifier is culture-sensitive and will display dates according to the specified culture's formatting conventions. Syntax Following is the syntax for using the "f" format specifier − DateTime.ToString("f") DateTime.ToString("f", CultureInfo.CreateSpecificCulture("culture-code")) Using "f" Format Specifier with Default Culture Example using System; class Demo { static void Main() { ...
Read MoreCompute modulus division by a power-of-2-number in C#
Computing modulus division by a power-of-2 number in C# can be optimized using bitwise operations. When the divisor is a power of 2 (like 2, 4, 8, 16, etc.), we can use the bitwise AND operation with (divisor - 1) instead of the traditional modulus operator for better performance. This optimization works because powers of 2 in binary have only one bit set, and subtracting 1 creates a mask that isolates the relevant lower bits. Syntax Following is the syntax for computing modulus with a power-of-2 divisor − result = dividend & (divisor - 1); ...
Read MoreWhat is Cast Operator () in C#?
The cast operator () in C# is used for explicit type conversion, converting one data type to another when an implicit conversion is not available or when you want to force a specific conversion. It requires you to explicitly specify the target type in parentheses before the value or variable. Syntax Following is the syntax for using the cast operator − (target_type) expression Where target_type is the type you want to convert to, and expression is the value or variable being converted − int result = (int) doubleValue; float floatResult = (float) ...
Read MoreHow to find the length of jagged array using a property?
A jagged array in C# is an array of arrays where each inner array can have different lengths. To find the length of a jagged array, you use the Length property, which returns the number of inner arrays (rows) in the jagged array. Syntax Following is the syntax for declaring a jagged array − dataType[][] arrayName = new dataType[size][]; To get the length of a jagged array, use the Length property − int numberOfRows = arrayName.Length; Jagged Array Structure [0, 0] ...
Read MoreC# Console.WindowLeft Property
The Console.WindowLeft property in C# gets or sets the leftmost position of the console window area relative to the screen buffer. This property is useful when you need to control or retrieve the horizontal position of the console window within the buffer area. Syntax Following is the syntax to get the left position of the console window − int position = Console.WindowLeft; Following is the syntax to set the left position of the console window − Console.WindowLeft = value; Return Value The property returns an int value representing the ...
Read MoreException Propagation in C#
Exception propagation in C# refers to the process of how exceptions move up through the call stack when they are not handled by the current method or try-catch block. When an exception occurs, the runtime searches for an appropriate exception handler, and if none is found, the exception propagates to the calling method. How Exception Propagation Works When an exception occurs in a try block, the runtime checks the corresponding catch blocks to see if they can handle the exception. If no matching catch block is found, the exception propagates to a higher-level try block or to the ...
Read MoreC# Program to replace a character with asterisks in a sentence
The Replace() method in C# is used to replace all occurrences of a specified character or string with another character or string. This method is particularly useful when you need to replace special characters like asterisks with other characters in a sentence. Syntax Following is the syntax for replacing a character using the Replace() method − string newString = originalString.Replace(oldChar, newChar); For replacing a string with another string − string newString = originalString.Replace(oldString, newString); Parameters oldChar/oldString: The character or string to be replaced newChar/newString: The character or string to replace with Return Value The Replace()
Read MoreC# Program to get current day of week
Use the DateTime.DayOfWeek property to get the current day of the week in C#. This property returns a DayOfWeek enumeration value representing the day. Syntax Following is the syntax to get the current day of the week − DayOfWeek dayOfWeek = DateTime.Today.DayOfWeek; You can also use DateTime.Now.DayOfWeek to include the current time − DayOfWeek dayOfWeek = DateTime.Now.DayOfWeek; Using DateTime.Today.DayOfWeek The DateTime.Today property gets the current date with the time component set to 00:00:00. Using DayOfWeek on this returns the current day as an enumeration value − using ...
Read MoreHow to find the length and rank of a jagged array in C#?
A jagged array in C# is an array of arrays where each sub-array can have different lengths. Unlike multi-dimensional arrays, jagged arrays provide flexibility in storing varying amounts of data in each row. To find the length and rank of a jagged array, you can use the Length property, GetLowerBound() and GetUpperBound() methods, and the Rank property. Syntax Following is the syntax for declaring a jagged array − dataType[][] arrayName = new dataType[rows][]; Following are the methods and properties to find length and rank − arrayName.Length ...
Read MoreC# program to find maximum and minimum element in an array
To find the maximum and minimum elements in an array in C#, we initialize both values to the first element of the array and then compare each subsequent element. This approach ensures we correctly identify the extreme values regardless of the array's content. Algorithm The algorithm follows these steps − Initialize both max and min variables to the first array element. Iterate through the array starting from the second element (index 1). Compare each element with the current max and min values. Update max and min accordingly when a larger or smaller element is found. ...
Read More