George John

George John

789 Articles Published

Articles by George John

Page 12 of 79

Intersect two lists in C#

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

In C#, you can find common elements between two lists using the Intersect() method from LINQ. This method returns an IEnumerable containing elements that appear in both lists, automatically removing duplicates. Syntax Following is the syntax for using the Intersect() method − IEnumerable result = list1.Intersect(list2); The method can also accept a custom equality comparer − IEnumerable result = list1.Intersect(list2, comparer); Using Intersect() with Integer Lists The most common use case is finding intersection between two integer lists − using System; using System.Collections.Generic; using System.Linq; class ...

Read More

Enum with Customized Value in C#

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

An enum (enumeration) in C# is used to store a set of named constants such as days of the week, months, seasons, or any fixed collection of related values. By default, enum constants start from 0 and increment by 1, but you can customize these values to match your specific requirements. Customizing enum values is useful when you need specific numeric representations, want to maintain compatibility with external systems, or need non-sequential numbering. Syntax Following is the syntax for declaring an enum with customized values − public enum EnumName { Value1 ...

Read More

C# Aggregate() method

George John
George John
Updated on 17-Mar-2026 639 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 523 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 903 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 957 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 877 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 902 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 567 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
Showing 111–120 of 789 articles
« Prev 1 10 11 12 13 14 79 Next »
Advertisements