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 146 of 196
C# Program to filter array elements based on a predicate
In C#, filtering array elements based on a predicate allows you to select elements that meet specific conditions. A predicate is a function that returns true or false for each element, determining whether it should be included in the result. The most common approach is using LINQ's Where method, which applies a predicate function to filter elements. You can also use traditional loops or the Array.FindAll method for filtering. Syntax Following is the syntax for filtering with LINQ's Where method − IEnumerable result = array.Where(element => condition); Following is the syntax for filtering ...
Read MoreConvert.ToDecimal Method in C#
The Convert.ToDecimal() method in C# converts a specified value to a decimal number. This method can convert various data types including strings, integers, floating-point numbers, and other numeric types to the decimal type, which provides high precision for financial and monetary calculations. Syntax Following are the common syntax forms for Convert.ToDecimal() − decimal result = Convert.ToDecimal(value); decimal result = Convert.ToDecimal(stringValue, IFormatProvider); Parameters value − The value to convert to decimal. Can be string, int, double, float, bool, or other convertible types. IFormatProvider − Optional culture-specific formatting information for string ...
Read MoreSet tuple as a method parameter in C#
In C#, you can pass tuples as method parameters to group related values together. This approach is useful when you need to pass multiple values to a method without creating a separate class or using multiple parameters. Syntax Following is the syntax for creating a tuple and passing it as a method parameter − // Creating a tuple var tuple = Tuple.Create(value1, value2, value3); // Method with tuple parameter static void MethodName(Tuple tuple) { // Access tuple items using Item1, Item2, Item3 } Using Classic Tuple as Method Parameter ...
Read MoreReturn a C# tuple from a method
A tuple in C# is a data structure that can hold multiple values of different types. You can return a tuple from a method, which is useful when you need to return multiple values from a single method call. There are two main ways to return tuples from methods in C# − using the Tuple class and using the newer value tuples with more concise syntax. Syntax Using the Tuple class − static Tuple MethodName() { return Tuple.Create(value1, value2, value3); } Using value tuples (C# 7.0 and later) − ...
Read MoreConvert Decimal to Int64 (long) in C#
The Convert.ToInt64() method in C# converts a decimal value to a 64-bit signed integer (long). This conversion rounds the decimal to the nearest integer using banker's rounding (round to even) and truncates any fractional part. Syntax Following is the syntax for converting decimal to Int64 − long result = Convert.ToInt64(decimalValue); Parameters decimalValue − The decimal number to be converted to Int64. Return Value Returns a 64-bit signed integer equivalent of the specified decimal value, rounded to the nearest integer. Using Convert.ToInt64() for Basic Conversion The following example ...
Read MoreC# Program to display the first element from an array
In C#, there are multiple ways to access the first element from an array. The most common approaches include using array indexing, the First() LINQ method, and the Take() method. Syntax Following is the syntax for accessing the first element using array indexing − dataType firstElement = arrayName[0]; Following is the syntax using the LINQ First() method − dataType firstElement = arrayName.First(); Using Array Indexing The simplest and most efficient way to get the first element is using zero-based indexing − using System; class Demo { ...
Read MoreReturn the total elements in a sequence as a 64-bit signed integer in C#
The LongCount() method in C# returns the total number of elements in a sequence as a 64-bit signed integer (long). This method is particularly useful when dealing with large datasets where the element count might exceed the range of a regular 32-bit integer. The LongCount() method is available in both LINQ to Objects and LINQ to Entities, and can be used with any IEnumerable or IQueryable collection. Syntax Following is the syntax for using LongCount() method − public static long LongCount(this IEnumerable source) public static long LongCount(this IEnumerable source, Func predicate) Parameters ...
Read MoreObject Initializer in C#
An object initializer in C# allows you to initialize an object's properties or fields at the time of object creation without explicitly calling a constructor with parameters. This feature provides a more readable and concise way to create and initialize objects. Object initializers use curly braces {} to assign values to accessible properties or fields immediately after creating the object instance. Syntax Following is the basic syntax for object initializers − ClassName objectName = new ClassName() { PropertyName1 = value1, PropertyName2 = value2, ...
Read MoreMonth ("M", "m") Format Specifier in C#
The Month ("M", "m") format specifier in C# represents a custom date and time format string that displays the month and day portions of a date. This format specifier is defined by the current DateTimeFormatInfo.MonthDayPattern property and typically follows the pattern MMMM dd. Syntax Following is the syntax for using the Month format specifier − dateTime.ToString("M") dateTime.ToString("m") The custom format string pattern is − MMMM dd Using Month Format Specifier Basic Example using System; using System.Globalization; class Demo { static void Main() ...
Read MoreShort Time ("t") Format Specifier in C#
The Short Time format specifier ("t") in C# is a standard date and time format specifier that displays only the time portion of a DateTime in a short format. It excludes the date and seconds, showing only hours and minutes along with the AM/PM designator for 12-hour formats. The "t" format specifier is defined by the DateTimeFormatInfo.ShortTimePattern property of the current culture. Different cultures may display the time differently based on their regional settings. Syntax Following is the syntax for using the short time format specifier − DateTime.ToString("t") DateTime.ToString("t", CultureInfo) The underlying custom ...
Read More