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
Programming Articles
Page 869 of 2547
How to write single-line comments in C#?
Single-line comments in C# are created using two forward slashes (//). Everything after the // on that line is treated as a comment and is ignored by the compiler. These comments are useful for explaining code, adding notes, or temporarily disabling a line of code. Syntax Following is the syntax for single-line comments in C# − // This is a single-line comment int variable = 10; // Comment at the end of a line Using Single-Line Comments for Code Documentation Example using System; namespace Demo { class ...
Read MoreHow to compare two lists and add the difference to a third list in C#?
When working with lists in C#, you often need to compare two lists and find the elements that exist in one list but not in another. This is commonly done using LINQ's Except method, which returns the set difference between two sequences. Syntax Following is the syntax for using the Except method to find differences between two lists − IEnumerable result = list1.Except(list2); To store the result in a new list − List differenceList = list1.Except(list2).ToList(); Using Except Method to Find Differences The Except method returns elements from the ...
Read MoreWhere to use #region directive in C#?
The #region directive in C# is a preprocessor directive that allows you to specify a block of code that can be collapsed or expanded in code editors like Visual Studio. This feature improves code organization and readability by grouping related code sections together. The #region directive must always be paired with a corresponding #endregion directive to mark the end of the collapsible block. Syntax Following is the syntax for using the #region directive − #region Region Name or Description // Code block that can be collapsed #endregion Basic Usage of #region Example ...
Read MoreDifference between IComparable and IComparer Interface in C#
The IComparable and IComparer interfaces in C# are both used for sorting and comparing objects, but they serve different purposes and are implemented in different ways. The IComparable interface allows an object to compare itself with another object of the same type. The IComparer interface provides a way to compare two objects externally, offering more flexibility for custom sorting logic. Syntax Following is the syntax for implementing IComparable − public class ClassName : IComparable { public int CompareTo(object obj) { // comparison logic ...
Read MorePush vs pop in stack class in C#
The Stack class in C# represents a last-in, first-out (LIFO) collection of objects. It is used when you need to store and retrieve elements in reverse order — the last element added is the first one to be removed. The Stack class provides two fundamental operations: Push() to add elements and Pop() to remove elements from the top of the stack. Stack LIFO Operations D (Top) C B A ...
Read MoreHow to convert a number from Decimal to Binary using recursion in C#?
Converting a decimal number to binary using recursion in C# involves repeatedly dividing the number by 2 and collecting the remainders. The recursive approach breaks down the problem into smaller subproblems until the base case is reached. Syntax Following is the basic syntax for the recursive binary conversion method − public void ConvertToBinary(int decimalNumber) { if (decimalNumber > 0) { ConvertToBinary(decimalNumber / 2); Console.Write(decimalNumber % 2); } } How It ...
Read MoreWhat is the operator that concatenates two or more string objects in C#?
The + operator is used to concatenate two or more string objects in C#. This operator provides a simple and intuitive way to combine strings, making it one of the most commonly used string operations in C# programming. Syntax Following is the basic syntax for string concatenation using the + operator − string result = string1 + string2; string result = string1 + string2 + string3; Using + Operator for String Concatenation Example 1: Basic String Concatenation using System; class Program { static void Main() { ...
Read MoreDifferences between C and C#
C is a general-purpose, high-level programming language originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was first implemented on the DEC PDP-11 computer in 1972. C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg. It was designed to combine the power of C++ with the simplicity of Visual Basic. While both languages share the letter "C" in their names, they represent different programming paradigms and approaches. Here are the key differences between C and C#. ...
Read MoreHow to compare two lists for equality in C#?
Comparing two lists for equality in C# can be done using several approaches. The most common methods include using SequenceEqual() for order-dependent comparison, Except() to find differences, and custom logic for specific comparison requirements. Using SequenceEqual() for Order-Dependent Comparison The SequenceEqual() method from LINQ compares two sequences element by element in the same order − using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List list1 = new List {"A", "B", "C"}; List list2 ...
Read MoreHow to remove an empty string from a list of empty strings in C#?
In C#, you can remove empty strings from a list using several methods. Empty strings can be truly empty ("") or contain only whitespace characters (" "). This article demonstrates different approaches to remove these empty or whitespace-only strings from a list. Syntax Using RemoveAll() method to remove empty strings − list.RemoveAll(string.IsNullOrEmpty); Using RemoveAll() with lambda expression to remove empty and whitespace strings − list.RemoveAll(x => string.IsNullOrWhiteSpace(x)); Using LINQ Where() method to filter out empty strings − var filteredList = list.Where(x => !string.IsNullOrEmpty(x)).ToList(); Using RemoveAll() Method ...
Read More