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 on Trending Technologies
Technical articles with clear explanations and examples
Calculate the sum of squares of the first N natural numbers in C#
In this problem, we are given a number N, and we need to calculate the sum of the squares of the first N natural numbers. The first N natural numbers are 1, 2, 3, ..., N, and we need to find 1² + 2² + 3² + ... + N². Problem Description Given a positive integer N, calculate the sum of squares of the first N natural numbers using different approaches in C#. Example 1 Input: N = 4 Output: 30 Explanation: The squares of the first 4 natural numbers are: 1², ...
Read MoreStack.Contains() Method in C#
The Stack.Contains() method in C# is used to check whether a specific element exists in the Stack or not. It returns true if the element is found, otherwise false. Syntax The syntax for the Stack.Contains() method is as follows − public virtual bool Contains(object obj); Parameters obj − The object to be searched in the stack. It can be null. Return Value Returns a bool value − true − if the element is found in the Stack false − if the element is not found in the ...
Read MoreGets or sets the value in HybridDictionary with specified key in C#
The HybridDictionary class in C# provides an indexer property that allows you to get or set values using a specified key. The HybridDictionary is part of the System.Collections.Specialized namespace and combines the benefits of both ListDictionary and Hashtable for optimal performance with small and large collections. Syntax Following is the syntax for getting or setting values in HybridDictionary using the indexer − // Getting a value object value = hybridDict[key]; // Setting a value hybridDict[key] = value; Parameters key − The key of the element to get or set. Can ...
Read MoreWhat does LINQ return when the results are empty in C#?
Language-Integrated Query (LINQ) is a set of technologies that integrates query capabilities directly into the C# language. When LINQ queries return no results, the behavior depends on which LINQ method you use. LINQ queries can be written for SQL Server databases, XML documents, ADO.NET Datasets, and any collection that supports IEnumerable or IEnumerable. Understanding what happens when queries return empty results is crucial for avoiding runtime exceptions. Common LINQ Methods and Empty Results LINQ Method Empty Result Behavior ToList() Returns an empty List ToArray() Returns an empty array ...
Read MoreHow to post data to specific URL using WebClient in C#?
The WebClient class in C# provides simple methods for sending and receiving data from web servers. It's particularly useful for posting data to specific URLs, making it easy to interact with Web APIs without complex HTTP handling. WebClient offers methods like UploadString, UploadData, and UploadValues for posting different types of data. While HttpClient is the modern alternative, WebClient remains useful for simple scenarios. Namespace and Assembly Namespace: System.Net Assembly: System.Net.WebClient.dll Syntax Following is the basic syntax for posting data using WebClient − using (WebClient client = new WebClient()) { ...
Read MoreBoolean.ToString(IFormatProvider) Method in C#
The Boolean.ToString(IFormatProvider) method in C# converts a Boolean value to its equivalent string representation. This method accepts an IFormatProvider parameter, though Boolean values are not culture-sensitive and always return "True" or "False" regardless of the culture specified. Syntax Following is the syntax for the Boolean.ToString(IFormatProvider) method − public string ToString(IFormatProvider provider); Parameters provider − An IFormatProvider object that provides culture-specific formatting information. This parameter is reserved but not used for Boolean values. Return Value Returns a string representation of the Boolean value. The method returns "True" for true values ...
Read MoreHow to write Regex for numbers only in C#?
Regular expressions (regex) are patterns used to match specific text formats. In C#, you can create regex patterns to validate that input contains only numbers. The System.Text.RegularExpressions.Regex class provides methods to test strings against these patterns. For numbers-only validation, you need to understand the key regex metacharacters and how to construct patterns that accept different numeric formats like integers, decimals, and negative numbers. Syntax Basic regex pattern for numbers only − Regex regex = new Regex(@"^[0-9]+$"); Pattern for numbers with optional decimal point − Regex regex = new Regex(@"^[0-9]+\.?[0-9]*$"); ...
Read MoreMoving mouse pointer to a specific location or element using C# and Selenium
We can move mouse pointer to a specific location or element in Selenium WebDriver with C# using the Actions class. This class provides methods to simulate user interactions like moving the mouse, clicking, and performing complex gestures on web elements. To move the mouse to an element, we use the MoveToElement method and pass the element locator as a parameter. To move to specific coordinates, we use the MoveByOffset method. All actions must be executed using the Perform method. Syntax Following is the syntax for creating an Actions object and moving to an element − ...
Read MoreDouble.CompareTo Method in C# with Examples
The Double.CompareTo() method in C# compares the current double instance to another double value or object. It returns an integer that indicates whether the current value is less than, equal to, or greater than the compared value. Syntax Following are the two overloads of the Double.CompareTo() − public int CompareTo(double value); public int CompareTo(object value); Parameters value − A double-precision floating-point number or object to compare with the current instance. Return Value The method returns an integer with the following meaning − Less than ...
Read MoreHow do you get the file size in C#?
Getting the file size in C# can be accomplished using several methods. The most common approach is using the FileInfo class, which provides the Length property to retrieve the size of a file in bytes. You can also use static methods from the File class for quick operations without creating instances. The FileInfo class is part of the System.IO namespace and provides comprehensive file manipulation capabilities including size retrieval, creation dates, and access permissions. Syntax Using FileInfo class to get file size − FileInfo fileInfo = new FileInfo(filePath); long fileSize = fileInfo.Length; Using ...
Read More