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 114 of 2547
How to Get Value from HashTable Collection in C# using Specified Key
A Hashtable is a collection of key-value pairs that provides fast lookup by key. In C#, you can retrieve values from a hashtable using the indexer syntax hashtable[key] or by checking if a key exists using the Contains() method. This is essential for accessing specific data when you know the corresponding key. Syntax Following is the syntax for getting a value from a hashtable using a specified key − // Check if key exists if (hashtable.Contains(key)) { object value = hashtable[key]; } // Direct access (may return null if key doesn't exist) ...
Read MoreHow can we return multiple values from a function in C#?
In C#, there are several approaches to return multiple values from a function. This is useful when you need to return more than one result from a single method call. The main approaches are − Reference parameters using ref keyword Output parameters using out keyword Returning an Array of values Returning a Tuple object Using Reference Parameters The ref keyword passes arguments by reference, allowing the method to modify the original variable. The variable must be initialized before passing it to the method. Syntax ...
Read MoreHow to get only Date portion from DateTime object in C#?
There are several ways to extract only the date portion from a DateTime object in C#. Each method serves different purposes depending on whether you need a string representation or want to preserve the DateTime type. Methods Overview Method Return Type Description ToShortDateString() String Returns culture-specific short date format ToLongDateString() String Returns culture-specific long date format ToString(format) String Returns custom formatted date string DateTime.Date DateTime Returns DateTime with time set to 00:00:00 Using ToShortDateString() and ToLongDateString() These methods provide culture-specific ...
Read MoreHow to parse a string into a nullable int in C#?
Parsing a string into a nullable int in C# allows you to handle cases where the string might not represent a valid integer. A nullable int (int?) can store either an integer value or null, making it ideal for scenarios where conversion might fail. C# provides several approaches to parse strings into nullable integers, from extension methods to built-in parsing techniques that handle invalid input gracefully. Syntax Following is the syntax for declaring a nullable int − int? nullableInt = null; int? nullableInt = 42; Following is the syntax for parsing using int.TryParse() ...
Read MoreHow to change the CursorTop of the Console in C#?
The Console.CursorTop property in C# gets or sets the row position of the cursor within the console window. This property allows you to control the vertical position where text will be displayed, enabling you to create formatted console output, menus, or position text at specific locations. Syntax Following is the syntax for using Console.CursorTop − // Get current cursor top position int currentTop = Console.CursorTop; // Set cursor top position Console.CursorTop = value; Parameters The Console.CursorTop property accepts an integer value representing the row position (0-based) where the cursor should be positioned. ...
Read MoreSByte.ToString() Method in C# with Examples
The SByte.ToString() method in C# is used to convert the numeric value of a signed byte to its equivalent string representation. The sbyte data type represents 8-bit signed integers with values ranging from -128 to 127. Syntax Following is the syntax for the basic ToString() method − public override string ToString(); Following is the syntax for ToString() with format provider − public string ToString(IFormatProvider provider); public string ToString(string format); public string ToString(string format, IFormatProvider provider); Return Value The method returns a string that represents the value of the current ...
Read MoreAdd key and value into StringDictionary in C#
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 MoreGet the number of key/value pairs in the Dictionary in C#
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 MoreHow do you give a C# Auto-Property a default value?
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 MoreHow to get all the files, sub files and their size inside a directory in C#?
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