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
Csharp Articles
Page 118 of 196
C# 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 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 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 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 MoreC# program to convert time from 12 hour to 24 hour format
Converting time from 12-hour format to 24-hour format in C# is a common requirement in applications. The DateTime.Parse() method can parse 12-hour time strings, and the ToString() method with the "HH:mm" format specifier converts it to 24-hour format. Syntax Following is the syntax for parsing 12-hour time and converting to 24-hour format − DateTime dateTime = DateTime.Parse("12-hour time string"); string time24Hour = dateTime.ToString("HH:mm"); The HH format specifier represents hours in 24-hour format (00-23), while mm represents minutes (00-59). Using DateTime.Parse() for Basic Conversion The simplest approach uses DateTime.Parse() to convert a 12-hour ...
Read MoreC# program to find Largest, Smallest, Second Largest, Second Smallest in a List
Finding the largest, smallest, second largest, and second smallest elements in a list is a common programming task in C#. Using LINQ methods like Max(), Min(), OrderBy(), and Skip() provides an elegant solution for these operations. Syntax To find the largest element − list.Max() To find the smallest element − list.Min() To find the second largest element − list.OrderByDescending(x => x).Skip(1).First() To find the second smallest element − list.OrderBy(x => x).Skip(1).First() Using LINQ Methods The most straightforward approach uses LINQ extension ...
Read MoreC# program to find common elements in three arrays using sets
In C#, you can find common elements in three arrays efficiently using HashSet collections. A HashSet provides fast lookups and set operations, making it ideal for finding intersections between collections of data. The HashSet class offers built-in methods like IntersectWith() that can perform set operations directly, eliminating the need for complex loops and comparisons. Syntax Following is the syntax for creating a HashSet from an array − var hashSet = new HashSet(array); Following is the syntax for finding intersection using IntersectWith() − hashSet1.IntersectWith(hashSet2); hashSet1.IntersectWith(hashSet3); Finding ...
Read MoreWhat is an object pool in C#?
An object pool in C# is a software design pattern that maintains a collection of reusable objects to optimize resource usage and improve performance. Instead of constantly creating and destroying expensive objects, the pool keeps pre-initialized objects ready for use. The object pool pattern works on two fundamental operations − Rent/Get: When an object is needed, it is retrieved from the pool. Return: When the object is no longer needed, it is returned to the pool for reuse. Object Pool Pattern ...
Read MoreString Template Class in C#
The StringTemplate class is part of the NString library that provides enhanced string formatting capabilities in C#. It offers a more readable alternative to String.Format by using named placeholders instead of numbered ones, making code less error-prone and easier to maintain. The StringTemplate class comes with the NString library, which includes several useful extension methods for string manipulation such as IsNullOrEmpty(), IsNullOrWhiteSpace(), Join(), Truncate(), Left(), Right(), and Capitalize(). Syntax The basic syntax for StringTemplate.Format uses named placeholders − string result = StringTemplate.Format("{PropertyName}", new { PropertyName = value }); For formatting with specific format ...
Read MoreWhat is abstraction in C#?
Abstraction is a fundamental concept in object-oriented programming that focuses on hiding complex implementation details while showing only the essential features of an object. In C#, abstraction allows you to define what an object does without specifying how it does it. Abstraction and encapsulation work together − abstraction defines the interface and essential behavior, while encapsulation hides the internal implementation details from the outside world. Key Concepts of Abstraction Hide complexity − Users interact with simplified interfaces without knowing internal workings Focus on essential features − Only relevant methods and properties are exposed Provide common interface ...
Read More