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 826 of 2109
Display the default if element is not found in a C# List
When working with C# Lists, attempting to access an element that doesn't exist can throw exceptions. To safely handle empty lists or missing elements, you can use methods like FirstOrDefault() which return the default value for the type instead of throwing an exception. For reference types like strings, the default value is null. For value types like int, float, or double, the default value is 0. For bool, it's false. Syntax Following is the syntax for using FirstOrDefault() on a List − list.FirstOrDefault(); list.FirstOrDefault(condition); You can also specify a custom default value using ...
Read MoreDifference between TimeSpan Seconds() and TotalSeconds()
The TimeSpan.Seconds property returns only the seconds component of a time span, whereas TimeSpan.TotalSeconds property converts the entire time duration into seconds. Understanding this difference is crucial when working with time calculations in C#. Syntax Following is the syntax for accessing the seconds component − TimeSpan ts = new TimeSpan(days, hours, minutes, seconds, milliseconds); int seconds = ts.Seconds; Following is the syntax for getting total seconds − TimeSpan ts = new TimeSpan(days, hours, minutes, seconds, milliseconds); double totalSeconds = ts.TotalSeconds; How It Works TimeSpan: 1 ...
Read MoreConvert.ToChar Method in C#
The Convert.ToChar() method in C# converts a specified value to its equivalent Unicode character representation. This method accepts various data types including integers, bytes, strings, and other numeric types, and returns a char value. Syntax Following are the common overloads of the Convert.ToChar() method − public static char ToChar(byte value) public static char ToChar(int value) public static char ToChar(string value) public static char ToChar(object value) Parameters value − The value to be converted to a Unicode character. This can be a numeric type, string, or object. Return Value ...
Read MoreConvert.ToDateTime Method in C#
The Convert.ToDateTime method in C# converts a string representation of a date and time to a DateTime object. This method is commonly used when you need to parse date strings from user input, files, or external data sources into a usable DateTime format. The method supports various date formats and provides automatic parsing based on the current culture settings of your system. Syntax Following are the most commonly used overloads of Convert.ToDateTime − public static DateTime ToDateTime(string value) public static DateTime ToDateTime(string value, IFormatProvider provider) public static DateTime ToDateTime(object value) Parameters ...
Read MoreDifferent Star Pattern Programs in C#
Star pattern programs are common programming exercises that help developers practice loops and understand nested iteration logic. C# provides simple syntax to create various star patterns using for loops and Console.Write() methods. Common Star Pattern Types Right Triangle * ** Pyramid * *** Inverted *** ** Diamond * *** Right Triangle Pattern This pattern creates a right-angled triangle where each row contains one more star than the previous row − using System; class Program { static void Main() { for (int i = 1; i
Read MoreC# Program to merge sequences
In C#, you can merge sequences using the Zip method from LINQ. The Zip method combines elements from two sequences by pairing them together using a specified function. Syntax The Zip method syntax is − sequence1.Zip(sequence2, (first, second) => result) Parameters sequence1 − The first sequence to merge sequence2 − The second sequence to merge resultSelector − A function that defines how to combine elements from both sequences How It Works The Zip method pairs elements from two sequences by their index position. If sequences have different lengths, the ...
Read MoreC# Program to create a LinkedList
A LinkedList in C# is a generic collection that stores elements in nodes, where each node contains the data and references to the next and previous nodes. Unlike arrays, LinkedList elements are not stored in contiguous memory locations and allow efficient insertion and removal operations. Syntax Following is the syntax for creating a LinkedList − LinkedList listName = new LinkedList(); You can also create a LinkedList from an existing collection − LinkedList listName = new LinkedList(collection); Creating LinkedList from Array You can create a LinkedList by passing an array ...
Read MoreC# Program to invert the order of elements in a sequence
In C#, you can invert the order of elements in a sequence using several built-in methods. The most common approaches include using Queryable.Reverse() with LINQ, Array.Reverse(), and Enumerable.Reverse(). Each method has its specific use cases and performance characteristics. Syntax Following is the syntax for using Queryable.Reverse() method − IQueryable result = sequence.AsQueryable().Reverse(); Following is the syntax for using Array.Reverse() method − Array.Reverse(array); Following is the syntax for using Enumerable.Reverse() method − IEnumerable result = sequence.Reverse(); Using Queryable.Reverse() Method The Queryable.Reverse() method returns an IQueryable that ...
Read MoreC# Program to return a collection with repeated elements
To return a collection with repeated elements in C#, use the Enumerable.Repeat method from the System.Linq namespace. This method creates a sequence that contains one repeated value a specified number of times. Syntax Following is the syntax for Enumerable.Repeat method − public static IEnumerable Repeat(TResult element, int count) Parameters element − The value to be repeated in the result sequence. count − The number of times to repeat the value in the generated sequence. Return Value Returns an IEnumerable that contains a repeated value. ...
Read MoreConvert.ToInt32 Method in C#
The Convert.ToInt32() method in C# converts a specified value to a 32-bit signed integer. This method provides type conversion from various data types including string, double, float, decimal, and bool to int. The method uses rounding to nearest even (banker's rounding) when converting floating-point numbers, and throws exceptions for invalid conversions like null strings or out-of-range values. Syntax Following are the common overloads of Convert.ToInt32() method − Convert.ToInt32(object value) Convert.ToInt32(string value) Convert.ToInt32(double value) Convert.ToInt32(bool value) Parameters value − The value to convert to a 32-bit signed integer. Return Value ...
Read More