Articles on Trending Technologies

Technical articles with clear explanations and examples

Static vs. Non-Static method in C#

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

In C#, methods can be declared as either static or non-static (instance methods). Static methods belong to the class itself and can be called without creating an instance of the class, while non-static methods belong to specific instances of the class. Static methods can only access static variables and other static members directly. They exist even before any object of the class is created and are shared across all instances of the class. Syntax Following is the syntax for declaring a static method − public static returnType MethodName() { // method ...

Read More

C# Program to get the difference between two dates

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 4K+ Views

The DateTime.Subtract() method in C# is used to calculate the difference between two dates. It returns a TimeSpan object representing the time interval between the two dates. Syntax Following is the syntax for using DateTime.Subtract() method − TimeSpan result = dateTime1.Subtract(dateTime2); Alternatively, you can use the subtraction operator − TimeSpan result = dateTime1 - dateTime2; Parameters The Subtract() takes one parameter − value − A DateTime object to subtract from the current instance. Return Value Returns a TimeSpan object that represents the difference between ...

Read More

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 224 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 442 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
Showing 10821–10830 of 61,297 articles
Advertisements