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 121 of 196
How to find the product of 2 numbers using recursion in C#?
Finding the product of two numbers using recursion in C# is an interesting approach that demonstrates how multiplication can be implemented using only addition and recursive function calls. Instead of using the standard multiplication operator, we build the product by repeatedly adding one number to itself. How Recursive Multiplication Works The concept is based on the mathematical principle that multiplication is repeated addition. For example, 5 × 3 = 5 + 5 + 5. In recursion, we add the first number to itself and reduce the second number by 1 until it reaches zero. ...
Read MoreHow to find Volume and Surface Area of a Sphere using C#?
To find the volume and surface area of a sphere using C#, we need to apply mathematical formulas that use the sphere's radius. A sphere is a perfectly round three-dimensional object where every point on its surface is equidistant from its center. Sphere Formulas r Sphere Formulas Surface Area = 4πr² Volume = (4/3)πr³ where r = radius of the sphere Formulas The mathematical formulas for a sphere are − ...
Read MoreHow to find the size of a list in C#?
A List in C# is a dynamic collection that can grow or shrink in size. To determine the size of a list, you can use two different properties: Count (for the number of actual elements) and Capacity (for the total allocated space). Syntax To get the number of elements in a list − listName.Count To get the total capacity (allocated space) of a list − listName.Capacity Understanding Count vs Capacity Count vs Capacity Count Number of actual ...
Read MoreC# program to find the most frequent element
Finding the most frequent element in a string is a common programming problem that involves counting the occurrences of each character and determining which appears most often. In C#, this can be efficiently solved using an array to track character frequencies. Syntax Following is the basic approach to count character frequencies − int[] frequency = new int[256]; // ASCII character set for (int i = 0; i < str.Length; i++) { frequency[str[i]]++; } Using Array-Based Frequency Counting The most straightforward approach uses an integer array where each index corresponds ...
Read MoreDeclare a const array in C#
In C#, you cannot directly declare a const array because arrays are reference types and const fields must be compile-time constants. However, you can achieve similar behavior using readonly fields or ReadOnlyCollection to create immutable arrays. Syntax Following is the syntax for declaring a readonly array − public static readonly DataType[] arrayName = { value1, value2, value3 }; Following is the syntax for using ReadOnlyCollection − public static readonly ReadOnlyCollection arrayName = new ReadOnlyCollection(new DataType[] { value1, value2, value3 }); Using readonly Static Arrays ...
Read MoreHow to find the size of a variable without using sizeof in C#?
In C#, the sizeof operator is commonly used to determine the size of a value type in bytes. However, there are alternative approaches to find the size of a variable without using sizeof, such as using the BitConverter class or Marshal.SizeOf method. Syntax Using sizeof operator − int size = sizeof(dataType); Using BitConverter.GetBytes() method − byte[] dataBytes = BitConverter.GetBytes(variable); int size = dataBytes.Length; Using Marshal.SizeOf() method − int size = Marshal.SizeOf(typeof(dataType)); Using BitConverter.GetBytes() Method The BitConverter.GetBytes() method converts a value to its byte array representation. ...
Read MoreFind frequency of each word in a string in C#
Finding the frequency of each word in a string in C# involves splitting the string into individual words and counting their occurrences. This is a common text processing task useful for data analysis, word clouds, and text mining applications. Using Dictionary for Word Frequency The most efficient approach is to use a Dictionary to store words as keys and their frequencies as values − using System; using System.Collections.Generic; class WordFrequency { public static void Main() { string text = "apple banana apple orange banana apple"; ...
Read MoreHow to find the Sum of two Binary Numbers using C#?
Adding two binary numbers in C# involves performing binary arithmetic bit by bit, just like manual addition but working in base-2. The process includes handling carry operations when the sum of bits exceeds 1. Binary addition follows simple rules: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10 (which means 0 with a carry of 1). Syntax Following is the basic approach for binary addition − // Extract rightmost bits bit1 = val1 % 10; bit2 = val2 % 10; // Calculate ...
Read MoreFind free disk space using C#
The DriveInfo class in C# provides information about drives and their storage capacity. You can use this class to find free disk space, total disk space, and calculate the percentage of available space on any drive. Syntax Following is the syntax for creating a DriveInfo instance − DriveInfo driveInfo = new DriveInfo("driveLetter"); Following are the key properties for disk space information − long availableSpace = driveInfo.AvailableFreeSpace; long totalSpace = driveInfo.TotalSize; long usedSpace = driveInfo.TotalSize - driveInfo.AvailableFreeSpace; Using DriveInfo to Get Free Disk Space Example using System; using ...
Read MoreHow to check if a string is a valid keyword in C#?
To check if a string is a valid keyword in C#, you can use the IsValidIdentifier method from the CodeDomProvider class. This method determines whether a given string is a valid identifier or a reserved keyword in C#. The IsValidIdentifier method returns true if the string is a valid identifier (like variable names, method names), and false if it's a reserved keyword (like for, if, class, etc.). Syntax Following is the syntax for using IsValidIdentifier method − CodeDomProvider provider = CodeDomProvider.CreateProvider("C#"); bool isIdentifier = provider.IsValidIdentifier(stringToCheck); Using IsValidIdentifier to Check Keywords Here's how ...
Read More