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
Articles by karthikeya Boyini
Page 26 of 143
Difference between IComparable and IComparer Interface in C#
The IComparable and IComparer interfaces in C# are both used for sorting and comparing objects, but they serve different purposes and are implemented in different ways. The IComparable interface allows an object to compare itself with another object of the same type. The IComparer interface provides a way to compare two objects externally, offering more flexibility for custom sorting logic. Syntax Following is the syntax for implementing IComparable − public class ClassName : IComparable { public int CompareTo(object obj) { // comparison logic ...
Read MorePush vs pop in stack class in C#
The Stack class in C# represents a last-in, first-out (LIFO) collection of objects. It is used when you need to store and retrieve elements in reverse order — the last element added is the first one to be removed. The Stack class provides two fundamental operations: Push() to add elements and Pop() to remove elements from the top of the stack. Stack LIFO Operations D (Top) C B A ...
Read MoreHow to convert a number from Decimal to Binary using recursion in C#?
Converting a decimal number to binary using recursion in C# involves repeatedly dividing the number by 2 and collecting the remainders. The recursive approach breaks down the problem into smaller subproblems until the base case is reached. Syntax Following is the basic syntax for the recursive binary conversion method − public void ConvertToBinary(int decimalNumber) { if (decimalNumber > 0) { ConvertToBinary(decimalNumber / 2); Console.Write(decimalNumber % 2); } } How It ...
Read MoreHow to remove an empty string from a list of empty strings in C#?
In C#, you can remove empty strings from a list using several methods. Empty strings can be truly empty ("") or contain only whitespace characters (" "). This article demonstrates different approaches to remove these empty or whitespace-only strings from a list. Syntax Using RemoveAll() method to remove empty strings − list.RemoveAll(string.IsNullOrEmpty); Using RemoveAll() with lambda expression to remove empty and whitespace strings − list.RemoveAll(x => string.IsNullOrWhiteSpace(x)); Using LINQ Where() method to filter out empty strings − var filteredList = list.Where(x => !string.IsNullOrEmpty(x)).ToList(); Using RemoveAll() Method ...
Read MoreHow 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 MoreHow to convert a 2D array into 1D array in C#?
Converting a 2D array to a 1D array in C# involves flattening the multi-dimensional structure into a single-dimensional array. This process is commonly used in scenarios like matrix operations, data serialization, or when interfacing with APIs that expect 1D arrays. Using Nested Loops The most straightforward approach is using nested loops to iterate through the 2D array and copy elements to the 1D array − using System; class Program { static void Main(string[] args) { int[, ] a = new int[2, 2] {{1, ...
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 use #undef directive in C#?
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 MoreHow to print a blank line in C#?
To print a blank line in C#, you can use several methods. The most common approach is using Console.WriteLine() without any parameters, which outputs an empty line to the console. Syntax Following are the different ways to print a blank line − Console.WriteLine(); // Empty line Console.WriteLine(""); // Empty string Console.WriteLine(" "); // Single space Console.Write(""); // Newline character ...
Read MoreHow to create a Dictionary using C#?
A Dictionary in C# is a collection of key-value pairs where each key is unique. The Dictionary class is part of the System.Collections.Generic namespace and provides fast lookups based on keys. Dictionaries are useful when you need to associate values with unique identifiers, such as storing employee IDs with names or product codes with prices. Syntax Following is the syntax for creating a Dictionary − Dictionary dictionaryName = new Dictionary(); Following is the syntax for adding items to a Dictionary − dictionaryName.Add(key, value); dictionaryName[key] = value; // Alternative syntax ...
Read More