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 145 of 196
Convert.ToInt16 Method in C#
The Convert.ToInt16 method in C# converts a specified value to a 16-bit signed integer (short). This method accepts various data types including double, string, boolean, and other numeric types, and returns a short value ranging from -32, 768 to 32, 767. When converting from floating-point numbers, the method performs rounding to the nearest integer. If the value is exactly halfway between two integers, it rounds to the even number. Syntax Following are the common overloads of the Convert.ToInt16 method − public static short ToInt16(double value); public static short ToInt16(string value); public static short ToInt16(bool value); ...
Read MoreConvert.ToUInt16 Method in C#
The Convert.ToUInt16 method in C# converts a specified value to a 16-bit unsigned integer (ushort). This method can convert various data types including strings, integers, doubles, and other numeric types to a ushort value ranging from 0 to 65, 535. Syntax Following are the common overloads of the Convert.ToUInt16 method − public static ushort ToUInt16(string value); public static ushort ToUInt16(int value); public static ushort ToUInt16(double value); public static ushort ToUInt16(bool value); Parameters value − The value to convert to a 16-bit unsigned integer. Can be a string representation of a number, numeric ...
Read MoreEnumerable.Repeat method in C#
The Enumerable.Repeat method is part of the System.Linq namespace and creates a sequence that contains the same element repeated a specified number of times. This method is useful for initializing collections or generating test data with repeated values. Syntax Following is the syntax for the Enumerable.Repeat method − public static IEnumerable Repeat(TResult element, int count) Parameters element − The value to be repeated in the resulting sequence. count − The number of times to repeat the element. Return Value Returns an IEnumerable that contains the ...
Read MoreC# Aggregate() method
The Aggregate() method in C# applies an accumulator function over a sequence of elements. It processes each element in the collection and combines them into a single result using a specified function. This method is part of LINQ and provides a powerful way to perform custom aggregation operations. Syntax The Aggregate() method has three main overloads − // Simple aggregation without seed TSource Aggregate(Func func) // Aggregation with seed value TAccumulate Aggregate(TAccumulate seed, Func func) // Aggregation with seed and result selector TResult Aggregate(TAccumulate seed, Func func, Func resultSelector) Parameters ...
Read MoreRepresent Int64 as a Binary string in C#
To represent Int64 as a binary string in C#, use the Convert.ToString() method with base 2 as the second parameter. Int64 represents a 64-bit signed integer that can hold values from -9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 775, 807. Syntax Following is the syntax for converting Int64 to binary string − Convert.ToString(longValue, 2) Parameters longValue − The Int64 value to convert 2 − The base for binary representation Return Value Returns a string representation of the Int64 value in binary format (base ...
Read MoreC# All method
The All() method in C# is a LINQ extension method that checks whether all elements in a collection satisfy a given condition. It returns a bool value − true if all elements meet the condition, or false if even one element fails the condition. The method is available for any IEnumerable collection and is commonly used with arrays, lists, and other collections for validation and filtering operations. Syntax Following is the syntax for the All() method − bool result = collection.All(predicate); Parameters predicate − A lambda expression or delegate that ...
Read MoreC# Linq Distinct() Method
The Distinct() method in C# LINQ is used to remove duplicate elements from a collection and return only unique elements. This method is available for any collection that implements IEnumerable and preserves the order of first occurrence. Syntax Following is the basic syntax for the Distinct() method − IEnumerable Distinct() IEnumerable Distinct(IEqualityComparer comparer) Parameters comparer (optional) − An IEqualityComparer to compare elements for equality. If not provided, the default equality comparer is used. Return Value Returns an IEnumerable containing distinct elements from the source sequence. Using ...
Read MoreC# Queryable LongCount Method
The LongCount() method in C# is part of the System.Linq namespace and returns the number of elements in a sequence as a long data type. This method is particularly useful when dealing with large collections where the count might exceed the range of an int (2, 147, 483, 647). The LongCount() x % 2 == 0); Console.WriteLine("Even numbers: {0}", evenCount); long greaterThanFive = numbers.AsQueryable().LongCount(x => x > 5); ...
Read MoreWhy is f required while declaring floats in C#?
The f suffix is required when declaring float literals in C# because without it, the compiler treats decimal numbers as double by default. The f suffix explicitly tells the compiler that the literal should be treated as a float type. Why the 'f' Suffix is Needed In C#, numeric literals with decimal points are interpreted as double precision floating-point numbers by default. Since double has higher precision than float, implicit conversion from double to float is not allowed because it could result in data loss. Literal Type Assignment ...
Read MoreJoin, Sleep and Abort methods in C# Threading
The Thread class in C# provides several important methods to control thread execution: Join(), Sleep(), and Abort(). These methods allow you to synchronize threads, pause execution, and terminate threads when needed. Syntax Following is the syntax for the three main thread control methods − // Join method thread.Join(); thread.Join(timeout); // Sleep method Thread.Sleep(milliseconds); // Abort method (obsolete in .NET Core/.NET 5+) thread.Abort(); Join Method The Join() method blocks the calling thread until the target thread terminates. This ensures that the calling thread waits for another thread to complete before continuing execution. ...
Read More