Csharp Articles

Page 121 of 196

How to find the product of 2 numbers using recursion in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 300 Views

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 More

How to find Volume and Surface Area of a Sphere using C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 439 Views

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 More

How to find the size of a list in C#?

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 4K+ Views

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 More

C# program to find the most frequent element

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 1K+ Views

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 More

Declare a const array in C#

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 13K+ Views

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 More

How to find the size of a variable without using sizeof in C#?

Syed Javed
Syed Javed
Updated on 17-Mar-2026 2K+ Views

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 More

Find frequency of each word in a string in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 874 Views

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 More

How to find the Sum of two Binary Numbers using C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 2K+ Views

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 More

Find free disk space using C#

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 1K+ Views

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 More

How to check if a string is a valid keyword in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 2K+ Views

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
Showing 1201–1210 of 1,951 articles
« Prev 1 119 120 121 122 123 196 Next »
Advertisements