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 Arjun Thakur
Page 8 of 75
Add key-value pair in C# Dictionary
To add key-value pairs in C# Dictionary, you can use several methods. The Dictionary class provides multiple ways to insert elements, each with its own advantages for different scenarios. Syntax Following are the main syntaxes for adding key-value pairs to a Dictionary − // Using Add() method with separate key and value dictionary.Add(key, value); // Using Add() method with KeyValuePair dictionary.Add(new KeyValuePair(key, value)); // Using indexer syntax dictionary[key] = value; Using Add() Method with Key and Value The most common approach is using the Add() method with separate key and value ...
Read MoreWhat are objects in C#?
In C#, objects are instances of classes that represent real-world entities. An object is created from a class blueprint and contains actual data and behavior. Objects allow you to access and manipulate the members (fields, properties, and methods) defined in a class. To access class members through an object, you use the dot (.) operator. This operator connects the object name with the member name, enabling you to set values, call methods, and interact with the object's functionality. Syntax Following is the syntax for creating an object and accessing its members − ClassName objectName = ...
Read MoreC# Program to get the difference between two dates
The DateTime.Subtract() method in C# is used to calculate the difference between two dates. It returns a TimeSpan object representing the time interval between the two dates. Syntax Following is the syntax for using DateTime.Subtract() method − TimeSpan result = dateTime1.Subtract(dateTime2); Alternatively, you can use the subtraction operator − TimeSpan result = dateTime1 - dateTime2; Parameters The Subtract() takes one parameter − value − A DateTime object to subtract from the current instance. Return Value Returns a TimeSpan object that represents the difference between ...
Read MoreHow to use the Copy(, ,) method of array class in C#
The Array.Copy() method in C# is used to copy elements from one array to another. This method provides a fast and efficient way to duplicate array elements without manually iterating through each element. Syntax Following is the basic syntax for the three-parameter overload − Array.Copy(sourceArray, destinationArray, length); Parameters sourceArray − The array from which elements are copied destinationArray − The array to which elements are copied length − The number of elements to copy from the source array The method copies elements starting from index 0 of the source array ...
Read MoreConvert string to bool in C#
To convert a string to a bool in C#, you can use several methods. The most common approaches are bool.Parse(), bool.TryParse(), and Convert.ToBoolean(). Each method has different behaviors for handling invalid input. Syntax Following is the syntax for converting string to bool using different methods − bool result = bool.Parse(stringValue); bool result = Convert.ToBoolean(stringValue); bool.TryParse(stringValue, out bool result); Using bool.Parse() The bool.Parse() method converts a string representation of a boolean value to its boolean equivalent. It accepts "True" or "False" (case-insensitive) and throws an exception for invalid values − using System; ...
Read MoreAsQueryable() in C#
The AsQueryable() method in C# is used to convert an IEnumerable collection into an IQueryable interface. This conversion enables LINQ query providers to translate queries into optimized database queries or other queryable data sources. The key difference is that IEnumerable executes queries in-memory using LINQ to Objects, while IQueryable can be translated into expression trees for remote execution, such as database queries. Syntax Following is the syntax for using AsQueryable() method − IQueryable queryable = collection.AsQueryable(); The method returns an IQueryable that represents the original collection as a queryable data source. Using ...
Read MoreWhat is the Count property of Hashtable class in C#?
The Count property of the Hashtable class in C# returns the total number of key-value pairs stored in the hashtable. It is a read-only property that provides an efficient way to determine the size of the collection. Syntax Following is the syntax for using the Count property − int count = hashtable.Count; Return Value The Count property returns an int value representing the number of key-value pairs currently stored in the hashtable. Using Count Property with Basic Operations The following example demonstrates how to use the Count property to track the ...
Read MoreSort KeyValuePairs in C#
The KeyValuePair structure in C# represents a key-value pair that can be stored in collections. When working with lists of KeyValuePairs, you often need to sort them based on either the key or value. C# provides several approaches to accomplish this sorting. Syntax Following is the syntax for sorting KeyValuePairs using the Sort() method with a lambda expression − myList.Sort((x, y) => x.Value.CompareTo(y.Value)); // Sort by Value (ascending) myList.Sort((x, y) => y.Value.CompareTo(x.Value)); // Sort by Value (descending) myList.Sort((x, y) => x.Key.CompareTo(y.Key)); // Sort by Key (ascending) Following ...
Read MoreWhat are rvalue and lvalue in C#?
In C#, expressions are categorized into two types based on their position and usage in assignment operations: lvalue and rvalue. Understanding these concepts helps in writing correct assignment statements and understanding compiler errors. Definitions lvalue − An expression that represents a memory location and can appear on either the left-hand or right-hand side of an assignment. The term "lvalue" means "left value". rvalue − An expression that represents a value and can only appear on the right-hand side of an assignment. The term "rvalue" means "right value". ...
Read MoreC# Linq Reverse Method
The LINQ Reverse() method in C# is used to reverse the order of elements in a sequence. It returns a new sequence with elements in reverse order without modifying the original collection. Syntax Following is the syntax for the LINQ Reverse() method − public static IEnumerable Reverse( this IEnumerable source ) Parameters source: The sequence of values to reverse. It must implement IEnumerable. Return Value The Reverse() method returns an IEnumerable whose elements correspond to those of the input sequence in reverse order. ...
Read More