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 by Samual Sam
Page 32 of 151
C# program to print duplicates from a list of integers
To print duplicates from a list of integers in C#, you can use several approaches. The most common method involves using a Dictionary to count occurrences of each element, then filtering elements that appear more than once. Using Dictionary with ContainsKey This approach uses a Dictionary to store each number and its count. The ContainsKey method checks if a number already exists in the dictionary − using System; using System.Collections.Generic; class Program { public static void Main() { int[] arr = { 3, ...
Read MoreCohesion in C#
Cohesion in C# refers to how closely related and focused the responsibilities within a class or module are. It measures the functional strength and unity of a module's elements. High cohesion means that a class has a single, well-defined purpose with all its methods working together toward that purpose. The greater the cohesion, the better the program design becomes. High cohesion leads to more maintainable, reusable, and understandable code, while low cohesion results in classes that are difficult to maintain and test. Types of Cohesion Cohesion can be categorized from lowest to highest quality − ...
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 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 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 MoreShort Time ("t") Format Specifier in C#
The Short Time format specifier ("t") in C# is a standard date and time format specifier that displays only the time portion of a DateTime in a short format. It excludes the date and seconds, showing only hours and minutes along with the AM/PM designator for 12-hour formats. The "t" format specifier is defined by the DateTimeFormatInfo.ShortTimePattern property of the current culture. Different cultures may display the time differently based on their regional settings. Syntax Following is the syntax for using the short time format specifier − DateTime.ToString("t") DateTime.ToString("t", CultureInfo) The underlying custom ...
Read MoreWhat are the different types of conditional statements supported by C#?
Conditional statements in C# allow programs to execute different code blocks based on specified conditions. These statements evaluate boolean expressions and direct program flow accordingly, enabling dynamic decision-making in applications. Types of Conditional Statements Statement Description if statement Executes a block of code when a boolean expression is true. if...else statement Executes one block if the condition is true, another if false. nested if statements Uses one if or else if statement inside another for complex conditions. switch statement Tests a variable against multiple values ...
Read MoreWhat is the difference between VAR and DYNAMIC keywords in C#?
The var and dynamic keywords in C# both allow you to declare variables without explicitly specifying their type, but they work very differently. The key difference is when type checking occurs — var is resolved at compile-time, while dynamic is resolved at runtime. Syntax Following is the syntax for declaring a var variable − var variableName = value; Following is the syntax for declaring a dynamic variable − dynamic variableName = value; Using var Keyword The var keyword creates statically typed variables. The compiler determines the type based on ...
Read MoreC# program to add two matrices
Matrix addition is a fundamental operation in linear algebra where two matrices of the same dimensions are added element by element. In C#, we can implement matrix addition using two-dimensional arrays. Syntax To declare a two-dimensional array for matrix operations − int[, ] matrix = new int[rows, columns]; Matrix addition formula for each element − result[i, j] = matrix1[i, j] + matrix2[i, j]; How Matrix Addition Works Matrix addition requires both matrices to have the same dimensions. Each element at position (i, j) in the first matrix is added ...
Read MoreA Deque Class in C#
A Deque (double-ended queue) is a data structure that allows insertion and deletion of elements from both ends. In C#, while there's no built-in Deque class, we can implement one using a doubly-linked list or utilize existing collections like LinkedList to achieve deque functionality. The key advantage of a deque is its flexibility — you can add and remove elements from both the front and back, making it suitable for scenarios like implementing sliding window algorithms, browser history, or undo/redo operations. Deque Operations A typical deque supports the following core operations − AddFirst() — adds ...
Read More