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 126 of 196
How to iterate over a C# list?
A List in C# is a generic collection that stores elements of the same type. There are several ways to iterate over a C# list, each with its own advantages depending on your specific needs. Syntax Following is the basic syntax for declaring and initializing a list − List listName = new List(); Following are the common iteration syntaxes − // foreach loop foreach(var item in list) { // process item } // for loop for(int i = 0; i < list.Count; i++) { // ...
Read MoreHow to empty a C# list?
To empty a C# list, use the Clear() method. This method removes all elements from the list and sets the count to zero. The Clear() method is the most efficient way to empty a list as it removes all elements in a single operation. Syntax Following is the syntax for the Clear() method − listName.Clear(); Parameters The Clear() method does not take any parameters. Return Value The Clear() method does not return any value. It modifies the list in-place by removing all elements. Using Clear() Method The following example ...
Read MoreHow to iterate over a C# tuple?
A tuple in C# is a data structure that holds multiple values of different types. Since tuples have a fixed structure, you cannot iterate over them using traditional loops like arrays or collections. Instead, you access tuple elements individually using their Item properties or through deconstruction. Syntax To create a tuple and access its elements − // Creating a tuple Tuple tuple = new Tuple(100, "Tom"); // Accessing individual elements tuple.Item1 // first element tuple.Item2 // second element For modern C# (7.0+), you can use tuple literals − var ...
Read MoreHow do I determine the size of my array in C#
In C#, determining the size of an array is accomplished using the Length property. This property returns the total number of elements in the array as an int value. Syntax Following is the syntax for getting array length − arrayName.Length Parameters The Length property does not take any parameters and returns an integer representing the number of elements. Return Value Returns an int value indicating the total number of elements in the array. Using Length Property for Single-Dimensional Arrays For single-dimensional arrays, the Length property provides a direct count ...
Read MoreHow to List all Substrings in a given String using C#?
A substring is any contiguous sequence of characters within a string. In C#, you can generate all possible substrings of a given string using the Substring() method with nested loops to iterate through different starting positions and lengths. Syntax The Substring() method extracts a portion of a string − string.Substring(startIndex, length) To generate all substrings, use nested loops − for (int length = 1; length
Read MoreClass and Static Variables in C#
In C#, static variables belong to the class itself rather than to any specific instance of the class. They are shared among all instances of the class and can be accessed using the class name without creating an object. Class variables (instance variables) belong to individual objects and each instance has its own copy of these variables. Static Variables Static variables are declared using the static keyword and are useful for defining constants or shared data across all instances of a class. Syntax public static dataType variableName; Static vs ...
Read MoreFile Objects in C#
The FileStream class in C# is used to create, read, write, and manipulate files. It provides low-level access to file operations and is part of the System.IO namespace. FileStream works with bytes, making it suitable for both text and binary files. Syntax Following is the syntax for creating a FileStream object − FileStream objectName = new FileStream(fileName, FileMode, FileAccess, FileShare); A simplified syntax for common scenarios − FileStream objectName = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); Parameters Parameter Description Common Values fileName Path and ...
Read MoreHow to truncate a file in C#?
To truncate a file in C#, use the FileStream.SetLength method. This method allows you to change the size of a file by either reducing it (truncating) or expanding it to a specified length. Syntax Following is the syntax for the SetLength method − public override void SetLength(long value); Parameters value − A long representing the desired length of the stream in bytes. How It Works The behavior of SetLength depends on whether the new value is smaller or larger than the current file size − ...
Read MoreInitialization vs Instantiation in C#
In C#, initialization and instantiation are two fundamental concepts that are often confused. Initialization refers to assigning a value to a variable when it is declared, while instantiation refers to creating a new object instance using the new keyword. Initialization Initialization is the process of assigning a value to a variable at the time of declaration. This can be done for value types, reference types, and collections − Value Type Initialization using System; class Program { public static void Main() { int val = 50; ...
Read MoreComparison of double and float primitive types in C#
In C#, float and double are both floating-point data types used to store decimal numbers, but they differ significantly in precision, memory usage, and range. Understanding these differences is crucial for choosing the right data type for your applications. Syntax Following is the syntax for declaring float and double variables − float floatVariable = 3.14f; double doubleVariable = 3.14159265359; Note the f suffix for float literals and optional d suffix for double literals − float price = 19.99f; double pi = 3.14159265359d; // 'd' is optional for double Key Differences ...
Read More