Ankith Reddy

Ankith Reddy

730 Articles Published

Articles by Ankith Reddy

Page 11 of 73

C# program to create a ValueType with names

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 197 Views

ValueTuples in C# allow you to create lightweight data structures with named fields. Introduced in C# 7, ValueTuples provide a convenient way to group multiple values together without creating a separate class or struct. Note − For .NET Framework projects, you may need to add the System.ValueTuple NuGet package to use ValueTuples. Installing System.ValueTuple Package To add the System.ValueTuple package to your project − Go to your project Right click on the project in the Solution Explorer Select "Manage NuGet Packages" Click the Browse tab and search for "System.ValueTuple" Install the System.ValueTuple package ...

Read More

How to print one dimensional array in reverse order?

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 609 Views

In C#, there are several ways to print a one-dimensional array in reverse order. You can either reverse the actual array using Array.Reverse() or print the elements in reverse without modifying the original array. Syntax Using Array.Reverse() to reverse the array − Array.Reverse(arrayName); Using a reverse loop to print without modifying the array − for (int i = arr.Length - 1; i >= 0; i--) { Console.WriteLine(arr[i]); } Using Array.Reverse() Method The Array.Reverse() method permanently reverses the order of elements in the array − ...

Read More

How to compare two lists and add the difference to a third list in C#?

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

When working with lists in C#, you often need to compare two lists and find the elements that exist in one list but not in another. This is commonly done using LINQ's Except method, which returns the set difference between two sequences. Syntax Following is the syntax for using the Except method to find differences between two lists − IEnumerable result = list1.Except(list2); To store the result in a new list − List differenceList = list1.Except(list2).ToList(); Using Except Method to Find Differences The Except method returns elements from the ...

Read More

Find missing number in a sequence in C#

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

Finding missing numbers in a sequence is a common programming problem. In C#, you can solve this efficiently using LINQ operations by comparing the original sequence with a complete range of numbers. Using LINQ Except Method The most straightforward approach is to create a complete range from the minimum to maximum value and use the Except method to find missing numbers − using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { List myList = new List(){1, 2, 3, 5, 8, ...

Read More

How to find a number in a string in C#?

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 522 Views

To find a number in a string in C#, you can use Regular Expressions (Regex) from the System.Text.RegularExpressions namespace. The Regex pattern \d+ matches one or more consecutive digits in a string. Syntax Following is the syntax for creating a Regex pattern to match numbers − Regex regex = new Regex(@"\d+"); Match match = regex.Match(inputString); Following is the syntax for checking if a match was found − if (match.Success) { string number = match.Value; } Using Regex to Find the First Number The Match method ...

Read More

Enumerable.Repeat method in C#

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

The Enumerable.Repeat method is part of the System.Linq namespace and creates a sequence that contains the same element repeated a specified number of times. This method is useful for initializing collections or generating test data with repeated values. Syntax Following is the syntax for the Enumerable.Repeat method − public static IEnumerable Repeat(TResult element, int count) Parameters element − The value to be repeated in the resulting sequence. count − The number of times to repeat the element. Return Value Returns an IEnumerable that contains the ...

Read More

How to demonstrate Prefix Operator using C#?

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 316 Views

The prefix operator in C# is used to increment or decrement a variable's value before using it in an expression. The prefix increment operator ++ increases the value by 1, while the prefix decrement operator -- decreases the value by 1. The key characteristic is that the operation happens before the variable's value is returned or used. Syntax Following is the syntax for prefix increment and decrement operators − ++variable; // Prefix increment --variable; // Prefix decrement The operators can also be used in expressions − result = ++a; ...

Read More

Value Type vs Reference Type in C#

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

Value type and reference type are two fundamental categories of data types in C#. Understanding their differences is crucial for memory management and program behavior. Value types store data directly, while reference types store a reference to the memory location where the data is stored. Syntax Following is the syntax for declaring value type variables − int number = 10; char letter = 'A'; bool flag = true; Following is the syntax for declaring reference type variables − string text = "Hello"; object obj = new object(); int[] array = new int[5]; ...

Read More

Join, Sleep and Abort methods in C# Threading

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 691 Views

The Thread class in C# provides several important methods to control thread execution: Join(), Sleep(), and Abort(). These methods allow you to synchronize threads, pause execution, and terminate threads when needed. Syntax Following is the syntax for the three main thread control methods − // Join method thread.Join(); thread.Join(timeout); // Sleep method Thread.Sleep(milliseconds); // Abort method (obsolete in .NET Core/.NET 5+) thread.Abort(); Join Method The Join() method blocks the calling thread until the target thread terminates. This ensures that the calling thread waits for another thread to complete before continuing execution. ...

Read More

How to use #if..#elif...#else...#endif directives in C#?

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

Preprocessor directives in C# begin with # and are processed before compilation. The #if..#elif...#else...#endif directives allow conditional compilation, meaning you can include or exclude code blocks based on defined symbols. These directives are useful for creating debug builds, platform-specific code, or feature toggles without affecting runtime performance. Syntax Following is the syntax for conditional compilation directives − #if condition // code block 1 #elif condition // code block 2 #else // code block 3 #endif Key Directives #if − Tests if ...

Read More
Showing 101–110 of 730 articles
« Prev 1 9 10 11 12 13 73 Next »
Advertisements