George John

George John

789 Articles Published

Articles by George John

Page 11 of 79

C# Aggregate() method

George John
George John
Updated on 17-Mar-2026 637 Views

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 More

How to count the number of items in a C# list?

George John
George John
Updated on 17-Mar-2026 1K+ Views

The Count property in C# is used to determine the number of elements in a List. This property returns an integer representing the total count of items currently stored in the list. Syntax Following is the syntax for using the Count property − int count = myList.Count; Parameters The Count property does not take any parameters. It is a read-only property that returns the current number of elements in the list. Return Value The Count property returns an int value representing the number of elements in the list. It returns 0 ...

Read More

C# program to find common values from two or more Lists

George John
George John
Updated on 17-Mar-2026 517 Views

Finding common values from two or more Lists in C# is a frequent requirement in programming. The Intersect() method from LINQ provides an efficient way to identify elements that exist in multiple collections. Syntax Following is the syntax for using Intersect() method − var result = list1.Intersect(list2); For multiple lists, chain the Intersect() method − var result = list1.Intersect(list2).Intersect(list3); Using Intersect() with Two Lists The Intersect() method returns elements that appear in both lists. It automatically removes duplicates and maintains the order from the first list − Example ...

Read More

C# Linq Distinct() Method

George John
George John
Updated on 17-Mar-2026 896 Views

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 More

How to declare a two-dimensional array in C#

George John
George John
Updated on 17-Mar-2026 925 Views

A two-dimensional array in C# is a data structure that stores elements in a grid-like format with rows and columns. It can be thought of as an array of arrays, where each element is accessed using two indices. Syntax Following is the syntax for declaring a two-dimensional array − datatype[, ] arrayName; Following is the syntax for initializing a two-dimensional array − datatype[, ] arrayName = new datatype[rows, columns]; You can also declare and initialize in one statement − datatype[, ] arrayName = new datatype[rows, columns] { ...

Read More

How to print multiple blank lines in C#?

George John
George John
Updated on 17-Mar-2026 870 Views

In C#, there are several ways to print multiple blank lines to the console. You can use loops, string repetition, or direct method calls depending on your needs. Using a While Loop The most straightforward approach is to use a while loop to print blank lines repeatedly − using System; namespace Program { public class Demo { public static void Main(string[] args) { int a = 0; while ...

Read More

Difference between Static Constructor and Instance Constructor in C#

George John
George John
Updated on 17-Mar-2026 893 Views

In C#, constructors are special methods used to initialize objects. There are two main types: static constructors and instance constructors. Understanding their differences is crucial for proper class initialization and memory management. Static Constructor A static constructor is declared using the static modifier and is responsible for initializing static members of a class. It executes only once during the application lifetime, before any static members are accessed or any instances are created. Syntax static ClassName() { // initialize static members } Instance Constructor An instance constructor initializes instance ...

Read More

String slicing in C# to rotate a string

George John
George John
Updated on 17-Mar-2026 558 Views

String slicing in C# allows you to rotate characters within a string by combining substrings. This technique uses the Substring() method to extract portions of a string and rearrange them to achieve the desired rotation effect. String rotation involves moving characters from one position to another within the string. For example, rotating the first two characters of "welcome" to the end results in "lcomewe". Syntax Following is the syntax for the Substring() method − string.Substring(startIndex, length) For string rotation, combine two substrings − string rotated = str.Substring(n) + str.Substring(0, n); ...

Read More

What does the interface ICollection do in C#

George John
George John
Updated on 17-Mar-2026 3K+ Views

The ICollection interface in C# defines the size, enumerators, and synchronization methods for all nongeneric collections. It is the base interface for classes in the System.Collections namespace and serves as the foundation for collection types like ArrayList, Hashtable, and Queue. This interface provides essential functionality that all collection classes must implement, including methods to copy elements and enumerate through the collection, along with properties to get the count and synchronization object. Syntax Following is the syntax for implementing the ICollection interface − public interface ICollection : IEnumerable { int Count { ...

Read More

Formatted output in C#

George John
George John
Updated on 17-Mar-2026 2K+ Views

In C#, formatted output allows you to control how data is displayed when converting values to strings. This is essential for presenting numbers, dates, and other data types in a user-friendly format using format specifiers and the String.Format() method. Syntax Following is the syntax for basic string formatting − String.Format("{index:format}", value) Where index is the parameter position (starting from 0) and format is the format specifier. Using Format Specifiers for Numbers Decimal Places and Thousands Separator using System; class Demo { public static void Main(String[] ...

Read More
Showing 101–110 of 789 articles
« Prev 1 9 10 11 12 13 79 Next »
Advertisements