C# Average Method

George John
Updated on 17-Mar-2026 07:04:35

23K+ Views

The Average() method in C# calculates the arithmetic mean of a sequence of numeric values. This method is part of LINQ (Language Integrated Query) and can be used with arrays, lists, and other enumerable collections. The Average() method has multiple overloads to work with different numeric types including int, double, decimal, float, and their nullable counterparts. Syntax Following is the basic syntax for the Average() method − public static double Average(this IEnumerable source) public static double Average(this IEnumerable source) public static decimal Average(this IEnumerable source) Parameters source − An enumerable ... Read More

DateTime.DaysInMonth() Method in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:35

3K+ Views

The DateTime.DaysInMonth() method in C# returns the number of days in the specified month and year. This static method is particularly useful for calendar calculations and date validations, automatically handling leap years and different month lengths. Syntax Following is the syntax for the DateTime.DaysInMonth() method − public static int DaysInMonth(int year, int month); Parameters year − An integer representing the year (1 to 9999). month − An integer representing the month (1 to 12). Return Value Returns an int representing the number of days in ... Read More

C# program to count total set bits in a number

karthikeya Boyini
Updated on 17-Mar-2026 07:04:35

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
Updated on 17-Mar-2026 07:04:35

785 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

What is the difference between Read(), ReadKey() and ReadLine() methods in C#?

George John
Updated on 17-Mar-2026 07:04:35

2K+ Views

The Console class in C# provides three different methods for reading user input: Read(), ReadKey(), and ReadLine(). Each method serves a specific purpose and returns different types of data from the standard input stream. Syntax Following are the syntaxes for the three input methods − int result = Console.Read(); // Returns ASCII value ConsoleKeyInfo keyInfo = Console.ReadKey(); // Returns key information string input = Console.ReadLine(); // Returns string Console.Read() Method The Read() method reads the next character from the ... Read More

How to instantiate a class in C#?

Samual Sam
Updated on 17-Mar-2026 07:04:35

5K+ Views

In C#, you create an instance of a class using the new operator. This process is called instantiation, which allocates memory for the object and calls the class constructor. Syntax Following is the basic syntax for instantiating a class − ClassName objectName = new ClassName(); For classes with parameterized constructors − ClassName objectName = new ClassName(parameters); Using Default Constructor When you instantiate a class without parameters, it calls the default constructor. Here's an example using a Line class − using System; class Line { ... Read More

Group by Operator in C#

karthikeya Boyini
Updated on 17-Mar-2026 07:04:35

223 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

C# Cast method

Samual Sam
Updated on 17-Mar-2026 07:04:35

2K+ Views

The Cast() method in C# is a LINQ extension method used to cast each element in a collection from one type to another. It is particularly useful when working with collections of object type that need to be converted to a specific type for further processing. Syntax Following is the syntax for the Cast() method − public static IEnumerable Cast(this IEnumerable source) Parameters source − The collection containing elements to be cast. TResult − The target type to cast elements to. Return Value Returns an IEnumerable containing each element ... Read More

Extracting MAC address using C#

karthikeya Boyini
Updated on 17-Mar-2026 07:04:35

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

Convert.ToBoolean(String, IFormatProvider) Method in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:35

503 Views

The Convert.ToBoolean(String, IFormatProvider) method in C# converts a string representation to an equivalent Boolean value using culture-specific formatting information. This method accepts only specific string values: "True", "False", or their equivalents in different cultures. Syntax Following is the syntax − public static bool ToBoolean(string value, IFormatProvider provider); Parameters value − A string containing the value of either TrueString or FalseString. provider − An object that supplies culture-specific formatting information (this parameter is ignored for Boolean conversions). Return Value Returns true if value equals TrueString, or false if value equals ... Read More

Advertisements