Programming Articles

Page 816 of 2547

How to handle empty collections in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 487 Views

Handling empty collections in C# is a common scenario that can lead to runtime errors if not managed properly. The DefaultIfEmpty() method from LINQ provides an elegant solution to work with empty collections by returning a default value when the collection contains no elements. When working with collections, you often need to ensure that operations don't fail on empty collections. The DefaultIfEmpty() method prevents exceptions and provides predictable behavior. Syntax Following is the syntax for using DefaultIfEmpty() method − collection.DefaultIfEmpty() collection.DefaultIfEmpty(defaultValue) Parameters defaultValue (optional) − The value to return if the ...

Read More

Background Worker Class in C#

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

The BackgroundWorker class in C# allows you to run long-running operations on a separate thread while keeping the user interface responsive. It provides a simple way to execute tasks in the background and communicate with the main thread for progress updates and completion notifications. BackgroundWorker is particularly useful in Windows Forms applications where intensive tasks need to run without freezing the UI. It handles thread management automatically and provides events for progress reporting and task completion. Key Properties Property Description CancellationPending Indicates whether the application has requested cancellation of the ...

Read More

What are nested namespaces in C#?

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 2K+ Views

A nested namespace in C# is a namespace declared inside another namespace. This hierarchical structure helps organize code logically by grouping related classes and functionalities under appropriate namespace levels. Nested namespaces provide better code organization, prevent naming conflicts, and create a logical hierarchy that reflects the structure of your application. Syntax Following is the syntax for declaring nested namespaces − namespace OuterNamespace { namespace InnerNamespace { public class MyClass { // ...

Read More

String format for Double in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 2K+ Views

The String.Format method in C# provides powerful options for formatting double values. You can control decimal places, add thousands separators, and apply various numeric formatting patterns to display doubles exactly as needed. Syntax Following is the basic syntax for formatting double values − String.Format("{0:format_specifier}", double_value) Common format specifiers for doubles − {0:0.000} // Fixed decimal places {0:0, 0.0} // Thousands separator with decimal {0:F2} // Fixed-point notation with 2 decimals {0:N2} ...

Read More

Display years in different formats with C# DateTime

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 223 Views

In C#, you can display years in different formats using the DateTime.ToString() method with specific year format specifiers. The DateTime class provides several format specifiers to control how the year is displayed. Syntax Following is the syntax for formatting years using DateTime.ToString() − DateTime dt = DateTime.Now; dt.ToString("format_specifier"); Year Format Specifiers Format Specifier Description Example Output yy Two-digit year (00-99) 23 yyy Three-digit year with leading zeros 2023 yyyy Four-digit year 2023 yyyyy Five-digit year with leading zero 02023 ...

Read More

Get first three letters from every string in C#

George John
George John
Updated on 17-Mar-2026 3K+ Views

In C#, you can extract the first three letters from every string using the Substring() method combined with LINQ operations. This is useful for creating abbreviations, prefixes, or shortened versions of strings in a collection. Syntax Following is the syntax for getting a substring from a string − string.Substring(startIndex, length) Following is the syntax for applying substring to a collection using LINQ − collection.Select(str => str.Substring(0, 3)) Using Substring with LINQ Select The most common approach is to use the Substring() method with LINQ's Select() method to transform each ...

Read More

How to use the Copy(, ,) method of array class in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 441 Views

The Array.Copy() method in C# is used to copy elements from one array to another. This method provides a fast and efficient way to duplicate array elements without manually iterating through each element. Syntax Following is the basic syntax for the three-parameter overload − Array.Copy(sourceArray, destinationArray, length); Parameters sourceArray − The array from which elements are copied destinationArray − The array to which elements are copied length − The number of elements to copy from the source array The method copies elements starting from index 0 of the source array ...

Read More

Basic calculator program using C#

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

A calculator program in C# can be built using either Windows Forms or Console applications. This tutorial demonstrates how to create a basic calculator that performs fundamental arithmetic operations like addition, subtraction, multiplication, and division. We'll create a complete console-based calculator that takes user input and performs calculations, making it easy to understand the core logic before moving to GUI implementations. Console-Based Calculator Basic Calculator with Menu using System; class Calculator { public static void Main(string[] args) { double num1, num2, result; ...

Read More

How do you find the length of an array in C#?

George John
George John
Updated on 17-Mar-2026 18K+ Views

To find the length of an array in C#, use the Length property (not a method). The Length property returns the total number of elements in the array as an integer value. Syntax Following is the syntax for using the Length property − int length = arrayName.Length; Parameters arrayName − The array whose length you want to find Return Value The Length property returns an int value representing the total number of elements in the array. Example Let us see an example − using System; ...

Read More

String Literal Vs String Object in C#

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

In C#, strings can be created using string literals or as string objects. Understanding the difference between these two approaches is important for memory management and performance optimization. String Literals String literals are constants enclosed in double quotes "" or prefixed with @"" for verbatim strings. They are stored in a special area of memory called the string pool, where identical string values are shared to save memory. Syntax Following is the syntax for string literals − string variableName = "string value"; string verbatimString = @"string with multiple lines or special characters"; ...

Read More
Showing 8151–8160 of 25,466 articles
« Prev 1 814 815 816 817 818 2547 Next »
Advertisements