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 10 of 151
What are the differences between a multi-dimensional array and jagged array?
Multi-dimensional arrays and jagged arrays are two different ways to work with nested data structures in C#. Understanding their differences is crucial for choosing the right approach for your specific use case. Multi-dimensional Arrays A multi-dimensional array is also called a rectangular array because all rows have the same number of columns, forming a rectangular structure. All sub-arrays must have identical lengths. Syntax Following is the syntax for declaring multi-dimensional arrays − // 2D array int[, ] array2D = new int[rows, columns]; // 3D array int[, , ] array3D = new ...
Read MoreHow to find the product of two binary numbers using C#?
To find the product of two binary numbers in C#, we can implement binary multiplication using the traditional algorithm. This involves multiplying each digit of one binary number with the other and adding the results with proper shifting. How Binary Multiplication Works Binary multiplication follows the same principle as decimal multiplication, but uses only 0 and 1. When we multiply by 1, we get the same number; when we multiply by 0, we get zero. The partial products are then added together with appropriate left shifts. Binary Multiplication Process 11100 (28 ...
Read MoreIterating C# StringBuilder in a foreach loop
The StringBuilder class in C# is used for efficient string manipulation when dealing with multiple string operations. While StringBuilder itself is not directly iterable in a foreach loop, you can use foreach loops to iterate over collections and append elements to a StringBuilder. Syntax Following is the syntax for using foreach with StringBuilder − StringBuilder sb = new StringBuilder(); foreach (type item in collection) { sb.Append(item); } Using foreach to Build StringBuilder from Array You can iterate through a string array and append each element to a StringBuilder − ...
Read MoreHow to initialize an empty DateTime in C#
In C#, there are several ways to initialize an empty or default DateTime value. The most common approach is using DateTime.MinValue, which represents the minimum possible date and time value in the .NET framework. Syntax Following are the different ways to initialize an empty DateTime − DateTime dt1 = DateTime.MinValue; DateTime dt2 = new DateTime(); DateTime dt3 = default(DateTime); Using DateTime.MinValue The DateTime.MinValue property returns the minimum possible DateTime value, which is January 1, 0001 at 12:00:00 AM − using System; public class Demo { public static ...
Read MoreAccess a character in C# StringBuilder
A StringBuilder in C# allows you to access individual characters using the indexer syntax, similar to accessing elements in an array. The indexer provides both read and write access to characters at specific positions within the StringBuilder. Syntax Following is the syntax for accessing a character in StringBuilder − char character = stringBuilder[index]; Following is the syntax for modifying a character in StringBuilder − stringBuilder[index] = 'newCharacter'; Parameters index − The zero-based position of the character to access or modify. Return Value Returns the character ...
Read MoreC# program to remove characters starting at a particular index in StringBuilder
The StringBuilder class in C# provides the Remove() method to delete a sequence of characters starting from a specific index position. This method is more efficient than string manipulation when performing multiple character operations. Syntax Following is the syntax for the Remove() method − StringBuilder.Remove(int startIndex, int length) Parameters startIndex − The zero-based position where removal begins. length − The number of characters to remove. Return Value The method returns a reference to the same StringBuilder instance after the removal operation, allowing for method chaining. ...
Read MoreC# TimeSpan Min value
The TimeSpan structure in C# represents a time interval or duration. The TimeSpan.MinValue property returns the minimum possible value that a TimeSpan can represent, which is approximately -10.7 million days. This minimum value is useful when you need to initialize a TimeSpan variable to the smallest possible value or when performing comparisons to find the minimum time span in a collection. Syntax Following is the syntax to access the minimum TimeSpan value − TimeSpan.MinValue Return Value The TimeSpan.MinValue property returns a TimeSpan object representing the minimum possible time interval, which equals approximately ...
Read MoreHow to find the index of an item in a C# list in a single step?
To find the index of an item in a C# list in a single step, you can use several built-in methods. The most common approaches are IndexOf() for exact matches and FindIndex() for condition-based searches. Syntax For exact item matching − int index = list.IndexOf(item); For condition-based searching − int index = list.FindIndex(predicate); Using IndexOf() for Exact Matches The IndexOf() method returns the zero-based index of the first occurrence of the specified item − using System; using System.Collections.Generic; public class Program { public ...
Read MoreDeclare char arrays in C#
A char array in C# is used to store a sequence of characters. You can declare and initialize char arrays in several ways, depending on whether you know the values at compile time or need to set them dynamically. Syntax Following are the different ways to declare a char array − // Declaration with size char[] arr = new char[size]; // Declaration with initialization char[] arr = {'a', 'b', 'c'}; // Alternative initialization syntax char[] arr = new char[] {'a', 'b', 'c'}; Declaring and Setting Elements Individually You can declare a ...
Read MoreStreamWriter in C#
The StreamWriter class in C# is used to write characters to a stream in a particular encoding. It provides an easy way to create and write text to files, making it essential for file I/O operations. StreamWriter automatically handles file creation and provides various methods to write text data efficiently. Syntax Following is the syntax for creating a StreamWriter object − StreamWriter writer = new StreamWriter("filename.txt"); Using the using statement ensures automatic disposal − using (StreamWriter writer = new StreamWriter("filename.txt")) { writer.WriteLine("text"); } Basic File Writing ...
Read More