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 881 of 2547
Covariance and Contravariance in C#
Covariance and contravariance in C# enable flexible type relationships when working with generics, delegates, and interfaces. Covariance allows you to use a more derived type than originally specified, while contravariance allows you to use a more general type than originally specified. These concepts are essential for understanding how type safety works with generic interfaces and delegates, particularly when dealing with inheritance hierarchies. Class Hierarchy Example Let us consider the following class hierarchy where One is the base class, Two inherits from One, and Three inherits from Two − using System; class One { ...
Read MoreType.GetEnumUnderlyingType() Method in C#
The Type.GetEnumUnderlyingType() method in C# returns the underlying data type of an enumeration. By default, enums are based on int, but they can also be based on other integral types like byte, short, long, etc. Syntax Following is the syntax − public virtual Type GetEnumUnderlyingType(); Return Value Returns a Type object representing the underlying type of the enumeration. Throws ArgumentException if the current type is not an enumeration. Using GetEnumUnderlyingType() with Default Enum Let us see an example that demonstrates getting the underlying type of a default enum − ...
Read MoreHow to display numbers in the form of Triangle using C#?
To display numbers in the form of a triangle in C#, we use a two-dimensional array to store the triangle values and nested loops to generate the pattern. This creates what's known as Pascal's Triangle, where each number is the sum of the two numbers above it. Syntax Following is the syntax for declaring a two-dimensional array for the triangle − int[, ] array = new int[rows, columns]; Following is the pattern for Pascal's Triangle logic − if (j == 0 || i == j) { a[i, j] ...
Read MoreC# program to print all distinct elements of a given integer array in C#
Finding distinct elements in an array is a common programming task in C#. There are several approaches to accomplish this, including using Dictionary, HashSet, and LINQ methods. Each approach has its own advantages depending on your specific requirements. Using Dictionary to Count Occurrences A Dictionary allows us to store each element as a key and its occurrence count as the value. This approach is useful when you need both distinct elements and their frequencies − using System; using System.Collections.Generic; class Program { public static void Main() { ...
Read MoreObject Initializer in C#
An object initializer in C# allows you to initialize an object's properties or fields at the time of object creation without explicitly calling a constructor with parameters. This feature provides a more readable and concise way to create and initialize objects. Object initializers use curly braces {} to assign values to accessible properties or fields immediately after creating the object instance. Syntax Following is the basic syntax for object initializers − ClassName objectName = new ClassName() { PropertyName1 = value1, PropertyName2 = value2, ...
Read MoreType.GetEnumValues() Method in C#
The Type.GetEnumValues() method in C# returns an array containing the values of all constants in an enumeration type. This method is useful when you need to iterate through all possible values of an enum or perform operations on the entire set of enum values. Syntax Following is the syntax for the GetEnumValues() method − public virtual Array GetEnumValues(); Return Value Returns an Array that contains the values of the constants in the current enumeration type. The elements of the array are sorted by the binary values of the enumeration constants. Using GetEnumValues() ...
Read MoreC# Program to perform Currency Conversion
Currency conversion is a common programming task that involves multiplying an amount by an exchange rate. In C#, we can create simple currency conversion programs using basic arithmetic operations. A currency converter takes an amount in one currency and converts it to another currency using the current exchange rate. The formula is: Converted Amount = Original Amount × Exchange Rate. Syntax Following is the basic syntax for currency conversion − double convertedAmount = originalAmount * exchangeRate; Where variables are declared as − double originalAmount, convertedAmount, exchangeRate; Using Simple Currency ...
Read MoreFile Searching using C#
File searching in C# allows you to programmatically locate and examine files within directories using the System.IO namespace. The DirectoryInfo and FileInfo classes provide powerful methods to search, filter, and retrieve detailed information about files and directories. Syntax Following is the syntax for creating a DirectoryInfo object and searching files − DirectoryInfo directory = new DirectoryInfo(@"path\to\directory"); FileInfo[] files = directory.GetFiles(); Following is the syntax for searching files with specific patterns − FileInfo[] files = directory.GetFiles("*.txt"); // Text files only FileInfo[] files = directory.GetFiles("*", SearchOption.AllDirectories); // ...
Read MoreMonth ("M", "m") Format Specifier in C#
The Month ("M", "m") format specifier in C# represents a custom date and time format string that displays the month and day portions of a date. This format specifier is defined by the current DateTimeFormatInfo.MonthDayPattern property and typically follows the pattern MMMM dd. Syntax Following is the syntax for using the Month format specifier − dateTime.ToString("M") dateTime.ToString("m") The custom format string pattern is − MMMM dd Using Month Format Specifier Basic Example using System; using System.Globalization; class Demo { static void Main() ...
Read MoreC# program to split the Even and Odd integers into different arrays
In C#, you can split an array of integers into separate arrays for even and odd numbers by checking if each element is divisible by 2. This technique is useful for data organization and filtering operations. How It Works The modulo operator (%) is used to determine if a number is even or odd. When a number is divided by 2, if the remainder is 0, the number is even; otherwise, it's odd. Even vs Odd Number Detection Even Numbers num % 2 == 0 ...
Read More