Csharp Articles

Page 132 of 196

Iterating C# StringBuilder in a foreach loop

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

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 More

Clear a StringBuilder in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 2K+ Views

The Clear() method in C# is used to remove all characters from a StringBuilder object, effectively resetting it to an empty state. This method is efficient as it doesn't create a new object but simply resets the internal character buffer. Syntax Following is the syntax for the Clear() method − stringBuilder.Clear(); Return Value The Clear() method returns a reference to the same StringBuilder instance with all characters removed. This allows for method chaining. Using StringBuilder Clear() Method Example The following example demonstrates how to clear a StringBuilder and check its ...

Read More

Access a character in C# StringBuilder

Samual Sam
Samual Sam
Updated on 17-Mar-2026 486 Views

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 More

C# Program to change a character from a string

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 363 Views

In C#, strings are immutable, meaning their characters cannot be changed directly. However, you can change characters in a string using the StringBuilder class, which provides a mutable sequence of characters. The StringBuilder class allows you to modify individual characters using indexer notation str[index] = newChar, where the index is zero-based. Syntax Following is the syntax for changing a character in a StringBuilder − StringBuilder stringBuilder = new StringBuilder("original"); stringBuilder[index] = 'newChar'; Using StringBuilder to Change Characters Let's say our string is − StringBuilder str = new StringBuilder(); str.Append("pre"); ...

Read More

C# program to remove characters starting at a particular index in StringBuilder

Samual Sam
Samual Sam
Updated on 17-Mar-2026 185 Views

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 More

Declare char arrays in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 7K+ Views

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 More

CompareTo() method in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 1K+ Views

The CompareTo() method in C# is used to compare two values and returns an integer that indicates their relative position in the sort order. This method is part of the IComparable interface and is available for most built-in data types like integers, strings, and dates. Syntax Following is the syntax for using the CompareTo() method − int result = value1.CompareTo(value2); Return Value The CompareTo() method returns the following integer values − 0 = both values are equal Positive integer (typically 1) = the calling value is greater than the parameter Negative ...

Read More

StreamWriter in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 277 Views

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

GroupBy() Method in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 3K+ Views

The GroupBy() method in C# is a LINQ extension method that groups elements from a collection based on a specified key selector function. It returns an IGrouping where elements sharing the same key are grouped together. Syntax Following is the syntax for the GroupBy() method − public static IEnumerable GroupBy( this IEnumerable source, Func keySelector ) Parameters source − The collection to group elements from. keySelector − A function that extracts the key for each element. Return Value Returns an ...

Read More

Orderby clause in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 518 Views

The orderby clause in C# is used to sort elements in a collection based on one or more specified criteria. It supports both ascending (default) and descending order, and can be used with LINQ query expressions or method syntax. Syntax Following is the syntax for using orderby in query expression − var result = from element in collection orderby element.Property [ascending|descending] select element; Following is the syntax for ...

Read More
Showing 1311–1320 of 1,951 articles
« Prev 1 130 131 132 133 134 196 Next »
Advertisements