karthikeya Boyini

karthikeya Boyini

1,420 Articles Published

Articles by karthikeya Boyini

Page 23 of 142

C# program to count total set bits in a number

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 420 Views

A set bit refers to a bit that has a value of 1 in the binary representation of a number. Counting set bits is a common programming problem that involves examining each bit position to determine how many are set to 1. For example, the number 11 in decimal has the binary representation 1011, which contains 3 set bits (three 1s). Approach The most straightforward approach uses bitwise operations to examine each bit − Use the bitwise AND operator (&) with 1 to check if the least significant bit is set Right-shift ...

Read More

What are the differences between ref and out parameters in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 782 Views

The ref and out parameters in C# are both used to pass arguments by reference, but they have distinct behaviors and use cases. Both allow methods to modify the original variable, but they differ in initialization requirements and intended purposes. Syntax Following is the syntax for declaring ref parameters − public void MethodName(ref int parameter) { // parameter can be read and modified } Following is the syntax for declaring out parameters − public void MethodName(out int parameter) { // parameter must be assigned ...

Read More

Group by Operator in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 222 Views

The Group By operator in C# is used to group elements in a collection based on a specified key. It separates the results into groups where each group contains elements that share the same key value. Syntax Following is the syntax for using Group By with LINQ query syntax − var result = from element in collection group element by keySelector; Following is the syntax for using Group By with method syntax − var result = collection.GroupBy(keySelector); ...

Read More

Extracting MAC address using C#

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

A MAC address (Media Access Control address) is a unique identifier assigned to network interfaces for communications at the data link layer of a network segment. It serves as a network address for most IEEE 802 network technologies, including Ethernet, Wi-Fi, and Bluetooth. In C#, you can extract MAC addresses using the NetworkInterface class from the System.Net.NetworkInformation namespace. This class provides methods to enumerate all network interfaces on the local computer and retrieve their physical addresses. Syntax Following is the syntax for getting all network interfaces − NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); Following is ...

Read More

How to iterate any Map in C#

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

C# doesn't have a built-in Map type like Java. Instead, C# uses Dictionary to achieve the same functionality. A Dictionary stores key-value pairs and provides several ways to iterate through its contents. Syntax Following is the syntax for creating a Dictionary − Dictionary dictionary = new Dictionary(); Following are the common iteration patterns − // Iterate over keys foreach (var key in dictionary.Keys) { } // Iterate over values foreach (var value in dictionary.Values) { } // Iterate over key-value pairs foreach (var kvp in dictionary) { } ...

Read More

C# Program to display name of Current Thread

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 343 Views

To display the name of the current thread in C#, use the Name property of the Thread.CurrentThread object. This property allows you to both set and retrieve the name of the currently executing thread. Syntax Following is the syntax to get the current thread − Thread thread = Thread.CurrentThread; Following is the syntax to set and get the thread name − thread.Name = "ThreadName"; string currentName = thread.Name; Using Thread.CurrentThread Property The Thread.CurrentThread property returns a reference to the currently executing thread. You can then use the Name property ...

Read More

C# Program to search for a string in an array of strings

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

Searching for a string in an array of strings is a common programming task. C# provides several methods to accomplish this, with the most straightforward being the LINQ Contains() method. This method returns a boolean value indicating whether the specified string exists in the array. Syntax Following is the syntax for using Contains() method to search in an array − array.Contains(searchString) You can also use it with AsQueryable() for LINQ queries − array.AsQueryable().Contains(searchString) Using LINQ Contains() Method The simplest approach is to use the LINQ Contains() method which returns ...

Read More

How to iterate over a C# dictionary?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 560 Views

A Dictionary in C# is a collection of key-value pairs that can be iterated using several different approaches. Each method provides access to keys, values, or both simultaneously. Syntax Following is the syntax for declaring and initializing a Dictionary − Dictionary dict = new Dictionary(); dict.Add(key, value); Following are the common iteration patterns − // Using KeyValuePair foreach (KeyValuePair pair in dict) { // Access pair.Key and pair.Value } // Using Keys collection foreach (TKey key in dict.Keys) { // Access key and dict[key] } ...

Read More

Round a number to the nearest even number in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 864 Views

The MidpointRounding.ToEven option is used with rounding methods in C# to round a number to the nearest even number when the fractional part is exactly 0.5. This is also known as banker's rounding or round half to even. Syntax Following is the syntax for rounding to the nearest even number − decimal.Round(value, digits, MidpointRounding.ToEven) Math.Round(value, digits, MidpointRounding.ToEven) Parameters value − The decimal or double number to be rounded digits − The number of decimal places in the return value MidpointRounding.ToEven − Rounds to the nearest even number ...

Read More

How to list down all the files available in a directory using C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 481 Views

In C#, you can list all files in a directory using the DirectoryInfo class and its GetFiles() method. This approach provides detailed information about each file, including name, size, and other properties. Syntax Following is the syntax for creating a DirectoryInfo object and getting files − DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\path\to\directory"); FileInfo[] files = directoryInfo.GetFiles(); You can also specify search patterns and options − FileInfo[] files = directoryInfo.GetFiles("*.txt", SearchOption.TopDirectoryOnly); Using DirectoryInfo.GetFiles() The DirectoryInfo class provides detailed file information and is ideal when you need file properties like size, creation ...

Read More
Showing 221–230 of 1,420 articles
« Prev 1 21 22 23 24 25 142 Next »
Advertisements