Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 843 of 2547
C# Program to find the largest element from an array
Finding the largest element in an array is a common programming task in C#. There are multiple approaches to achieve this, ranging from using built-in LINQ methods to implementing custom logic with loops. Using LINQ Max() Method The simplest approach is to use the Max() method from the System.Linq namespace − using System; using System.Linq; class Demo { static void Main() { int[] arr = { 20, 50, -35, 25, 60 }; int largest = ...
Read MoreImplicit conversion from 32-bit unsigned integer (UInt) to Decimal in C#
Implicit conversion from a 32-bit unsigned integer (uint) to decimal in C# happens automatically when you assign a uint value to a decimal variable. This conversion is considered safe because decimal can represent all possible uint values without any loss of precision. Syntax Following is the syntax for implicit conversion from uint to decimal − uint uintValue = someValue; decimal decimalValue = uintValue; // implicit conversion How It Works The C# compiler automatically performs this conversion because: uint ranges from 0 to 4, 294, 967, 295 decimal can ...
Read MoreWhat are the differences between constructors and destructors in C#?
A constructor is a special member function that initializes objects when they are created, while a destructor is called when an object goes out of scope or is garbage collected. Understanding the differences between these two fundamental concepts is essential for proper object lifecycle management in C#. Constructors A constructor is a special method that is automatically called when an object is created. It has the same name as the class and no return type. Syntax class ClassName { public ClassName() { // constructor code ...
Read MoreC# program to check whether two sequences are the same or not
The SequenceEqual method in C# is a LINQ extension method that determines whether two sequences are equal by comparing their elements using the default equality comparer. It returns true if the sequences have the same length and corresponding elements are equal, otherwise it returns false. Syntax Following is the syntax for using SequenceEqual method − bool result = sequence1.SequenceEqual(sequence2); You can also use a custom equality comparer − bool result = sequence1.SequenceEqual(sequence2, comparer); Parameters sequence2 − The sequence to compare with the current sequence. comparer ...
Read MoreC# Average Method
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 MoreDateTime.DaysInMonth() Method in C#
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 MoreC# program to count total set bits in a number
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 MoreWhat are the differences between ref and out parameters in C#?
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 MoreWhat is the difference between Read(), ReadKey() and ReadLine() methods in C#?
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 MoreHow to instantiate a class in C#?
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