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 108 of 196
How to compare two tuples in C#?
Tuple comparison was introduced in C# 7.3, allowing you to easily compare two tuples using equality operators. Tuples are compared element-wise, meaning each corresponding element in both tuples must be equal for the tuples to be considered equal. Syntax Following is the syntax for comparing tuples using the equality operator − bool result = tuple1 == tuple2; You can also use the inequality operator − bool result = tuple1 != tuple2; How Tuple Comparison Works Tuple comparison in C# follows these rules: Tuples must have the same ...
Read MoreWhy does the indexing start with zero in C# arrays?
In C#, array indexing starts at zero because arrays are fundamentally based on memory addressing and offset calculations. This design choice has deep roots in computer science and provides both efficiency and logical consistency. When an array is created, it occupies a contiguous block of memory. The array name acts as a pointer to the first element's memory address, and accessing elements involves calculating offsets from this base address. Memory Layout and Addressing Consider an integer array with 5 elements stored in memory. Since each int occupies 4 bytes, the memory layout would look like this − ...
Read MoreHow are values assigned to arrays in C#?
Declaring an array does not initialize the array in memory. When the array variable is initialized, you can assign values to the array using several different approaches in C#. Array is a reference type, so you need to use the new keyword to create an instance of the array. There are multiple ways to assign values to arrays, each with its own syntax and use cases. Syntax Following is the syntax for declaring and initializing arrays − // Declaration with size datatype[] arrayName = new datatype[size]; // Assignment using index arrayName[index] = value; ...
Read MoreHow to use Remove, RemoveAt, RemoveRange methods in C# list collections?
C# provides several methods to remove elements from List collections. The Remove() method removes the first occurrence of a specific value, RemoveAt() removes an element at a specific index, and RemoveRange() removes multiple consecutive elements. Syntax Following is the syntax for the three removal methods − // Remove by value (first occurrence) bool Remove(T item) // Remove by index void RemoveAt(int index) // Remove range of elements void RemoveRange(int index, int count) Parameters Remove(T item): The item to remove from the list. Returns true if item is found and removed. ...
Read MoreWhat is the difference between a list and an array in C#?
An array stores a fixed-size sequential collection of elements of the same type, whereas a list is a generic collection that can dynamically resize during runtime. Understanding the differences between arrays and lists is crucial for choosing the right data structure for your C# applications. Syntax Following is the syntax for declaring and initializing an array − dataType[] arrayName = new dataType[size]; dataType[] arrayName = {value1, value2, value3}; Following is the syntax for declaring and initializing a list − List listName = new List(); List listName = new List {value1, value2, value3}; ...
Read Morefinal, finally and finalize in C#
In C#, the terms final, finally, and finalize serve different purposes. While final doesn't exist in C#, finally and finalize are crucial concepts for exception handling and resource cleanup respectively. final (sealed in C#) C# does not have a final keyword like Java. Instead, C# uses the sealed keyword to achieve similar functionality − public sealed class SealedClass { // cannot be inherited } public override sealed void SealedMethod() { // cannot be overridden further } The sealed keyword prevents overriding of methods or inheritance of classes. ...
Read MoreStreams and Byte Streams in C#
A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream. Streams in C# provide a unified interface for reading and writing data, whether it's from files, memory, or network connections. The type of streams includes − Byte Streams − They handle raw binary data and include Stream, FileStream, MemoryStream, and BufferedStream. Character Streams − They handle text data and include TextReader, TextWriter, StreamReader, StreamWriter, and other text-based streams. Byte ...
Read MoreString format for DateTime in C#
The String.Format method in C# provides powerful formatting capabilities for DateTime objects. You can use various format specifiers to display dates and times in different formats, from simple year displays to complex custom patterns. Syntax Following is the syntax for formatting DateTime using String.Format − String.Format("{0:format_specifier}", dateTimeObject) Common format specifiers include − y − Year (1 digit minimum) yy − Year (2 digits) yyyy − Year (4 digits) M − Month (1-12) MM − Month (01-12) MMM − Month abbreviation (Jan, Feb, etc.) MMMM − Full month name (January, February, etc.) d ...
Read Morec# Put spaces between words starting with capital letters
To place spaces between words starting with capital letters in C#, you can use LINQ to examine each character and insert spaces before uppercase letters. This technique is commonly used to format PascalCase strings into readable text. Syntax The basic syntax using LINQ to add spaces before capital letters − string.Concat(str.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' '); Using LINQ with Select and IsUpper The following example demonstrates how to add spaces before each uppercase letter in a PascalCase string − using System; using System.Linq; class Demo ...
Read MoreInitializing HashSet in C#
A HashSet in C# is a collection that stores unique elements and automatically eliminates duplicates. It provides fast lookups and is ideal when you need to ensure all elements are distinct. Syntax Following is the syntax for initializing a HashSet − var hashSet = new HashSet(); var hashSet = new HashSet(collection); var hashSet = new HashSet { element1, element2, element3 }; Using Array to Initialize HashSet You can initialize a HashSet with an existing array. The HashSet will automatically remove any duplicate values from the array − using System; using System.Collections.Generic; ...
Read More