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 96 of 196
What does the interface IList do in C#?
The IList interface in C# represents a non-generic collection of objects that can be individually accessed by index. It provides indexed access to elements, allowing you to retrieve, modify, add, and remove items at specific positions within the collection. The IList interface combines the functionality of ICollection and IEnumerable, making it suitable for collections that need both indexed access and dynamic resizing capabilities. Syntax Following is the syntax for declaring an IList variable − IList list = new ArrayList(); IList list = new List(); Following is the syntax for accessing elements by index ...
Read MoreWhat does the interface ICloneable do in C#?
The ICloneable interface in C# provides a mechanism to create a copy of an existing object, known as cloning. This interface is part of the System namespace and defines a standard way to duplicate objects. Syntax The ICloneable interface contains only one method − public interface ICloneable { object Clone(); } To implement ICloneable, a class must provide the Clone() method − public class MyClass : ICloneable { public object Clone() { // return a copy ...
Read MoreWhat does the interface IStructuralComparable do in C#?
The IStructuralComparable interface in C# enables structural comparison of collection objects, meaning it compares elements in sequence rather than using reference equality. This interface was introduced in .NET 4.0 to provide consistent comparison behavior for collections and tuples. Syntax Following is the syntax for the IStructuralComparable interface − public interface IStructuralComparable { int CompareTo(object other, IComparer comparer); } Parameters The CompareTo method accepts the following parameters − other − The object to compare with the current instance. comparer − An IComparer object that defines ...
Read MoreHow to display numbers in the form of Triangle using C#?
To display numbers in the form of a triangle in C#, we use a two-dimensional array to store the triangle values and nested loops to generate the pattern. This creates what's known as Pascal's Triangle, where each number is the sum of the two numbers above it. Syntax Following is the syntax for declaring a two-dimensional array for the triangle − int[, ] array = new int[rows, columns]; Following is the pattern for Pascal's Triangle logic − if (j == 0 || i == j) { a[i, j] ...
Read MoreWhat are the different types of conditional statements supported by C#?
Conditional statements in C# allow programs to execute different code blocks based on specified conditions. These statements evaluate boolean expressions and direct program flow accordingly, enabling dynamic decision-making in applications. Types of Conditional Statements Statement Description if statement Executes a block of code when a boolean expression is true. if...else statement Executes one block if the condition is true, another if false. nested if statements Uses one if or else if statement inside another for complex conditions. switch statement Tests a variable against multiple values ...
Read MoreHow to use C# BinaryReader class?
The BinaryReader class in C# is used to read binary data from a stream in a specific encoding. It provides methods to read various data types like integers, doubles, strings, and byte arrays from binary files or streams. The BinaryReader class is located in the System.IO namespace and is commonly used for reading binary files, network streams, or any binary data format. Syntax Following is the syntax for creating a BinaryReader instance − BinaryReader reader = new BinaryReader(stream); Following is the syntax for reading different data types − int value = ...
Read MoreWhat is the Item property of BitArray class in C#?
The Item property of the BitArray class in C# gets or sets the value of the bit at a specific position in the BitArray. This property acts as an indexer, allowing you to access individual bits using bracket notation like bitArray[index]. The Item property provides a convenient way to read and modify individual bits without having to use explicit method calls. It returns a bool value representing the bit state at the specified index. Syntax Following is the syntax for accessing the Item property − public bool this[int index] { get; set; } ...
Read MoreHow to use C# BinaryWriter class?
The BinaryWriter class in C# is used to write binary data to a stream in a binary format. It is part of the System.IO namespace and provides methods to write primitive data types like integers, strings, doubles, and booleans directly to a stream as binary data. Unlike text-based writing, BinaryWriter stores data in its native binary representation, making it more efficient for storage and faster to read back using BinaryReader. Syntax Following is the syntax for creating a BinaryWriter instance − BinaryWriter writer = new BinaryWriter(stream); Following is the syntax for writing data ...
Read MoreHow to use C# FileStream class?
The FileStream class in C# provides a stream for file operations such as reading from and writing to files. It is part of the System.IO namespace and allows byte-level access to files on the file system. FileStream is useful when you need direct control over file operations, especially for binary data or when working with large files efficiently. Syntax Following is the basic syntax for creating a FileStream object − FileStream fileStream = new FileStream(path, FileMode, FileAccess); The FileMode parameter specifies how the file should be opened − FileMode.OpenOrCreate // ...
Read MoreHow to calculate the Size of Folder using C#?
To calculate the size of a folder in C#, you need to traverse through all files in the folder and its subdirectories, then sum up their sizes. The DirectoryInfo class provides methods to enumerate files and directories, making this task straightforward. Syntax Following is the syntax for getting folder information and enumerating files − DirectoryInfo info = new DirectoryInfo(@"C:\FolderPath"); long size = info.EnumerateFiles("*", SearchOption.AllDirectories).Sum(file => file.Length); Using DirectoryInfo for Simple Folder Size The most basic approach uses DirectoryInfo.EnumerateFiles() with SearchOption.AllDirectories to include all subdirectories − using System; using System.IO; using System.Linq; ...
Read More