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 10 of 2547
C# Program to Create File and Write to the File
Creating files and writing data to them is a fundamental aspect of file handling in C#. The System.IO namespace provides several methods to create and write to files efficiently. This article covers various approaches to create files and write content using different methods. File handling operations rely on streams, which provide a generic view of a sequence of bytes. Streams serve as the gateway between your application and file operations, enabling input and output processes. Using File.WriteAllText() Method The File.WriteAllText() method is one of the simplest ways to create a file and write text content. It creates ...
Read MoreGet an enumerator that iterates through the SortedList in C#
The SortedList class in C# provides the GetEnumerator() method to retrieve an enumerator that iterates through key-value pairs. This method returns an IDictionaryEnumerator object that allows sequential access to each element in the sorted collection. Syntax Following is the syntax for getting an enumerator from a SortedList − IDictionaryEnumerator enumerator = sortedList.GetEnumerator(); Following is the syntax for iterating using the enumerator − while (enumerator.MoveNext()) { Console.WriteLine("Key = " + enumerator.Key + ", Value = " + enumerator.Value); } Return Value The GetEnumerator() method returns an IDictionaryEnumerator ...
Read MoreCopy ListDictionary to Array instance at the specified index in C#
The ListDictionary class in C# provides the CopyTo method to copy its elements to an array at a specified index. This method is useful when you need to convert dictionary data into array format or merge dictionary elements with existing array data. Syntax Following is the syntax for the CopyTo method − public void CopyTo(Array array, int index) Parameters array − The target array where elements will be copied. Must be of type DictionaryEntry[]. index − The zero-based index in the array at which copying begins. Using CopyTo with Index ...
Read MoreHow to rethrow InnerException without losing stack trace in C#?
In C#, rethrowing exceptions while preserving the complete stack trace is crucial for effective debugging. When handling exceptions, you have several options for rethrowing that affect how much debugging information is preserved. The key difference lies between using throw; (which preserves the full stack trace) versus throw ex; (which resets the stack trace to the current location). Additionally, when dealing with InnerException, you need special techniques to maintain the complete exception chain. Syntax Following is the syntax for rethrowing an exception without losing stack trace − try { // code that might ...
Read MoreCopy OrderedDictionary elements to Array instance at the specified index in C#
The OrderedDictionary class in C# provides the CopyTo() method to copy its elements to an array instance at a specified index. This method copies all key-value pairs as DictionaryEntry objects to the target array. Syntax Following is the syntax for copying OrderedDictionary elements to an array − orderedDictionary.CopyTo(array, index); Parameters array − The one-dimensional array that is the destination of the elements copied from OrderedDictionary. The array must have zero-based indexing. index − The zero-based index in the array at which copying begins. Using CopyTo() with ...
Read MoreHow to set a property value by reflection in C#?
The System.Reflection namespace in C# provides classes that allow you to obtain information about applications and dynamically add types, values, and objects at runtime. One of the most common uses of reflection is setting property values dynamically when you don't know the property name at compile time. Reflection allows you to examine various types in an assembly, instantiate these types, perform late binding to methods and properties, and even create new types at runtime. Syntax Following is the syntax for setting a property value using reflection − Type type = obj.GetType(); PropertyInfo property = type.GetProperty("PropertyName"); ...
Read MoreC# Program to Find Integer Numbers from the List of Objects and Sort them Using LINQ
In this article, we will learn how to write a C# program to find integer numbers from a list of objects and sort them using LINQ. LINQ (Language Integrated Query) is one of C#'s powerful features that enables developers to query data from various sources using a SQL-like syntax. It provides a standard approach for data manipulation and sorting, regardless of the data source. Problem Statement We need to extract integer numbers from a heterogeneous list containing different data types (strings, integers, characters) and sort them in ascending order. We'll use LINQ's OfType() method to filter integers and ...
Read MoreHow to implement coin change problem using bottom-up approach using C#?
The coin change problem is a classic dynamic programming challenge where we need to find the minimum number of coins required to make a given amount. The bottom-up approach builds solutions incrementally, starting from the smallest amounts and working up to the target amount. This approach uses dynamic programming to avoid recalculating the same subproblems multiple times, storing previously computed results in an array. Time complexity − O(n × t) where n is the amount and t is the number of coin types Space complexity − O(n) for the dynamic programming array Syntax Following is the ...
Read MoreCheck if two String objects have the same value in C#
To check if two String objects have the same value in C#, you can use several methods. The most common approach is using the Equals() method, which performs a case-sensitive comparison of string values. Syntax Following are the main syntax forms for comparing string values − string1.Equals(string2) string.Equals(string1, string2) string1 == string2 Using the Equals() Method The Equals() method is the recommended way to compare string values. It returns true if both strings have identical content − Example using System; public class Demo { public ...
Read MoreCopy StringCollection at the specified index of array in C#
The StringCollection.CopyTo() method in C# copies all elements from a StringCollection to a compatible array starting at the specified index. This method is particularly useful when you need to transfer string data from a collection to an array for further processing. Syntax Following is the syntax for the CopyTo() method − public void CopyTo(string[] array, int index) Parameters array − The one-dimensional array that is the destination of the elements copied from StringCollection. index − The zero-based index in the destination array at which copying begins. ...
Read More