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 Samual Sam
Page 17 of 151
How do you find the number of dimensions of an array in C#?
To find the number of dimensions of an array in C#, use the Rank property. This property returns an integer representing the total number of dimensions in the array. Syntax Following is the syntax for getting the number of dimensions − int dimensions = arr.Rank; For getting the length of specific dimensions, use the GetLength() method − int lengthOfDimension = arr.GetLength(dimensionIndex); Using Rank Property The Rank property works with single-dimensional, multi-dimensional, and jagged arrays − using System; class Program { static void Main() ...
Read MoreBinary to decimal using C#
Converting binary numbers to decimal is a fundamental operation in programming. In C#, you can convert binary to decimal using manual calculation methods or built-in functions. The binary number system uses base 2, where each digit position represents a power of 2. How Binary to Decimal Conversion Works Binary to decimal conversion follows a simple mathematical principle. Each binary digit (bit) is multiplied by the corresponding power of 2, starting from 2⁰ for the rightmost digit − Binary 1010 → Decimal 10 1 ...
Read MoreWhat is the Count property of ArrayList class in C#?
The Count property in the ArrayList class returns the number of elements currently stored in the ArrayList. This property is read-only and provides an efficient way to determine the size of the collection. Syntax Following is the syntax for accessing the Count property − int count = arrayListName.Count; Return Value The Count property returns an int value representing the total number of elements in the ArrayList. Using Count Property with ArrayList First, create an ArrayList and add elements to it − ArrayList arrList = new ArrayList(); arrList.Add(98); arrList.Add(55); arrList.Add(65); ...
Read MoreOverriding Vs Shadowing in C#
In C#, overriding and shadowing are two different mechanisms for changing method behavior in derived classes. Overriding provides true polymorphism using virtual methods, while shadowing (method hiding) creates a new method that hides the base class method without polymorphic behavior. Overriding Overriding allows a subclass to provide a specific implementation of a method that is already defined in its base class. It requires the base class method to be marked as virtual or abstract, and the derived class method to use the override keyword. Method Overriding Base Class ...
Read MoreHow to use the GetLowerBound method of array class in C#
The GetLowerBound() method of the Array class in C# returns the lower bound of the specified dimension in an array. For most arrays in C#, the lower bound is typically 0, but this method becomes useful when working with arrays that have custom bounds or multi-dimensional arrays. Syntax Following is the syntax for the GetLowerBound() method − public int GetLowerBound(int dimension); Parameters dimension − A zero-based dimension of the array whose lower bound needs to be found. Return Value Returns an integer representing the lower bound of the specified ...
Read MoreC# 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 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 MoreSystem.ArrayCopyTo() vs System.ArrayClone() in C#
The CopyTo() and Clone() methods in C# are used to copy array elements, but they work differently. The CopyTo() method copies elements from one array to an existing array at a specified index, while Clone() creates a new array that is a shallow copy of the original array. Understanding the difference between these methods is crucial for proper array manipulation and memory management in C# applications. Syntax Following is the syntax for the CopyTo() method − sourceArray.CopyTo(destinationArray, startIndex); Following is the syntax for the Clone() method − object clonedArray = sourceArray.Clone(); ...
Read MoreC# and Multiple Inheritance
C# does not support multiple inheritance of classes, meaning a class cannot inherit from more than one base class directly. However, C# provides interfaces to achieve similar functionality to multiple inheritance by allowing a class to implement multiple interfaces. Multiple inheritance allows a class to inherit features from multiple parent classes. While this can be powerful, it can also lead to complexity and ambiguity, which is why C# chose to support only single inheritance of classes. Why Multiple Inheritance is Not Supported C# avoids multiple class inheritance to prevent the diamond problem, where ambiguity arises when two ...
Read MoreHow is a new object created in C#?
In C#, creating objects is fundamental to object-oriented programming. An object is an instance of a class that represents a real-world entity. Objects allow you to access class members including fields, properties, and methods using the dot operator. Syntax Following is the basic syntax for creating a new object in C# − ClassName objectName = new ClassName(); You can also initialize an object with constructor parameters − ClassName objectName = new ClassName(parameter1, parameter2); Object Creation Process When you create an object using the new keyword, the following steps occur: ...
Read More