Programming Articles

Page 45 of 2547

Add key and value into StringDictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 282 Views

The StringDictionary class in C# provides a specialized collection for storing string key-value pairs. It is case-insensitive, meaning keys are automatically converted to lowercase when added. The Add() method is used to insert new key-value pairs into the collection. Syntax Following is the syntax for adding key-value pairs to a StringDictionary − StringDictionary dictionary = new StringDictionary(); dictionary.Add(key, value); Parameters key − The key to add to the StringDictionary (string type). value − The value associated with the key (string type). Key Rules ...

Read More

Get the number of key/value pairs in the Dictionary in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 654 Views

The Dictionary class in C# provides the Count property to get the number of key/value pairs stored in the dictionary. This property returns an integer representing the total count of elements currently in the dictionary. Syntax Following is the syntax for using the Count property − Dictionary dictionary = new Dictionary(); int count = dictionary.Count; Parameters The Count property does not take any parameters. It is a read-only property that returns the current number of key/value pairs in the dictionary. Return Value The Count property returns an int value representing the ...

Read More

How do you give a C# Auto-Property a default value?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

In C#, auto-properties provide a shorthand way to declare properties without explicitly writing getter and setter methods. Setting default values for auto-properties can be done in two main ways: using constructor initialization (available in all C# versions) or using property initializers (introduced in C# 6.0). Syntax Constructor initialization syntax (C# 5.0 and earlier) − public class ClassName { public PropertyType PropertyName { get; set; } public ClassName() { PropertyName = defaultValue; ...

Read More

How to get all the files, sub files and their size inside a directory in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

To get all files and subdirectories within a directory in C#, the Directory.GetFiles method provides a comprehensive solution. This method returns the names of all files (including their full paths) that match a specified search pattern and can optionally search through subdirectories. The FileInfo class allows you to retrieve detailed information about each file, including its size, creation date, and other properties. Syntax Following is the syntax for using Directory.GetFiles − string[] files = Directory.GetFiles(path, searchPattern, searchOption); Parameters path − The directory path to search searchPattern − The search pattern (e.g., ...

Read More

SortedDictionary.Keys Property in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 309 Views

The SortedDictionary.Keys property in C# returns a collection containing all the keys in the SortedDictionary. The keys are returned in sorted order based on the comparer used by the SortedDictionary. Syntax Following is the syntax for the Keys property − public SortedDictionary.KeyCollection Keys { get; } Return Value The Keys property returns a SortedDictionary.KeyCollection containing all the keys in the SortedDictionary. The collection is read-only and reflects changes made to the underlying dictionary. Using Keys Property with Integer Keys Example using System; using System.Collections.Generic; public class Demo { ...

Read More

Getting the keys in a SortedList object C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 128 Views

The SortedList class in C# stores key-value pairs sorted by the keys. To retrieve all the keys from a SortedList object, you use the Keys property, which returns an ICollection containing all the keys in sorted order. Syntax Following is the syntax for getting keys from a SortedList − SortedList sortedList = new SortedList(); ICollection keys = sortedList.Keys; The Keys property returns an ICollection that can be iterated using a foreach loop − foreach(string key in keys) { Console.WriteLine(key); } Using Keys Property with String Keys ...

Read More

ConvertDecimal to equivalent 32-bit unsigned integer in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 507 Views

To convert the value of the specified decimal to the equivalent 32-bit unsigned integer, C# provides the Decimal.ToUInt32() method. This method truncates the fractional part and returns only the whole number portion as a uint value. Syntax Following is the syntax for converting decimal to uint − public static uint ToUInt32(decimal value); Parameters value − The decimal number to be converted to a 32-bit unsigned integer. Return Value Returns a 32-bit unsigned integer (uint) equivalent to the specified decimal value. The fractional part is truncated, not rounded. Using ...

Read More

What is the best data type to use for currency in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 7K+ Views

The best data type to use for currency in C# is decimal. The decimal type is a 128-bit data type specifically designed for financial and monetary calculations that require high precision and accuracy. The decimal type can represent values ranging from 1.0 × 10^-28 to approximately 7.9 × 10^28 with 28-29 significant digits. To initialize a decimal variable, use the suffix m or M − decimal currency = 2.1m; Why Decimal is Best for Currency Unlike float and double data types, decimal can represent fractional numbers like 0.1 exactly without rounding errors. Float and ...

Read More

What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 5K+ Views

Converting seconds to a formatted time string in the format Hour:Minutes:Seconds:Milliseconds is a common requirement in C# applications. The most efficient approach is using the TimeSpan structure, which provides built-in methods for time calculations and formatting. TimeSpan Structure TimeSpan represents a time interval and provides properties like Hours, Minutes, Seconds, and Milliseconds for easy access to time components. The TimeSpan.FromSeconds() method creates a TimeSpan object from a given number of seconds. Syntax Following is the syntax for converting seconds using TimeSpan.FromSeconds() − TimeSpan timeSpan = TimeSpan.FromSeconds(totalSeconds); string formatted = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", ...

Read More

MathF.Min() Method in C# with Examples

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 147 Views

The MathF.Min() method in C# returns the smaller of two specified float values. This method is part of the MathF class, which provides mathematical functions optimized for single-precision floating-point numbers. Syntax Following is the syntax − public static float Min(float val1, float val2); Parameters val1 − The first float number to compare. val2 − The second float number to compare. Return Value Returns a float value representing the smaller of the two input parameters. If either value is NaN (Not a Number), the method returns ...

Read More
Showing 441–450 of 25,467 articles
« Prev 1 43 44 45 46 47 2547 Next »
Advertisements