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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Matching strings with a wildcard in C#
Wildcard characters in C# allow you to match patterns in strings where some characters are unknown. The most common wildcard is the asterisk (*), which represents zero or more characters. C# provides several ways to implement wildcard matching, including regular expressions and custom methods. Using Regular Expressions for Wildcard Matching Regular expressions are the most powerful way to implement wildcard pattern matching. The pattern \bt\S*s\b matches words that start with 't' and end with 's' − Example using System; using System.Text.RegularExpressions; namespace Demo { public class Program { ...
Read MoreC# program to replace n-th character from a given index in a string
In C#, you can replace a character at a specific index in a string by converting the string to a character array, modifying the desired character, and then creating a new string from the modified array. Syntax Following is the basic syntax for replacing a character at a specific index − char[] charArray = originalString.ToCharArray(); charArray[index] = newCharacter; string newString = new string(charArray); Using ToCharArray() Method The most straightforward approach is to convert the string to a character array, replace the character at the desired index, and construct a new string − ...
Read MoreType.GetEnumNames() Method in C#
The Type.GetEnumNames() method in C# is used to return the names of the members of the current enumeration type as an array of strings. This method is particularly useful when you need to dynamically retrieve all the constant names defined in an enum type at runtime. Syntax Following is the syntax for the GetEnumNames() method − public virtual string[] GetEnumNames(); Return Value This method returns a string array containing the names of the enumeration constants. If the current type is not an enumeration type, it throws an ArgumentException. Using GetEnumNames() with Enum ...
Read MoreWhat does the interface IStructuralComparable do in C#?
The IStructuralComparable interface in C# enables structural comparison of collection objects, meaning it compares elements in sequence rather than using reference equality. This interface was introduced in .NET 4.0 to provide consistent comparison behavior for collections and tuples. Syntax Following is the syntax for the IStructuralComparable interface − public interface IStructuralComparable { int CompareTo(object other, IComparer comparer); } Parameters The CompareTo method accepts the following parameters − other − The object to compare with the current instance. comparer − An IComparer object that defines ...
Read MoreC# program to determine if any two integers in array sum to given integer
In this article, we'll learn how to determine if any two integers in an array sum to a given target integer. This is a common programming problem that can be solved using different approaches with varying time complexities. Problem Statement Given an array of integers and a target sum, we need to find if there exist any two numbers in the array that add up to the target value. Using Nested Loops (Brute Force Approach) The simplest approach is to check every possible pair of numbers using nested loops − using System; public ...
Read MoreManaged code vs Unmanaged code in C#
In C#, code can be categorized as either managed or unmanaged based on how it is executed and controlled by the .NET runtime environment. Understanding the difference is crucial for C# developers working with system-level programming or interoperability scenarios. Managed Code Managed code is code whose execution is managed by the Common Language Runtime (CLR). The CLR provides automatic memory management, type safety, exception handling, and garbage collection. When you write C# code, it is compiled into Intermediate Language (IL) code, which is then executed by the CLR. Managed Code Execution Flow ...
Read MoreCovariance 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 More