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
Server Side Programming Articles
Page 801 of 2109
How to insert an item in a list at a given position in C#?
To insert an item in an already created List, use the Insert() method. This method allows you to insert an element at a specific index position within the list, shifting all subsequent elements to the right. Syntax Following is the syntax for the Insert() method − list.Insert(index, item); Parameters index − The zero-based index at which the item should be inserted. item − The object to insert into the list. Insert Operation at Index 3 Before Insert: ...
Read MoreHow to join two lists in C#?
To join two lists in C#, you can use several methods. The most common approaches are using the AddRange() method to modify an existing list, or using LINQ's Concat() method to create a new combined list without modifying the originals. Syntax Using AddRange() to modify the first list − list1.AddRange(list2); Using LINQ Concat() to create a new list − var combinedList = list1.Concat(list2).ToList(); Using AddRange() Method The AddRange() method adds all elements from the second list to the end of the first list, modifying the original list − ...
Read MoreHow 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 More