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 karthikeya Boyini
Page 15 of 143
Remove duplicates from a List in C#
Removing duplicates from a List in C# is a common task when working with collections. There are several approaches to accomplish this, with Distinct() being the most straightforward method. The Distinct() method uses LINQ to filter out duplicate elements based on their default equality comparison. Syntax Following is the syntax for using Distinct() to remove duplicates − List uniqueList = originalList.Distinct().ToList(); For custom equality comparison − List uniqueList = originalList.Distinct(comparer).ToList(); Using Distinct() Method The Distinct() method from LINQ is the simplest way to remove duplicates from a list. It ...
Read MoreHow to declare member function in C# interface?
To declare member functions in C# interfaces, you define method signatures without implementations. The implementing class must provide the actual method bodies using the public access modifier. Syntax Following is the syntax for declaring member functions in an interface − public interface InterfaceName { ReturnType MethodName(parameters); void AnotherMethod(); } The implementing class must provide implementations for all interface methods − public class ClassName : InterfaceName { public ReturnType MethodName(parameters) { // implementation } ...
Read MoreAssertions in C#
Assertions in C# are debugging tools used to check assumptions in your code during development. They have two main arguments − a boolean expression that should evaluate to true, and an optional message to display if the assertion fails. Assertions are particularly useful in large and complex programs to quickly identify logic errors that may arise when code is modified. They help catch bugs early in the development process. Syntax Following is the syntax for using Debug.Assert() − Debug.Assert(condition); Debug.Assert(condition, "Error message"); Debug.Assert(condition, "Error message", "Detailed message"); Key Rules Assertions ...
Read MoreC# Program to Subtract Two TimeSpan
In C#, the TimeSpan structure represents a time interval and provides methods to perform arithmetic operations. To subtract one TimeSpan from another, you can use the Subtract() method or the subtraction operator (-). Syntax Following is the syntax for subtracting TimeSpan objects using the Subtract() method − TimeSpan result = timeSpan1.Subtract(timeSpan2); Following is the syntax using the subtraction operator − TimeSpan result = timeSpan1 - timeSpan2; Using Subtract() Method The Subtract() method returns a new TimeSpan that represents the difference between two TimeSpan values − using System; ...
Read MoreLong Date ("D") Format Specifier in C#
The "D" format specifier in C# represents the long date format pattern. It displays dates in a culture-specific long format, showing the full day name, month name, day, and year without time information. The format string is defined by the culture's DateTimeFormatInfo.LongDatePattern property and varies based on the specified culture. Syntax Following is the syntax for using the "D" format specifier − DateTime.ToString("D") DateTime.ToString("D", CultureInfo) The default custom format string for English (US) culture is − dddd, dd MMMM yyyy Using "D" Format with Default Culture Example ...
Read MoreOverriding in C#
Method overriding in C# enables runtime polymorphism by allowing a derived class to provide a specific implementation of a method that is already defined in its base class. This is achieved using the virtual keyword in the base class and the override keyword in the derived class. Overriding is essential for implementing dynamic polymorphism, where the method to be called is determined at runtime based on the actual object type rather than the reference type. Syntax Following is the syntax for method overriding using virtual and override keywords − // Base class with virtual method ...
Read MoreC# program to Loop over a two dimensional array
A two-dimensional array in C# stores elements in a grid format with rows and columns. To loop through all elements, you need to iterate through both dimensions using nested loops. Syntax Following is the syntax for declaring a two-dimensional array − dataType[,] arrayName = new dataType[rows, columns]; Following is the syntax for looping through a two-dimensional array using nested for loops − for (int i = 0; i
Read MoreAddition and Concatenation in C#
In C#, addition refers to mathematical operations with numeric types, while concatenation refers to joining strings together. The + operator can perform both operations depending on the operand types, and C# provides several methods for string concatenation. Syntax Following is the syntax for numeric addition − int result = number1 + number2; Following is the syntax for string concatenation using the + operator − string result = string1 + string2; Following is the syntax for string concatenation using String.Concat() method − string result = String.Concat(string1, string2); ...
Read MoreHow to swap two numbers without using a temp variable in C#
Swapping two numbers without using a temporary variable can be achieved using different techniques in C#. These methods eliminate the need for extra memory allocation and demonstrate clever mathematical and bitwise operations. Using Arithmetic Operations The most common approach uses addition and subtraction operations to swap values − val1 = val1 + val2; val2 = val1 - val2; val1 = val1 - val2; Example using System; class Program { static void Main(string[] args) { int val1, val2; ...
Read MoreHow to define a single-dimensional array in C Sharp?
An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations. A single-dimensional array in C# is a linear collection of elements of the same data type. It allows you to store multiple values in a single variable and access them using an index. Syntax Following is the syntax for declaring a single-dimensional array − datatype[] arrayName = new datatype[size]; Following is the syntax for initializing an array with ...
Read More