Programming Articles

Page 872 of 2547

C# program to find Union of two or more Dictionaries

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 800 Views

The union of two or more dictionaries combines all unique keys from the dictionaries. In C#, you can find the union using HashSet and the UnionWith() method to merge dictionary keys, or use LINQ methods to combine both keys and values. Syntax Following is the syntax for finding union of dictionary keys using HashSet − HashSet unionKeys = new HashSet(dict1.Keys); unionKeys.UnionWith(dict2.Keys); Following is the syntax for merging dictionary values using LINQ − var unionDict = dict1.Union(dict2).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Using HashSet for Key Union The most straightforward ...

Read More

C# Aggregate() method

George John
George John
Updated on 17-Mar-2026 645 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 create 4-Tuple or quadruple in C#?

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 291 Views

The Tuple class represents a 4-tuple, which is called a quadruple. A tuple is a data structure that holds a sequence of elements of different types in a single object. 4-tuples are commonly used for − Easier access to a data set. Easier manipulation of a data set. To represent a single set of data. To return multiple values from a method. To pass multiple values to a method. 4-Tuple Structure Item1 Item2 Item3 ...

Read More

How to use #undef directive in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 343 Views

The #undef directive in C# allows you to undefine a previously defined symbol, making it unavailable for use in conditional compilation directives like #if. This is useful for controlling which code sections are included during compilation. Syntax Following is the syntax for using the #undef directive − #undef SYMBOL Where SYMBOL is the identifier you want to undefine. For example − #undef DEBUG #undef TESTING Key Rules The #undef directive must appear at the top of the file, before any code or using statements. You ...

Read More

What is the difference between function overriding and method hiding in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 791 Views

In C#, function overriding and method hiding are two different mechanisms for changing method behavior in derived classes. While both allow a child class to provide its own implementation of a parent class method, they work in fundamentally different ways. Function Overriding Function overriding allows a subclass to provide a specific implementation of a method that is already defined in its parent class. The parent method must be marked as virtual, abstract, or override, and the child method uses the override keyword. Syntax // Parent class public virtual ReturnType MethodName() { } // Child ...

Read More

How to find date difference in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 167 Views

To find the difference between two dates in C#, you can use the DateTime class along with the TimeSpan structure. The DateTime subtraction operation returns a TimeSpan object that represents the time difference between two dates. Syntax Following is the syntax for calculating date difference − DateTime date1 = new DateTime(year, month, day); DateTime date2 = new DateTime(year, month, day); TimeSpan difference = date2 - date1; The TimeSpan object provides various properties to access different units of the difference − difference.Days // Total ...

Read More

C# program to find Union of two or more Lists

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 1K+ Views

The Union operation in C# combines multiple lists while removing duplicate elements. The Union() method from LINQ returns distinct elements from both collections, preserving the order of first occurrence. Syntax Following is the syntax for using the Union() method − var result = list1.Union(list2); For multiple lists, chain the Union operations − var result = list1.Union(list2).Union(list3); Using Union() Method with Two Lists The Union()

Read More

Represent Int64 as a Binary string in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 2K+ Views

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 More

Math.Max() Method in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 7K+ Views

The Math.Max() method in C# returns the larger of two specified numbers. This static method is overloaded to work with various numeric types including int, double, decimal, float, and more. Syntax The Math.Max() method has multiple overloads for different numeric types − public static int Max(int val1, int val2); public static double Max(double val1, double val2); public static decimal Max(decimal val1, decimal val2); public static float Max(float val1, float val2); public static long Max(long val1, long val2); public static byte Max(byte val1, byte val2); public static short Max(short val1, short val2); public static uint Max(uint val1, ...

Read More

How to capture null reference exception in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 435 Views

A NullReferenceException occurs when you attempt to access a member (method, property, or field) of a null object reference. This is one of the most common runtime exceptions in C#. What Causes NullReferenceException? The exception is thrown when you try to access members of an object that has not been initialized or has been set to null − string str = null; int length = str.Length; // Throws NullReferenceException Using Null Checks The most straightforward way to prevent NullReferenceException is to check for null before accessing object members − using System; ...

Read More
Showing 8711–8720 of 25,466 articles
« Prev 1 870 871 872 873 874 2547 Next »
Advertisements