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 129 of 196
How to Convert Hex String to Hex Number in C#?
In C#, converting a hexadecimal string to its numeric value is a common operation. Hexadecimal strings represent numbers in base 16 using digits 0-9 and letters A-F. C# provides several methods to convert hex strings to different numeric types depending on your needs. Syntax Following is the syntax for converting hex strings using different methods − Convert.ToInt32(hexString, 16) Convert.ToSByte(hexString, 16) int.Parse(hexString, NumberStyles.HexNumber) Using Convert.ToInt32() Method The most commonly used method is Convert.ToInt32() which converts a hex string to a 32-bit signed integer − using System; public class Program { ...
Read MoreC# program to check whether a given string is Heterogram or not
A Heterogram is a string that contains no duplicate letters. Each character in the string appears exactly once. For example, words like "mobile", "cry", and "laptop" are heterograms because no letter is repeated. Examples of Heterograms mobile - No repeated letters cry - All unique letters laptop - Each letter appears once Algorithm The algorithm uses an integer array to track which letters have been encountered. For each character in the string − Convert the character to an array index using str[i] - ...
Read MoreHow to check if two Strings are anagrams of each other using C#?
An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once. For example, "silent" and "listen" are anagrams because they contain the same letters in different arrangements. In C#, there are several approaches to check if two strings are anagrams. The most common method is to sort the characters of both strings and compare them for equality. Using Array.Sort() Method The simplest approach is to convert both strings to character arrays, sort them, and then compare the sorted arrays − ...
Read MoreC# program to find Union of two or more Dictionaries
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 MoreC# program to find Union of two or more Lists
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 MoreC# program to concat two or more Lists
In C#, you can concatenate multiple List objects using the Concat() method from LINQ. This method creates a new sequence that contains all elements from the original lists in order without modifying the original lists. Syntax Following is the syntax for concatenating lists using the Concat() method − var result = list1.Concat(list2); For multiple lists, you can chain the Concat() calls − var result = list1.Concat(list2).Concat(list3); Using Concat() to Merge Two Lists Example using System; using System.Collections.Generic; using System.Linq; public class Demo { ...
Read MoreC# program to find common values from two or more Lists
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 MoreUnion Method in C#
The Union method in C# is a LINQ extension method that returns the unique elements from both collections. It combines two sequences and automatically removes duplicate values, ensuring each element appears only once in the result. Syntax Following is the syntax for the Union method − public static IEnumerable Union( this IEnumerable first, IEnumerable second ) Parameters first − The first sequence to merge. second − The second sequence to merge. Return Value Returns an IEnumerable containing ...
Read MoreString slicing in C# to rotate a string
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 MoreString Formatting in C# to add padding
String formatting with padding in C# allows you to align text in columns by adding spaces to the left or right of strings. This is particularly useful for creating formatted tables, reports, or aligned output displays. Padding is controlled by format specifiers that define the width and alignment of each placeholder in the format string. Syntax The basic syntax for padding in format strings is − string.Format("{index, width}", value) Where − index − The parameter index (0, 1, 2, etc.) width − The total width of the field Positive width − ...
Read More