Csharp Articles

Page 58 of 196

How to convert byte array to string in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 20K+ Views

In C#, converting a byte array to a string requires understanding character encoding. Every string has a character set and encoding that tells the computer how to interpret raw bytes into characters. The Encoding class provides various methods to decode byte arrays into strings. The most common approach is using the Encoding.GetString() method, which decodes all bytes in a specified byte array into a string. Several encoding schemes are available such as UTF8, Unicode, UTF32, and ASCII. Syntax Following is the basic syntax for converting byte array to string − string result = Encoding.EncodingType.GetString(byteArray); ...

Read More

What is the main difference between int.Parse() and Convert.ToInt32 in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 6K+ Views

Both int.Parse() and Convert.ToInt32() methods in C# are used to convert string representations of numbers to integers. However, they handle null values and certain edge cases differently. The key difference is that Convert.ToInt32() handles null values gracefully by returning 0, while int.Parse() throws an ArgumentNullException when encountering null. Syntax Following is the syntax for int.Parse() method − int result = int.Parse(stringValue); Following is the syntax for Convert.ToInt32() method − int result = Convert.ToInt32(stringValue); Using int.Parse() with Valid String The int.Parse() method converts a valid numeric string to an ...

Read More

How can we update the values of a collection using LINQ in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 13K+ Views

LINQ provides several approaches to update collection values in C#. While LINQ is primarily designed for querying data, you can combine it with other methods to modify collection elements efficiently. Using ForEach with List Collections The List class provides a ForEach method that can be used to update all elements in the collection − using System; using System.Collections.Generic; namespace DemoApplication { class Program { static void Main(string[] args) { List fruits ...

Read More

How to replace line breaks in a string in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

In C#, you often need to remove or replace line breaks from strings when processing text data. Line breaks can appear as (line feed), \r (carriage return), or \r (Windows-style line endings). This article demonstrates different approaches to handle line breaks in strings. Common Line Break Characters Different operating systems use different line break characters − − Line Feed (LF), used on Unix/Linux/Mac \r − Carriage Return (CR), used on older Mac systems \r − Carriage Return + Line Feed (CRLF), used on Windows Line Break Types ...

Read More

How to find items in one list that are not in another list in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 15K+ Views

Finding items in one list that are not present in another list is a common task in C# programming. LINQ provides multiple approaches to solve this problem, with the Except() method being the most straightforward for simple data types and the Where() clause being more flexible for complex objects. Syntax Using the Except() method for simple types − var result = list1.Except(list2); Using Where() clause with All() for complex objects − var result = list1.Where(item1 => list2.All(item2 => condition)); Using Except() Method The Except() method is a LINQ set ...

Read More

How can we return null from a generic method in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

Generics in C# allow us to define classes and methods with placeholders for types, which are replaced with specific types at compile time. However, returning null from a generic method requires special handling because the compiler cannot determine whether the generic type T is a reference type or value type. When you try to return null directly from a generic method, you'll encounter a compilation error because null cannot be assigned to value types like int, double, etc. The Problem Here's what happens when we try to return null directly from a generic method − ...

Read More

What is the use of yield return in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 3K+ Views

The yield return keyword in C# enables lazy evaluation and custom iteration over collections. When you use yield return, the method becomes an iterator that returns values one at a time, pausing execution between each return and resuming where it left off when the next value is requested. Syntax Following is the syntax for using yield return − public static IEnumerable MethodName() { // some logic yield return value; // more logic yield return anotherValue; } You can ...

Read More

What is the difference between Last() and LastOrDefault() in Linq C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 1K+ Views

Both Last() and LastOrDefault() are LINQ extension methods that retrieve the last element from a sequence. The key difference is how they handle empty sequences or no matching elements: Last() throws an InvalidOperationException when no element is found, while LastOrDefault() returns the default value for the type (null for reference types, 0 for integers, etc.). Syntax Following is the syntax for Last() method − public static T Last(this IEnumerable source); public static T Last(this IEnumerable source, Func predicate); Following is the syntax for LastOrDefault() method − public static T LastOrDefault(this IEnumerable source); ...

Read More

How to convert an integer to string with padding zero in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

Converting an integer to a string with padding zeros is a common requirement in C# programming. This allows you to format numbers with a consistent width by adding leading zeros. There are several approaches to achieve this, each with its own syntax and use cases. Syntax Following are the common syntaxes for zero-padding integers − // Using PadLeft method number.ToString().PadLeft(width, '0'); // Using custom numeric format number.ToString("0000"); // Using standard numeric format number.ToString("D4"); // Using string interpolation $"{number:0000}" Using PadLeft Method The PadLeft method pads the beginning of a string ...

Read More

How to return multiple values to caller method in c#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

Returning multiple values from a method in C# can be achieved through several approaches. The most modern and efficient approach is using ValueTuple, introduced in C# 7.0, which provides a lightweight mechanism for returning multiple values with optional named elements. ValueTuples are both performant and allow referencing by names the programmer chooses. They are available under the System.ValueTuple NuGet package for older framework versions. Syntax Following is the syntax for declaring a method that returns multiple values using ValueTuple − public (int, string, string) MethodName() { return (value1, value2, value3); } ...

Read More
Showing 571–580 of 1,951 articles
« Prev 1 56 57 58 59 60 196 Next »
Advertisements