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 38 of 2547
What is the difference between int and Int32 in C#?
Int32 is a type provided by .NET framework whereas int is an alias for Int32 in C# language. Both represent 32-bit signed integers and compile to identical code, making them functionally equivalent at runtime. Syntax Following is the syntax for declaring integers using both approaches − Int32 variable1 = value; int variable2 = value; Key Differences Aspect int Int32 Type C# language keyword (alias) .NET Framework type Namespace dependency No namespace required Requires System namespace Compilation Compiles to System.Int32 Direct .NET type ...
Read MoreHow to return multiple values to caller method in c#?
Returning multiple values from a method in C# can be achieved through several approaches. The most modern and efficient approach is using ValueTuple, introduced in C# 7.0, which provides a lightweight mechanism for returning multiple values with optional named elements. ValueTuples are both performant and allow referencing by names the programmer chooses. They are available under the System.ValueTuple NuGet package for older framework versions. Syntax Following is the syntax for declaring a method that returns multiple values using ValueTuple − public (int, string, string) MethodName() { return (value1, value2, value3); } ...
Read MoreHow do I get a human-readable file size in bytes abbreviation using C#?
To get a human-readable file size in bytes with proper abbreviations, C# requires converting raw byte values into appropriate units like KB, MB, GB, or TB. This involves dividing by 1024 (or 1000 depending on your preference) and selecting the most appropriate unit to display. The key is to create a method that automatically determines the best unit and formats the size accordingly, rather than showing all units simultaneously. Syntax To get file size in bytes − long sizeInBytes = new FileInfo(filePath).Length; To format the size into human-readable format − string ...
Read MoreSortedDictionary.Item[] Property in C#
The SortedDictionary.Item[] property in C# provides indexer access to get or set values associated with specified keys in a sorted dictionary. This property allows you to access dictionary elements using square bracket notation, making it convenient to retrieve and modify values. Syntax Following is the syntax for the SortedDictionary.Item[] property − public TValue this[TKey key] { get; set; } Parameters key − The key of the value to get or set of type TKey. Return Value Returns the value of type TValue associated with the specified key. ...
Read MoreGetting the Type of the Tuple's Element in C#
In C#, you can get the type of a tuple or its individual elements using the GetType() method and reflection. The GetType() method returns the runtime type of the tuple, which includes information about all its element types. Syntax Following is the syntax to get the type of a tuple − var tuple = Tuple.Create(value1, value2, ...); Type tupleType = tuple.GetType(); To get the type of individual elements, you can use the typeof operator or access element types through reflection − Type elementType = typeof(T); // where T is the element type ...
Read MoreGet an enumerator that iterates through the StringDictionary in C#
The StringDictionary class in C# provides the GetEnumerator() method to obtain an enumerator that iterates through the collection. This enumerator returns DictionaryEntry objects containing key-value pairs. Note that StringDictionary automatically converts all keys to lowercase during storage. Syntax Following is the syntax for getting an enumerator from StringDictionary − IEnumerator enumerator = stringDictionary.GetEnumerator(); To iterate through the enumerator − while (enumerator.MoveNext()) { DictionaryEntry entry = (DictionaryEntry)enumerator.Current; // Use entry.Key and entry.Value } Using GetEnumerator() Method The following example demonstrates how to ...
Read MoreTotal number of elements in a specified dimension of an Array in C#
In C#, you can get the total number of elements in a specific dimension of an array using the GetLongLength() method. This method returns a long value representing the number of elements in the specified dimension, making it useful for both single-dimensional and multi-dimensional arrays. Syntax Following is the syntax for using GetLongLength() method − long count = array.GetLongLength(dimension); Parameters dimension − An integer that specifies the dimension (zero-based index) for which you want to get the number of elements. Return Value The method returns a long value representing ...
Read MoreWhat does the two question marks together (??) mean in C#?
The null-coalescing operator (??) in C# returns the value of its left-hand operand if it isn't null; otherwise, it evaluates and returns the right-hand operand. This operator provides a concise way to handle null values and assign default values. The ?? operator is particularly useful with nullable types, which can represent either a value from the type's domain or be undefined (null). It helps prevent InvalidOperationException exceptions and reduces the need for verbose null-checking code. Syntax Following is the syntax for the null-coalescing operator − result = leftOperand ?? rightOperand; If leftOperand is ...
Read MoreWhat is connection pooling in C# and how to achieve it?
Connection pooling in C# is a technique that improves database application performance by reusing database connections rather than creating new ones for each request. When a connection is closed, it's returned to a pool for future use instead of being destroyed. The .NET Framework automatically manages connection pooling for ADO.NET connections. When you use the using statement with database connections, it ensures proper disposal and automatic participation in connection pooling. How Connection Pooling Works Connection Pool Lifecycle Application Connection Pool ...
Read MoreUri.IsHexDigit() Method in C#
The Uri.IsHexDigit() method in C# determines whether a specified character is a valid hexadecimal digit. This method returns true if the character is a valid hexadecimal digit (0-9, A-F, a-f), and false otherwise. This method is commonly used when working with URL encoding, parsing hexadecimal values, or validating user input that should contain only hexadecimal characters. Syntax Following is the syntax − public static bool IsHexDigit(char ch); Parameters ch − The character to validate as a hexadecimal digit. Return Value Returns true if the character is ...
Read More