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
Programming Articles
Page 819 of 2547
C# program to check if binary representation is palindrome
A binary palindrome is a number whose binary representation reads the same forwards and backwards. To check if a number's binary representation is a palindrome, we need to reverse its binary form and compare it with the original. For example, the number 5 has binary representation 101, which reads the same forwards and backwards, making it a binary palindrome. How It Works The algorithm uses bitwise operations to reverse the binary representation − Left shift (=) moves bits to the right, effectively dividing by 2 AND operation (&) checks if the least ...
Read MoreWhat is the Count property of BitArray class in C#?
The Count property of the BitArray class in C# returns the total number of elements (bits) in the BitArray. This is a read-only property that gives you the size of the BitArray, which is determined when the BitArray is created. Syntax Following is the syntax for accessing the Count property − int count = bitArray.Count; Return Value The Count property returns an int value representing the number of bits in the BitArray. Using Count Property with BitArray Let us first create a BitArray and then use the Count property to get ...
Read MoreWhat is the difference between overriding and shadowing in C#?
In C#, overriding and shadowing (also known as method hiding) are two different mechanisms for modifying inherited behavior. Overriding uses the virtual/override keywords to provide specific implementations of base class methods, while shadowing uses the new keyword to hide base class members entirely. Syntax Following is the syntax for method overriding − // Base class public virtual void MethodName() { } // Derived class public override void MethodName() { } Following is the syntax for method shadowing/hiding − // Base class public void MethodName() { } // Derived class public ...
Read MoreAsQueryable() in C#
The AsQueryable() method in C# is used to convert an IEnumerable collection into an IQueryable interface. This conversion enables LINQ query providers to translate queries into optimized database queries or other queryable data sources. The key difference is that IEnumerable executes queries in-memory using LINQ to Objects, while IQueryable can be translated into expression trees for remote execution, such as database queries. Syntax Following is the syntax for using AsQueryable() method − IQueryable queryable = collection.AsQueryable(); The method returns an IQueryable that represents the original collection as a queryable data source. Using ...
Read MoreHow to sort a list in C#?
Sorting a list in C# can be accomplished using several methods. The List class provides the Sort() method for in-place sorting, while LINQ offers additional sorting capabilities. This article demonstrates various approaches to sort lists in ascending and descending order. Syntax Following is the syntax for the basic Sort() method − list.Sort(); Following is the syntax for sorting with a custom comparer − list.Sort((x, y) => y.CompareTo(x)); // descending order Using List.Sort() Method The Sort() method sorts the elements in the entire list using the default comparer for the ...
Read MoreMath.Round() Method in C#
The Math.Round() method in C# rounds a value to the nearest integer or to the specified number of fractional digits. This method provides several overloads to handle different data types and rounding scenarios. Syntax The following are the primary syntax forms of Math.Round() − Math.Round(Double) Math.Round(Double, Int32) Math.Round(Double, Int32, MidpointRounding) Math.Round(Double, MidpointRounding) Math.Round(Decimal) Math.Round(Decimal, Int32) Math.Round(Decimal, Int32, MidpointRounding) Math.Round(Decimal, MidpointRounding) Parameters value − The decimal or double number to be rounded. digits − The number of fractional digits in the return value. mode − Specification for how to round value if it ...
Read MoreC# program to check if there are K consecutive 1's in a binary number
A C# program to check if there are K consecutive 1's in a binary number involves counting the longest sequence of consecutive 1's and comparing it with the required count K. This is useful for binary pattern analysis and digital signal processing applications. Approach The algorithm iterates through the binary array, maintaining a counter for consecutive 1's. When a 0 is encountered, the counter resets. The maximum consecutive count is tracked using Math.Max() method. Using Boolean Array Representation A binary number can be represented as a boolean array where true represents 1 and false represents 0 ...
Read MoreWhat is the Count property of Hashtable class in C#?
The Count property of the Hashtable class in C# returns the total number of key-value pairs stored in the hashtable. It is a read-only property that provides an efficient way to determine the size of the collection. Syntax Following is the syntax for using the Count property − int count = hashtable.Count; Return Value The Count property returns an int value representing the number of key-value pairs currently stored in the hashtable. Using Count Property with Basic Operations The following example demonstrates how to use the Count property to track the ...
Read MoreVirtual vs Sealed vs New vs Abstract in C#
In C#, virtual, sealed, new, and abstract are important keywords that control method inheritance and overriding behavior. Understanding these modifiers is crucial for implementing proper object-oriented design patterns and controlling how classes interact through inheritance. Virtual The virtual keyword allows a method in a base class to be overridden in derived classes. When a method is declared as virtual, derived classes can provide their own implementation using the override keyword. Syntax public virtual ReturnType MethodName() { // base implementation } Example using System; class Animal { ...
Read MoreSort KeyValuePairs in C#
The KeyValuePair structure in C# represents a key-value pair that can be stored in collections. When working with lists of KeyValuePairs, you often need to sort them based on either the key or value. C# provides several approaches to accomplish this sorting. Syntax Following is the syntax for sorting KeyValuePairs using the Sort() method with a lambda expression − myList.Sort((x, y) => x.Value.CompareTo(y.Value)); // Sort by Value (ascending) myList.Sort((x, y) => y.Value.CompareTo(x.Value)); // Sort by Value (descending) myList.Sort((x, y) => x.Key.CompareTo(y.Key)); // Sort by Key (ascending) Following ...
Read More