Programming Articles

Page 881 of 2547

Covariance and Contravariance in C#

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

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 More

Type.GetEnumUnderlyingType() Method in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 159 Views

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 More

How to display numbers in the form of Triangle using C#?

George John
George John
Updated on 17-Mar-2026 293 Views

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 More

C# program to print all distinct elements of a given integer array in C#

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

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 More

Object Initializer in C#

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

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 More

Type.GetEnumValues() Method in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 434 Views

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 More

C# Program to perform Currency Conversion

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 5K+ Views

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 More

File Searching using C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 635 Views

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 More

Month ("M", "m") Format Specifier in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 396 Views

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 More

C# program to split the Even and Odd integers into different arrays

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

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
Showing 8801–8810 of 25,466 articles
« Prev 1 879 880 881 882 883 2547 Next »
Advertisements