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 841 of 2547
Byte.Equals(Byte) Method in C#
The Byte.Equals(Byte) method in C# returns a value indicating whether this instance and a specified Byte object represent the same value. This method provides a reliable way to compare two byte values for equality. Syntax Following is the syntax − public bool Equals(byte obj); Parameters obj − A byte object to compare to this instance. Return Value Returns true if obj has the same value as this instance; otherwise, false. Using Byte.Equals() for Value Comparison Example using System; public class Demo { public static ...
Read MoreWhat are contextual keywords in C#?
In C#, contextual keywords are special identifiers that have reserved meaning only in specific contexts. Unlike regular keywords, they can still be used as variable names or identifiers when not in their special context. Common examples include get and set in properties, where in LINQ queries, and partial in class definitions. Contextual keywords provide flexibility by allowing the same word to serve as both a keyword and an identifier depending on the context in which it appears. Syntax Contextual keywords are used within their specific contexts. Here are some common patterns − // Property accessors ...
Read MoreWhat are the hidden features of C#?
C# contains several powerful but often overlooked features that can significantly improve code readability, safety, and efficiency. These hidden gems can make your C# programming more elegant and productive when used appropriately. Lambda Expressions A lambda expression is a concise way to write anonymous functions using the => operator (called the "goes to" operator). Lambda expressions are commonly used with LINQ operations and delegate assignments. Syntax (parameters) => expression (parameters) => { statements; } Example using System; using System.Linq; class Program { public static void Main() ...
Read MoreDifference between prefix and postfix operators in C#?
The prefix and postfix operators in C# are increment (++) and decrement (--) operators that behave differently based on their position relative to the variable. The key difference lies in when the increment or decrement occurs and what value is returned. Syntax Following is the syntax for prefix operators − ++variable; // prefix increment --variable; // prefix decrement Following is the syntax for postfix operators − variable++; // postfix increment variable--; // postfix decrement Prefix Operators The prefix operator increments or decrements the variable first, ...
Read MoreHow to initialize a dictionary to an empty dictionary in C#?
To initialize a dictionary to an empty state in C#, there are several approaches. You can create a new empty dictionary directly, use the Clear() method to empty an existing dictionary, or check if a dictionary is already empty using the Count property. Syntax Following is the syntax for creating an empty dictionary − Dictionary dict = new Dictionary(); Following is the syntax for clearing an existing dictionary − dict.Clear(); Following is the syntax for checking if a dictionary is empty − if (dict.Count == 0) { ...
Read MoreSet 6-item tuple in C#'
A 6-item tuple in C# is a data structure that can hold six values of different or same data types. Tuples are useful when you need to return multiple values from a method or group related data together without creating a separate class. Syntax Following is the syntax for creating a 6-item tuple using the Tuple class − var tuple = new Tuple(item1, item2, item3, item4, item5, item6); Alternatively, you can use the modern tuple syntax with parentheses − var tuple = (item1, item2, item3, item4, item5, item6); Using Traditional ...
Read MoreC# Program to convert a Double to an Integer Value
To convert a double value to an integer value in C#, you can use several methods. The most common approach is using the Convert.ToInt32() method, which performs rounded conversion, or explicit casting for truncation. The Int32 data type represents a 32-bit signed integer that can store values from -2, 147, 483, 648 to 2, 147, 483, 647. Syntax Following are the different syntaxes for converting double to integer − // Using Convert.ToInt32() - rounds to nearest integer int result = Convert.ToInt32(doubleValue); // Using explicit casting - truncates decimal part int result = (int)doubleValue; ...
Read MoreDateTimeOffset.AddMinutes() Method in C#
The DateTimeOffset.AddMinutes() method in C# adds a specified number of whole and fractional minutes to a DateTimeOffset instance. This method returns a new DateTimeOffset object with the updated time while preserving the original offset from UTC. The method is particularly useful when working with time-zone aware calculations, as it maintains the timezone offset information throughout the operation. Syntax Following is the syntax − public DateTimeOffset AddMinutes(double minutes); Parameters minutes − A double value representing the number of minutes to add. Can be positive (to add) or negative (to subtract). Fractional values ...
Read MoreC# Program to Count the Number of 1's in the Entered Numbers
This C# program demonstrates how to count the occurrences of a specific number (1) in an array of integers. We'll use an array to store the numbers and iterate through it to count how many times the value 1 appears. Syntax Following is the syntax for declaring an array and using a foreach loop to iterate through it − int[] arrayName = new int[] {value1, value2, value3}; foreach(int variable in arrayName) { // process each element } Using Array and foreach Loop The most straightforward approach is to use ...
Read MoreWhat are dynamic arrays in C#?
Dynamic arrays are growable arrays that can resize themselves during runtime, providing a significant advantage over static arrays which have a fixed size. In C#, you can create dynamic arrays using collections like ArrayList and the more modern List. These dynamic collections allow automatic memory allocation, resizing, adding, searching, and sorting items efficiently. The ArrayList stores objects of any type, while List is type-safe and performs better. Syntax Following is the syntax for creating an ArrayList − ArrayList arrayList = new ArrayList(); arrayList.Add(item); Following is the syntax for creating a generic List − ...
Read More