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 Ankith Reddy
Page 5 of 73
How to check if array contains a duplicate number using C#?
Checking if an array contains duplicate numbers is a common programming task in C#. There are multiple approaches to solve this problem, ranging from simple loops to LINQ methods and hash-based techniques. Using Dictionary to Count Occurrences The dictionary approach counts the frequency of each element in the array. Any element with a count greater than 1 is a duplicate − using System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { ...
Read MoreWhat are circular references in C#?
A circular reference in C# occurs when two or more objects reference each other directly or indirectly, creating a dependency loop. This can cause memory leaks and prevent proper garbage collection if not handled correctly. The .NET garbage collector is designed to detect and handle circular references automatically. It uses a mark-and-sweep algorithm that starts from root objects (like static variables and local variables) and marks all reachable objects, then collects unmarked objects even if they have circular references. Understanding Circular References Circular references typically occur in parent-child relationships, linked lists, or complex object graphs where objects ...
Read MoreC# program to count number of bytes in an array
In C#, you can count the number of bytes in an array using the Buffer.ByteLength() method. This method returns the total number of bytes occupied by all elements in the array, which is particularly useful when working with different data types that have varying byte sizes. Syntax Following is the syntax for using Buffer.ByteLength() − int byteCount = Buffer.ByteLength(array); Parameters array − The array whose byte length you want to determine. It must be an array of primitive types. Return Value The method returns an int representing ...
Read MoreInitializing HashSet in C#
A HashSet in C# is a collection that stores unique elements and automatically eliminates duplicates. It provides fast lookups and is ideal when you need to ensure all elements are distinct. Syntax Following is the syntax for initializing a HashSet − var hashSet = new HashSet(); var hashSet = new HashSet(collection); var hashSet = new HashSet { element1, element2, element3 }; Using Array to Initialize HashSet You can initialize a HashSet with an existing array. The HashSet will automatically remove any duplicate values from the array − using System; using System.Collections.Generic; ...
Read MoreConvert.ToInt64 Method in C#
The Convert.ToInt64 method in C# converts a specified value to a 64-bit signed integer (long). This method can convert various data types including strings, floating-point numbers, and other numeric types to a long value. Syntax Following are the common syntax overloads for Convert.ToInt64 − public static long ToInt64(object value); public static long ToInt64(string value); public static long ToInt64(double value); public static long ToInt64(float value); public static long ToInt64(decimal value); Parameters value − The value to convert to a 64-bit signed integer Return Value Returns a 64-bit signed integer (long) ...
Read MoreWhat are floating point literals in C#?
A floating-point literal in C# represents a decimal number that can contain a fractional part. It consists of an integer part, a decimal point, a fractional part, and optionally an exponent part. You can represent floating point literals in either decimal form or exponential form. Syntax Following are the different forms of floating-point literal syntax − // Decimal form 123.45 0.5678 // Exponential form 1.23E+2 // 1.23 × 10² 4.56e-3 // 4.56 × 10⁻³ // With type suffixes 3.14f // float 2.718 ...
Read MoreWhat are generic collections in C#?
Generic collections in C# are type-safe collections that allow you to specify the type of elements at compile time. They provide better performance, type safety, and IntelliSense support compared to non-generic collections like ArrayList and Hashtable. The most commonly used generic collections include List, Dictionary, SortedList, Queue, and Stack. These are part of the System.Collections.Generic namespace. Syntax Following is the syntax for declaring generic collections − List listName = new List(); Dictionary dictName = new Dictionary(); SortedList sortedListName = new SortedList(); Where T, TKey, and TValue are type parameters that specify the actual ...
Read MoreHow to check if String is Palindrome using C#?
A palindrome is a string that reads the same forwards and backwards, such as "Level", "madam", or "racecar". In C#, there are several ways to check if a string is a palindrome. Using Array.Reverse() Method The simplest approach is to reverse the string and compare it with the original. First, convert the string to a character array − char[] ch = str.ToCharArray(); Then reverse the array using Array.Reverse() − Array.Reverse(ch); Finally, compare the reversed string with the original using case-insensitive comparison − bool res = str.Equals(rev, StringComparison.OrdinalIgnoreCase); ...
Read MoreHow to multiply a given number by 2 using Bitwise Operators in C#?
A number can be multiplied by 2 using bitwise operators. This is done by using the left shift operator (
Read MoreC# Enum Parse Method
The Enum.Parse method in C# converts the string representation of an enum constant name or its numeric value into an equivalent enumerated object. This method is essential when you need to convert string data back into strongly-typed enum values. Syntax Following are the main syntax forms for Enum.Parse − // Basic syntax public static object Parse(Type enumType, string value) // With ignore case option public static object Parse(Type enumType, string value, bool ignoreCase) Parameters enumType − The type of the enumeration to parse to value − A string containing the name ...
Read More