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 89 of 2547
C# program to remove an item from Set
A HashSet in C# is a collection that stores unique elements and provides efficient methods to add, remove, and search items. To remove items from a HashSet, you can use methods like Remove(), RemoveWhere(), or Clear(). Syntax Following are the common methods to remove items from a HashSet − // Remove a specific item bool removed = hashSet.Remove(item); // Remove items based on a condition int count = hashSet.RemoveWhere(predicate); // Remove all items hashSet.Clear(); Using Remove() Method The Remove() method removes a specific item from the HashSet and returns true if ...
Read MoreC# Program to get the difference between two dates in seconds
In C#, you can calculate the difference between two dates in seconds using the TimeSpan structure. When you subtract one DateTime from another, it returns a TimeSpan object that represents the time interval between them. Syntax Following is the syntax for calculating date difference in seconds − DateTime date1 = new DateTime(year, month, day, hour, minute, second); DateTime date2 = new DateTime(year, month, day, hour, minute, second); TimeSpan difference = date2 - date1; double seconds = difference.TotalSeconds; Using DateTime Subtraction The most straightforward approach is to subtract one DateTime from another, which returns ...
Read MoreDecimal.ToUInt64() Method in C#
The Decimal.ToUInt64() method in C# converts a decimal value to a 64-bit unsigned integer (ulong). This method performs truncation, meaning it removes the fractional part and returns only the integer portion of the decimal number. Syntax Following is the syntax − public static ulong ToUInt64(decimal value); Parameters value − The decimal number to convert to a 64-bit unsigned integer. Return Value Returns a ulong (64-bit unsigned integer) that represents the truncated value of the specified decimal. Decimal to UInt64 Conversion ...
Read MoreHow to display Absolute value of a number in C#?
To find the absolute value of a number in C#, use the Math.Abs method. The absolute value represents the distance of a number from zero on the number line, always returning a non-negative result. Syntax The Math.Abs method is overloaded to work with different numeric types − Math.Abs(int value) Math.Abs(long value) Math.Abs(float value) Math.Abs(double value) Math.Abs(decimal value) Parameters value − The number whose absolute value is to be computed. Return Value Returns the absolute value of the specified number. If the number is positive or zero, it ...
Read MoreC# program to remove all duplicates words from a given sentence
Removing duplicate words from a sentence is a common string manipulation task in C#. This process involves splitting the sentence into individual words, identifying duplicates, and keeping only unique words while preserving the original structure. There are several approaches to accomplish this task, ranging from using LINQ's Distinct() method to using collections like HashSet for efficient duplicate removal. Using LINQ Distinct() Method The Distinct() method from LINQ provides a straightforward way to remove duplicates from a collection − using System; using System.Linq; public class Program { public static void Main() { ...
Read MoreScope of Variables in C#
The scope of a variable in C# determines the region of code where a variable can be accessed and used. Understanding variable scope is crucial for writing efficient and error-free programs. C# has several levels of variable scope, each with different accessibility rules and lifetimes. Types of Variable Scope Method Level (Local Variables) Variables declared inside a method are local variables. They are only accessible within that specific method and are destroyed when the method execution completes − using System; class Program { public void TestMethod() { ...
Read MoreCalculate minutes between two dates in C#
Calculating the difference in minutes between two dates is a common requirement in C# applications. The DateTime structure and TimeSpan class provide built-in functionality to perform this calculation efficiently. Syntax Following is the basic syntax for calculating minutes between two dates − DateTime date1 = new DateTime(year, month, day, hour, minute, second); DateTime date2 = new DateTime(year, month, day, hour, minute, second); TimeSpan difference = date2 - date1; double minutes = difference.TotalMinutes; How It Works When you subtract one DateTime from another, C# returns a TimeSpan object that represents the time difference. The ...
Read MoreChar.GetUnicodeCategory(String, Int32) Method with Examples in C#
The Char.GetUnicodeCategory(String, Int32) method in C# categorizes the character at the specified position in a specified string into a group identified by one of the UnicodeCategory values. This method is useful for determining the type of character (letter, digit, punctuation, etc.) at a specific index within a string. Syntax Following is the syntax − public static System.Globalization.UnicodeCategory GetUnicodeCategory(string str, int index); Parameters str − The string to examine. index − The zero-based position of the character to categorize within the string. Return Value Returns a UnicodeCategory enumeration value that ...
Read MoreFinal local variables in C#
In C#, there is no direct equivalent to Java's final keyword for local variables. However, you can achieve similar behavior using the readonly keyword for fields and implicit typing with var or const for local variables that should not change after initialization. The readonly keyword allows a field to be assigned a value only once − either at the time of declaration or in the constructor. Once assigned, it cannot be modified. Syntax Following is the syntax for declaring a readonly field − readonly dataType fieldName; For local variables that should remain constant, ...
Read MoreC# Program to replace a special character from a String
The Replace() method in C# is used to replace all occurrences of a specified character or substring in a string with a new character or substring. This method is particularly useful for removing or replacing special characters from strings. Syntax Following is the syntax for replacing characters using the Replace() method − string.Replace(oldChar, newChar) string.Replace(oldString, newString) Parameters oldChar/oldString − The character or string to be replaced. newChar/newString − The character or string to replace with. Return Value The Replace()
Read More