Arjun Thakur

Arjun Thakur

749 Articles Published

Articles by Arjun Thakur

Page 4 of 75

How to display Absolute value of a number in C#?

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

To find the absolute value of a number in C#, use the Math.Abs method. The absolute value represents the distance of a number from zero on the number line, always returning a non-negative result. Syntax The Math.Abs method is overloaded to work with different numeric types − Math.Abs(int value) Math.Abs(long value) Math.Abs(float value) Math.Abs(double value) Math.Abs(decimal value) Parameters value − The number whose absolute value is to be computed. Return Value Returns the absolute value of the specified number. If the number is positive or zero, it ...

Read More

What are the differences between a static and a non-static class in C#?

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

A static class in C# cannot be instantiated and contains only static members, while a non-static class can be instantiated to create objects and can contain both static and instance members. The key difference is that static classes are designed for utility functions that don't require object state, whereas non-static classes represent entities that can have multiple instances with their own data. Syntax Following is the syntax for declaring a static class − public static class ClassName { public static void StaticMethod() { ...

Read More

What is the Item property of Hashtable class in C#?

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

The Item property of the Hashtable class in C# gets or sets the value associated with the specified key. This property uses the indexer syntax [key] and allows you to both retrieve existing values and add new key-value pairs to the hashtable. When a key does not exist in the hashtable, you can use the Item property to add it along with its value. This makes it convenient for both accessing and modifying hashtable contents. Syntax Following is the syntax for using the Item property − // Getting a value object value = hashtable[key]; ...

Read More

C# Numeric Promotion

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

Numeric promotion in C# is the automatic conversion of smaller numeric types to larger types during arithmetic operations. This ensures that operations between different numeric types can be performed without data loss, following C#'s type promotion rules. When performing arithmetic operations, C# automatically promotes operands to a common type that can safely hold the result. For example, when multiplying a short and ushort, both are promoted to int before the operation. How Numeric Promotion Works The C# compiler follows a specific hierarchy when promoting numeric types during arithmetic operations − Numeric Promotion ...

Read More

C# Program to return specified number of elements from the end of an array

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

The TakeLast() method in C# returns a specified number of elements from the end of a sequence. It's part of the LINQ library and provides an efficient way to extract the last few elements from arrays, lists, or other enumerable collections. Syntax Following is the syntax for using TakeLast() − IEnumerable TakeLast(int count) Parameters count − The number of elements to return from the end of the sequence. Return Value Returns an IEnumerable containing the specified number of elements from the end of the source sequence. Using TakeLast() ...

Read More

How to find the product of 2 numbers using recursion in C#?

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

Finding the product of two numbers using recursion in C# is an interesting approach that demonstrates how multiplication can be implemented using only addition and recursive function calls. Instead of using the standard multiplication operator, we build the product by repeatedly adding one number to itself. How Recursive Multiplication Works The concept is based on the mathematical principle that multiplication is repeated addition. For example, 5 × 3 = 5 + 5 + 5. In recursion, we add the first number to itself and reduce the second number by 1 until it reaches zero. ...

Read More

How to compare two tuples in C#?

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

Tuple comparison was introduced in C# 7.3, allowing you to easily compare two tuples using equality operators. Tuples are compared element-wise, meaning each corresponding element in both tuples must be equal for the tuples to be considered equal. Syntax Following is the syntax for comparing tuples using the equality operator − bool result = tuple1 == tuple2; You can also use the inequality operator − bool result = tuple1 != tuple2; How Tuple Comparison Works Tuple comparison in C# follows these rules: Tuples must have the same ...

Read More

How to capture file not found exception in C#?

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

The FileNotFoundException is thrown when you try to access a file that does not exist on the system. This commonly occurs when using classes like StreamReader, File.ReadAllText(), or other file I/O operations with an incorrect file path. Syntax Following is the syntax for handling FileNotFoundException using try-catch − try { // File operation that might throw FileNotFoundException } catch (FileNotFoundException ex) { // Handle the exception Console.WriteLine("File not found: " + ex.Message); } Using StreamReader with Exception Handling When using StreamReader to read a ...

Read More

C# Program to create a LinkedList

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

A LinkedList in C# is a generic collection that stores elements in nodes, where each node contains the data and references to the next and previous nodes. Unlike arrays, LinkedList elements are not stored in contiguous memory locations and allow efficient insertion and removal operations. Syntax Following is the syntax for creating a LinkedList − LinkedList listName = new LinkedList(); You can also create a LinkedList from an existing collection − LinkedList listName = new LinkedList(collection); Creating LinkedList from Array You can create a LinkedList by passing an array ...

Read More

What does Array.IsSynchronized property of array class do in C#?

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

The Array.IsSynchronized property in C# gets a boolean value indicating whether access to the Array is synchronized (thread-safe). This property is part of the ICollection interface implementation and helps determine if an array can be safely accessed by multiple threads simultaneously. For standard C# arrays, this property always returns false, meaning that arrays are not inherently thread-safe. When multiple threads need to access the same array, you must implement your own synchronization using the SyncRoot property or other synchronization mechanisms. Syntax Following is the syntax for the Array.IsSynchronized property − public bool IsSynchronized { get; ...

Read More
Showing 31–40 of 749 articles
« Prev 1 2 3 4 5 6 75 Next »
Advertisements